Brigadier Documentation

repository·master·Indexed 25 days ago

https://github.com/mojang/brigadier

A high-performance command parser and dispatcher designed for Minecraft: Java Edition. Brigadier enables the creation of complex, hierarchical command trees using literal and argument nodes, supporting typed arguments, permission-aware usage information, and flexible execution via CommandDispatcher.

Tokens
1K
Snippets
3
Records
6
Agent score
37%

What's inside Brigadier

  1. Install Brigadier via Gradle

    master

    To use Brigadier in a Gradle project, add the Minecraft libraries repository and then include the brigadier dependency. Replace (the latest version) with the current version.

    maven {
        url "https://libraries.minecraft.net"
    }
    
    // ...
    
    dependency {
        compile 'com.mojang:brigadier:(the latest version)'
    }
  2. Install Brigadier via Maven

    master

    To use Brigadier in a Maven project, add the minecraft-libraries repository and then include the brigadier dependency. Replace (the latest version) with the current version.

    <repository>
      <id>minecraft-libraries</id>
      <name>Minecraft Libraries</name>
      <url>https://libraries.minecraft.net</url>
    </repository>
    
    <dependency>
        <groupId>com.mojang</groupId>
        <artifactId>brigadier</artifactId>
        <version>(the latest version)</version>
    </dependency>
  3. Parse and execute user input

    master

    There are two ways to handle user input:

    1. Direct Execution: Use dispatcher.execute(input, source) for a quick, single-step process. This returns an integer result from the command or throws a CommandSyntaxException if parsing fails.
    2. Two-Step Parsing: Use dispatcher.parse(input, source) to get a ParseResults<S> object. This is recommended for performance as the parsing step can be cached, and it allows for inspecting the command before execution.
  4. Inspect parsed command results

    master
    When using dispatcher.parse(input, source), the returned ParseResults<S> object allows you to inspect the command without executing it. It contains a possible context (including which nodes were matched and their positions in the input string) and a map of parse exceptions for nodes that could not be matched, explaining why the parse failed.
  5. Register commands with CommandDispatcher

    master

    Commands are built using a CommandDispatcher<S>, where <S> is your custom command source object. You build a command tree using a builder pattern.

    Key concepts:

    • Literal nodes: Created via literal("name"), requiring the user to type a specific string.
    • Argument nodes: Created via argument("name", type), which parse input into specific types (e.g., integer()).
    • Executes: An .executes(context -> { ... }) block defines the action to take when the command reaches that node.
    • Subcommands: Created by chaining .then() onto a node.
    CommandDispatcher<CommandSourceStack> dispatcher = new CommandDispatcher<>();
    
    dispatcher.register(
        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;
            })
    );
  6. Display command usage information

    master

    Brigadier provides two ways to generate human-readable usage strings for a specific CommandNode:

    • getAllUsage(node, source, restricted): Returns a list of all possible executable command paths under the node. If restricted is true, it only returns commands the source has permission to access (e.g., [foo, foo <bar>]).
    • getSmartUsage(node, source): Returns a map of child nodes to their "smart" usage paths, which attempt to squash future nodes and show optional/typed information (e.g., foo (<bar>)).