• Stars
    star
    3,327
  • Rank 12,919 (Top 0.3 %)
  • Language
    Java
  • License
    MIT License
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Brigadier Latest release License

Brigadier is a command parser & dispatcher, designed and developed for Minecraft: Java Edition and now freely available for use elsewhere under the MIT license.

Installation

Brigadier is available to Maven & Gradle via libraries.minecraft.net. Its group is com.mojang, and artifact name is brigadier.

Gradle

First include our repository:

maven {
    url "https://libraries.minecraft.net"
}

And then use this library (change (the latest version) to the latest version!):

compile 'com.mojang:brigadier:(the latest version)'

Maven

First include our repository:

<repository>
  <id>minecraft-libraries</id>
  <name>Minecraft Libraries</name>
  <url>https://libraries.minecraft.net</url>
</repository>

And then use this library (change (the latest version) to the latest version!):

<dependency>
    <groupId>com.mojang</groupId>
    <artifactId>brigadier</artifactId>
    <version>(the latest version)</version>
</dependency>

Contributing

Contributions are welcome! :D

Most contributions will require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Usage

At the heart of Brigadier, you need a CommandDispatcher<S>, where <S> is any custom object you choose to identify a "command source".

A command dispatcher holds a "command tree", which is a series of CommandNodes that represent the various possible syntax options that form a valid command.

Registering a new command

Before we can start parsing and dispatching commands, we need to build up our command tree. Every registration is an append operation, so you can freely extend existing commands in a project without needing access to the source code that created them.

Command registration also encourages the use of a builder pattern to keep code cruft to a minimum.

A "command" is a fairly loose term, but typically it means an exit point of the command tree. Every node can have an executes function attached to it, which signifies that if the input stops here then this function will be called with the context so far.

Consider the following example:

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;
        })
);

This snippet registers two "commands": foo and foo <bar>. It is also common to refer to the <bar> as a "subcommand" of foo, as it's a child node.

At the start of the tree is a "root node", and it must have LiteralCommandNodes as children. Here, we register one command under the root: literal("foo"), which means "the user must type the literal string 'foo'".

Under that is two extra definitions: a child node for possible further evaluation, or an executes block if the user input stops here.

The child node works exactly the same way, but is no longer limited to literals. The other type of node that is now allowed is an ArgumentCommandNode, which takes in a name and an argument type.

Arguments can be anything, and you are encouraged to build your own for seamless integration into your own product. There are some standard arguments included in brigadier, such as IntegerArgumentType.

Argument types will be asked to parse input as much as they can, and then store the "result" of that argument however they see fit or throw a relevant error if they can't parse.

For example, an integer argument would parse "123" and store it as 123 (int), but throw an error if the input were onetwothree.

When a command is actually run, it can access these arguments in the context provided to the registered function.

Parsing user input

So, we've registered some commands and now we're ready to take in user input. If you're in a rush, you can just call dispatcher.execute("foo 123", source) and call it a day.

The result of execute is an integer that was returned from an evaluated command. The meaning of this integer depends on the command, and will typically not be useful to programmers.

The source is an object of <S>, your own custom class to track users/players/etc. It will be provided to the command so that it has some context on what's happening.

If the command failed or could not parse, some form of CommandSyntaxException will be thrown. It is also possible for a RuntimeException to be bubbled up, if not properly handled in a command.

If you wish to have more control over the parsing & executing of commands, or wish to cache the parse results so you can execute it multiple times, you can split it up into two steps:

final ParseResults<S> parse = dispatcher.parse("foo 123", source);
final int result = execute(parse);

This is highly recommended as the parse step is the most expensive, and may be easily cached depending on your application.

You can also use this to do further introspection on a command, before (or without) actually running it.

Inspecting a command

If you parse some input, you can find out what it will perform (if anything) and provide hints to the user safely and immediately.

The parse will never fail, and the ParseResults<S> it returns will contain a possible context that a command may be called with (and from that, you can inspect which nodes the user entered, complete with start/end positions in the input string). It also contains a map of parse exceptions for each command node it encountered. If it couldn't build a valid context, then the reason why is inside this exception map.

Displaying usage info

There are two forms of "usage strings" provided by this library, both require a target node.

