Skip to content

Motivation

Nicolai edited this page Feb 3, 2021 · 2 revisions

While Brigadier is a great command library, writing the actual commands is not so great because of its nesting/indentation based style, and because of its comparatively verbose syntax.

Another problem with Brigadier is its unsafe retrieval of argument values, because values are retrieved by their argument name, and the class of the type they want the value to be casted as. This will cause an exception to be thrown if either the argument name is incorrect, or if the type does not match. Furthermore, Brigadier also does not have a concept of optional arguments, this can be inconvenient at best, but it can also lead to a lot of code duplication. This make commands harder to maintain.

Comparison

Below are shown two versions of the example command from the Brigadier README page on GitHub, that demonstrates the improvements in readability and simplicity with just a simple command. The first version is written using the standard Java Brigadier syntax, and the second is written using the Kotlin DSL.

Standard Java
literal("foo")
    .then(
        argument("bar", integer())
            .executes(c -> {
                System.out.println("Bar is " + getInteger(c, "bar"));
                return 1;
            })
    )
    .executes(c -> {
        System.out.println("Called foo with no arguments");
        return 1;
    })
Kotlin DSL
command<Any>("foo") {
    val bar by integer("bar").optional()

    runs {
        if (bar != null) {
            println("Bar is $bar")
        } else {
            println("Called foo with no arguments")
        }
    }
}
Clone this wiki locally