gitmyhub

brigadier

Java ★ 3.7k updated 1y ago

Brigadier is a command parser & dispatcher, designed and developed for Minecraft: Java Edition.

Brigadier is Mojang's open-source Java library for parsing and dispatching commands using a tree structure, originally built for Minecraft and now available for any JVM project that needs typed command parsing with autocomplete hints.

JavaMavenGradlesetup: easycomplexity 2/5

Brigadier is a command parsing and dispatching library built by Mojang, the company behind Minecraft. It was originally created for Minecraft: Java Edition and later released as a standalone open-source library available to any Java or JVM project under the MIT license.

The core idea is that you define a tree of possible command structures, and when a user types something, the library figures out which branch of that tree matches what they typed, validates any arguments, and calls the appropriate function. You build this tree by registering commands one at a time. Each command can be a fixed keyword, an argument that accepts a specific type of value like an integer, or a combination where a keyword is followed by one or more arguments. The README shows an example where typing "foo" runs one piece of code and typing "foo 123" runs a different piece.

Argument types handle the parsing and validation. The library includes some built-in types, and you can write your own if you need a custom format. When parsing fails because the user typed something the library cannot understand, it throws a specific exception type that describes what went wrong, which makes it straightforward to show the user a helpful error message.

For usage hints, the library can generate a list of all valid command patterns under any node in the tree, or a condensed "smart usage" summary that groups optional parts together. These are designed to be shown to users as autocomplete suggestions or help text.

The library is available through Maven and Gradle via Mojang's public repository. Contributions are welcome but require a Contributor License Agreement, and the project follows Microsoft's open-source code of conduct.

Where it fits