getAllUsage(node, source, restricted) will return a list of all possible commands (executable end-points) under the target node and their human readable path. If restricted, it will ignore commands that source does not have access to. This will look like [foo, foo <bar>].

getSmartUsage(node, source) will return a map of the child nodes to their "smart usage" human readable path. This tries to squash future-nodes together and show optional & typed information, and can look like foo (<bar>).

GitHub forks GitHub stars

More Repositories

1

DataFixerUpper

A set of utilities designed for incremental building, merging and optimization of data transformations.
Java
1,145
star
2

bedrock-samples

Samples and assets for building Minecraft: Bedrock Edition add-ons
655
star
3

ore-ui

πŸ’Ž Building blocks to construct game UIs using web tech.
TypeScript
378
star
4

leveldb-mcpe

A fork of Google's LevelDB with added zlib compression and windows support
C++
264
star
5

LegacyLauncher

Hacky code to launch our old versions from the new launcher!
Java
245
star
6

minecraft-editor

Minecraft Editor is in early development. It is an in-engine, multiblock editing experience focused on making it possible for creators of all skillsets to easily craft high-quality experiences in Bedrock.
233
star
7

bedrock-protocol-docs

Documentation of the Bedrock network protocol. Protocol is subject to change release over release.
HTML
195
star
8

slicer

Resource pack migration tool for Minecraft 1.14
Java
163
star
9

Sift

A lightweight and easy-to-use tool for accessing your clouds
JavaScript
146
star
10

web-theme-bootstrap

Contains the theme used on minecraft.net based on Boostrap 4
SCSS
123
star
11

AccountsClient

Java client for accessing Mojang's account/profile API
Java
115
star
12

blixtser

Fast Serialization
Java
66
star
13

minecraft-creator-tools

A set of tools for creating content and add-ons for Minecraft Bedrock Edition.
TypeScript
46
star
14

stats.minecraft.net

Show them datas
JavaScript
43
star
15

TextureEnder

A tool to convert Minecraft Texture Packs to Resource Packs.
Java
43
star
16

minecraft-debugger

TypeScript
36
star
17

EducationContent

Content repository for MEE
TypeScript
34
star
18

minecraft-legends-docs

Houses public-facing documentation of content formats for Minecraft Legends
25
star
19

minecraft-editor-extension-starter-kit

A repo containing the build pipeline, libraries, and types required for a 3rd party to build a Minecraft Editor Extension
PowerShell
21
star
20

mojang.github.io

JavaScript
21
star
21

t-component

React translation component
TypeScript
17
star
22

dcpu-16

Documentation for DCPU-16 and related hardware.
16
star
23

minecraft-scripting-libraries

Sets of typescript scripting libraries for use with the minecraft scripting modules.
TypeScript
12
star
24

LegacyUnifont

Tracking Java Edition changes to Unifont
C
10
star
25

minecraft-editor-extension-samples

Minecraft Bedrock Editor Extension samples repo.
TypeScript
10
star
26

i18n-scripts

Localization scripts for t-component
JavaScript
9
star
27

legends-blockbench-plugin

Blockbench plugin for exporting Minecraft Legends content
JavaScript
9
star
28

PaintScan

[InventionWeek2020] Paint, scan, and see your own creations in Minecraft!
Java
9
star
29

breakpad

C++
6
star
30

FakeIt

FakeIt repo with fixes needed by our code not included in the main repo (https://github.com/eranpeer/FakeIt)
C++
5
star
31

geojson2fbx

Tool to convert geo.json content into FBX
4
star
32

sloedeployinfra

Azure Function for Commit Status Updates
PowerShell
3
star
33

OrcaSettings

Misc settings for the 3d printer firmware/slicer/etc
3
star
34

cef

Fork of CEF, with changes required for the launcher. See branch 2883.
C++
3
star
35

SFAT-Data-Explorer

C++
2
star
36

poc_precompiled_headers

C++
2
star
37

freetype2

Fork of git://git.sv.nongnu.org/freetype/freetype2.git
C
2
star
38

react-native-0.64

JavaScript
2
star
39

PaintScanAR

PaintScan experiments. Notable piece is use of AR.
Java
2
star
40

PRONYas

A tool for populating word document templates with data
Visual Basic .NET
2
star
41

redux-persist-01

JavaScript
1
star
42

POC_TIME_TRACE_FLAG

C++
1
star