Skip to content

API reference / @jsprismarine/brigadier

@jsprismarine/brigadier ​

LicenseJoin the Discord ServerContributorsnpmCode CoverageTODOs CounterFIXMEs CounterGitHub Commit Activity

@jsprismarine/brigadier is a Node.js version of Mojang's Brigadier library. It is a command parser & dispatcher, originally designed and developed for Minecraft: Java Edition and now freely available for use elsewhere under the MIT license.

This project was originally developed by remtori as node-brigadier.

Prerequisites ​

Before you begin, ensure you have met the following requirements:

  • You have installed the latest version of Node.js and a package manager like npm, pnpm or bun.

Installation ​

To install @jsprismarine/brigadier, use the following command:

pnpm install @jsprismarine/brigadier --save

Usage ​

Dispatch a command ​

typescript
import { CommandDispatcher, literal, argument, string, Suggestions } from '@jsprismarine/brigadier';

// Define a BlockPos class
class BlockPos {
    constructor(x = 0, y = 0, z = 0) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    parse(reader) {
        this.x = reader.readInt();
        reader.skip();
        this.y = reader.readInt();
        reader.skip();
        this.z = reader.readInt();
        return this;
    }
    listSuggestions(context, builder) {
        return Suggestions.empty();
    }
    getExamples() {
        return ['1 2 3'];
    }
}

// Create a new CommandDispatcher
const dispatcher = new CommandDispatcher();

// Register a command
dispatcher.register(
    literal('fill').then(
        argument('pos1', new BlockPos()).then(
            argument('pos2', new BlockPos()).then(
                argument('block', string()).executes((context) => {
                    console.log(context.getArgument('pos1', BlockPos));
                    console.log(context.getArgument('pos2', BlockPos));
                    console.log(context.getArgument('block', 3));
                    return 0;
                })
            )
        )
    )
);

// Parse a command
const parsedCommand = dispatcher.parse('fill 3 4 5 10 11 12 air', {});

// Execute the command
try {
    dispatcher.execute(parsedCommand);
} catch (error) {
    console.error(error.getMessage());
}

Enumerations ​

StringType ​

Defined in: arguments/StringArgumentType.ts:5

Enumeration Members ​

GREEDY_PHRASE ​

GREEDY_PHRASE: "words with spaces"

Defined in: arguments/StringArgumentType.ts:8

QUOTABLE_PHRASE ​

QUOTABLE_PHRASE: "\"quoted phrase\""

Defined in: arguments/StringArgumentType.ts:7

SINGLE_WORD ​

SINGLE_WORD: "words_with_underscores"

Defined in: arguments/StringArgumentType.ts:6

Classes ​

Interfaces ​

ArgumentType ​

Defined in: arguments/ArgumentType.ts:27

Extended by ​

Type Parameters ​

T ​

T

Methods ​

getExamples()? ​

optional getExamples(): Iterable<string>

Defined in: arguments/ArgumentType.ts:32

Returns ​

Iterable<string>

listSuggestions()? ​

optional listSuggestions<S>(context, builder): Promise<Suggestions>

Defined in: arguments/ArgumentType.ts:30

Type Parameters ​
S ​

S

Parameters ​
context ​

CommandContext<S>

builder ​

SuggestionsBuilder

Returns ​

Promise<Suggestions>

parse() ​

parse(reader, context): T

Defined in: arguments/ArgumentType.ts:28

Parameters ​
reader ​

StringReader

context ​

ArgumentParseContext<any>

Returns ​

T

Type Aliases ​

ArgumentParseContext ​

ArgumentParseContext<S> = Pick<CommandContextBuilder<S>, "getSource">

Defined in: arguments/ArgumentType.ts:25

What an argument type can read from the command being parsed. The dispatcher passes its CommandContextBuilder, which is still being filled in, so only the command source is available at this point.

Type Parameters ​

S ​

S

Variables ​

bool ​

bool: () => BoolArgumentType

Defined in: index.ts:28

Returns ​

BoolArgumentType


float ​

float: (min, max) => FloatArgumentType

Defined in: index.ts:28

Parameters ​

min? ​

number = -Infinity

max? ​

number = Infinity

Returns ​

FloatArgumentType


greedyString ​

greedyString: () => StringArgumentType

Defined in: index.ts:28

Returns ​

StringArgumentType


integer ​

integer: (min, max) => IntegerArgumentType

Defined in: index.ts:28

Parameters ​

min? ​

number = -Infinity

max? ​

number = Infinity

Returns ​

IntegerArgumentType


string ​

string: () => StringArgumentType

Defined in: index.ts:28

Returns ​

StringArgumentType


word ​

word: () => StringArgumentType

Defined in: index.ts:28

Returns ​

StringArgumentType

Functions ​

argument() ​

argument<S, T>(name, type): RequiredArgumentBuilder<S, T>

Defined in: tree/ArgumentCommandNode.ts:186

Starts a required argument node builder.

Type Parameters ​

S ​

S

T ​

T

Parameters ​

name ​

string

type ​

ArgumentType<T>

Returns ​

RequiredArgumentBuilder<S, T>


literal() ​

literal<S>(name): LiteralArgumentBuilder<S>

Defined in: tree/LiteralCommandNode.ts:153

Starts a literal command node builder.

Type Parameters ​

S ​

S

Parameters ​

name ​

string

Returns ​

LiteralArgumentBuilder<S>