🇬🇧 English
🇬🇧 English
Appearance
🇬🇧 English
🇬🇧 English
Appearance
This page is written for version:
1.21.4
Creating commands can allow a mod developer to add functionality that can be used through a command. This tutorial will teach you how to register commands and the general command structure of Brigadier.
Commands are registered within the CommandHandler provided by the Bridge.
ItemHandler itemHandler = bridge.GetCommandHandler();not yet implementedCommands should be registered on your mods startup. New commands cannot be registered during runtime and the command result will then return false.
Commands can be build fluently using the CreateCommand() method.
CommandBuilder testCommand = commandHandler.CreateCommand("test_command")
.Executes(async context => await context.SendFeedback("Called /test_command"));not yet implementedThe context is of type CommandContext and it provides access to the CommandSource, which in turn gives access to (nearly) all context that java would have too.
For example the player name that executed the command can be gotten by running:
await context.GetSource().GetName();not yet implementedThe CommandContext might vary depending on if the command is a client or server command. (e.g. the NotInAnyWorld() method is only available in server commands)
After a command was build, it has to be registered using the RegisterCommandAsync method.
await commandHandler.RegisterCommandAsync(testCommand);not yet implementedTo execute this command, you must type /test_command, which is case-sensitive.
INFO
From this point onwards, we will be extracting the logic written within the lambda passed into .execute() builders into individual methods. We can then pass a method reference to .execute(). This is done for clarity.
If desired, you can also make sure a command is only registered under some specific circumstances, for example, only in the dedicated environment:
The environment context is not yet available in the JavaBridge.
Let's say you have a command that you only want operators to be able to execute. This is where the Requires() method comes into play. The Requires() method has one argument of a Func<S> which will supply a ServerCommandSource to test with and determine if the CommandSource can execute the command.
CommandBuilder requiredCommand = commandHandler.CreateCommand("required_command")
.Requires(async source => await source.HasPermissionLevel(1))
.Executes(ExecuteRequiredCommand);not yet implementedprivate static async Task ExecuteRequiredCommand(CommandContext context)
{
await context.SendFeedback("Called /required_command");
}not yet implementedThis command will only execute if the source of the command is a level 2 operator at a minimum, including command blocks. Otherwise, the command is not registered.
This has the side effect of not showing this command in tab completion to anyone who is not a level 2 operator. This is also why you cannot tab-complete most commands when you do not enable cheats.
To add a sub command, you register the first literal node of the command normally. To have a sub command, you have to append the next literal node to the existing node.
CommandBuilder subCommandOne = commandHandler.CreateCommand("sub_command_one")
.Executes(ExecuteSubCommandOne);
CommandBuilder commandOne = commandHandler.CreateCommand("command_one")
.AddSubCommand(subCommandOne);
await commandHandler.RegisterCommandAsync(commandOne);not yet implementedprivate static async Task ExecuteSubCommandOne(CommandContext context)
{
await context.SendFeedback("Called /sub_command_one");
}not yet implementedSimilar to arguments, sub command nodes can also be set optional. In the following case, both /command_two and /command_two sub_command_two will be valid.
CommandBuilder subCommandTwo = commandHandler.CreateCommand("sub_command_two")
.Executes(ExecuteSubCommandTwo);
CommandBuilder commandTwo = commandHandler.CreateCommand("command_two")
.Executes(ExecuteCommandTwo)
.AddSubCommand(subCommandTwo);
await commandHandler.RegisterCommandAsync(commandTwo);not yet implementedprivate static async Task ExecuteCommandTwo(CommandContext context)
{
await context.SendFeedback("Called /command_two");
}
private static async Task ExecuteSubCommandTwo(CommandContext context)
{
await context.SendFeedback("Called /sub_command_two");
}not yet implementedClient commands can be registered almost identically, but instead you have to get the client CommandHandler.
CommandHandler clientCommandHandler = bridge.GetClientCommandHandler();not yet implementedCommand redirects - also known as aliases - are a way to redirect the functionality of one command to another. This is useful for when you want to change the name of a command, but still want to support the old name.
Command redirects are not yet available in the JavaBridge.