tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:commands [2020/06/14 04:55] – Mark the construction site i509vcb | tutorial:commands [2024/02/23 14:09] – Adding one question in FAQ section allen1210 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Licensing: The code in this article is licensed under the " | ||
+ | |||
====== Creating Commands ====== | ====== Creating Commands ====== | ||
- | Creating commands can allow a mod developer to add functionality that can used through a command. | + | Creating commands can allow a mod developer to add functionality that can used through a command. This tutorial will teach you how to register commands, and the general command structure of Brigadier. |
- | This tutorial will teach you how to register commands, and the general command structure of Brigadier. | + | |
- | + | ||
- | Note: All code written here was written for 1.14.4. Some mappings may have changed in yarn, but all code should still be applicable. | + | |
===== What is Brigadier? ===== | ===== What is Brigadier? ===== | ||
Line 12: | Line 11: | ||
The source code for brigadier can be found here: https:// | The source code for brigadier can be found here: https:// | ||
- | ===== What is a command? | + | ===== The '' |
- | Brigadier requires you specify the '' | + | In Minecraft, |
- | A '' | + | |
The single method in '' | The single method in '' | ||
- | A command can be implemented in several ways as shown below: | + | Like other functional interfaces, it is usually used as a lambda or a method reference: |
- | **__As a lambda__** | + | <code java> |
- | <code java [enable_line_numbers=" | + | Command<ServerCommandSource> command = context -> { |
- | Command<Object> command = context -> { | + | |
return 0; | return 0; | ||
}; | }; | ||
</ | </ | ||
- | **__As an anonymous class__** | + | The integer can be considered the result of the command. Typically negative values mean a command has failed and will do nothing. A result of '' |
- | <code java [enable_line_numbers=" | + | |
- | Command< | + | |
- | @Override | + | |
- | public int run(CommandContext< | + | |
- | return | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | **__Implemented as a class__** | ||
- | <code java [enable_line_numbers=" | ||
- | final class XYZCommand implements Command< | ||
- | @Override | ||
- | public int run(CommandContext< | ||
- | return 0; | ||
- | } | ||
- | } | ||
- | </ | ||
- | **__As a method reference__** | + | ==== What can the ServerCommandSource do? ==== |
- | <code java [enable_line_numbers=" | + | |
- | void registerCommand() { | + | |
- | // Ignore this for now, we will explain it next. | + | |
- | dispatcher.register(CommandManager.literal(" | + | |
- | .executes(this:: | + | |
- | } | + | |
- | private int execute(CommandContext< | + | A '' |
- | return 0; | + | |
- | } | + | |
- | </ | + | |
+ | <code java [enable_line_numbers=" | ||
+ | // Get the source. This will always work. | ||
+ | final ServerCommandSource source = ctx.getSource(); | ||
- | The '' | + | // Unchecked, may be null if the sender was the console or the command block. |
+ | final @Nullable Entity sender = source.getEntity(); | ||
- | The integer can be considered | + | // Will throw an exception if the executor |
+ | // The result of this could contain | ||
+ | // This method will require your methods to throw a CommandSyntaxException. | ||
+ | // The entity options in ServerCommandSource could return a CommandBlock entity, any living entity or a player. | ||
+ | final @NotNull Entity sender2 = source.getEntityOrThrow(); | ||
- | ===== A basic command ===== | + | // null if the executor of the command |
+ | final @Nullable ServerPlayerEntity player | ||
- | Below is a command that contains no arguments: | + | // Will throw an exception if the executor of the command was not explicitly |
+ | // Also will send feedback telling the sender of the command that they must be a player. | ||
+ | // This method will require your methods to throw a CommandSyntaxException. | ||
+ | final @NotNull ServerPlayerEntity player = source.getPlayerOrThrow(); | ||
- | <code java [enable_line_numbers=" | + | // Gets the sender' |
- | dispatcher.register(CommandManager.literal(" | + | // This could be the location of the entity/ |
- | | + | final Vec3d position = source.getPosition(); |
- | return 1; | + | // Gets the world the sender is within. The console' |
- | })); | + | final ServerWorld world = source.getWorld(); |
- | </ | + | |
- | '' | + | // Gets the sender's rotation as a Vec2f. |
- | To execute this command, one must type ''/ | + | final Vec2f rotation = source.getRotation(); |
- | ==== A sub command ==== | + | // Access to the instance of the MinecraftServer this command |
+ | final MinecraftServer server | ||
- | To add a sub command, | + | // The name of the command |
+ | // the name of a CommandBlock that has been renamed before being placed down, or in the case of the Console, " | ||
+ | final String name = source.getName(); | ||
- | <code java [enable_line_numbers=" | + | // Returns |
- | dispatcher.register(CommandManager.literal(" | + | // This is based on the operator status of the sender. |
+ | // (On an integrated server, the player must have cheats enabled to execute these commands.) | ||
+ | final boolean b = source.hasPermissionLevel(int level); | ||
</ | </ | ||
- | In order to have a sub command, one needs to append the next node to the existing node. This is done use the '' | + | ===== Register |
- | This creates the command | + | Commands are registered by registering in '' |
- | <code java [enable_line_numbers=" | + | The event should be registered in your mod's initializer. The callback has three parameters. The '' |
- | dispatcher.register(CommandManager.literal(" | + | |
- | .then(CommandManager.literal(" | + | To simplify the code, it is highly recommended to '' |
- | ); | + | <code java> |
+ | import static net.minecraft.server.command.CommandManager.*; | ||
</ | </ | ||
- | It is advised to indent your code as you add nodes to the command. Usually the indentation corresponds to how many nodes deep one is on the command tree. The new line also makes it visible that another node is being added. There are alternative styles to formatting | + | In the mod initializer, |
- | **So let's try running the command** | + | <code java [enable_line_numbers=" |
- | + | public class ExampleMod implements ModInitializer { | |
- | Most likely if you typed ''/ | + | @Override |
- | + | public void onInitialize() { | |
- | <code java [enable_line_numbers=" | + | CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, |
- | dispatcher.register(CommandManager.literal(" | + | |
- | | + | |
.executes(context -> { | .executes(context -> { | ||
- | System.out.println(" | + | // For versions below 1.19, replace " |
+ | // For versions below 1.20, remode "() ->" directly. | ||
+ | context.getSource().sendFeedback(() -> Text.literal(" | ||
- | | + | |
- | }) | + | }))); |
- | | + | } |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ===== Registering the commands ===== | + | |
- | + | ||
- | Registering commands is done by registering a callback using the '' | + | |
- | + | ||
- | The event should be registered in your mod's initializer. The callback has two parameters. The '' | + | |
- | + | ||
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | public class ExampleCommandMod implements ModInitializer { | + | |
- | @Override | + | |
- | public void onInitialize() { | + | |
- | CommandRegistrationCallback.EVENT.register((dispatcher, | + | |
- | ... | + | |
- | }); | + | |
- | | + | |
} | } | ||
</ | </ | ||
- | Inside your lambda, | + | **Please ensure you import the correct static |
- | <code java [enable_line_numbers=" | + | In the '' |
- | public class ExampleCommandMod implements ModInitializer { | + | |
- | @Override | + | |
- | public void onInitialize() { | + | |
- | CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { | + | |
- | dispatcher.register(CommandManager.literal(" | + | |
- | System.out.println(" | + | |
- | return 1; | + | |
- | })); | + | |
- | }); | + | |
- | } | + | |
- | } | + | |
- | </code> | + | |
- | If desired, you can make sure a command is only registered on a dedicated server by checking the '' | + | If the command fails, instead of calling '' |
+ | To execute this command, you must type ''/ | ||
- | <code java [enable_line_numbers=" | + | ===== Registration environment ===== |
- | public class ExampleCommandMod implements ModInitializer { | + | If desired, you can also make sure a command is only registered under some specific circumstances, for example, only in the dedicated |
- | @Override | + | |
- | public void onInitialize() { | + | |
- | CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { | + | |
- | if (dispatcher) { | + | |
- | TestDedicatedCommand.register(dispatcher); | + | |
- | } | + | |
- | }); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | And vice versa | + | <yarncode |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
public class ExampleCommandMod implements ModInitializer { | public class ExampleCommandMod implements ModInitializer { | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | CommandRegistrationCallback.EVENT.register((dispatcher, | + | CommandRegistrationCallback.EVENT.register((dispatcher, |
- | if (!dispatcher) { | + | if (environment.field_25423) { |
- | | + | ...; |
} | } | ||
}); | }); | ||
} | } | ||
} | } | ||
- | </code> | + | </yarncode> |
- | + | ||
- | ===== Arguments ===== | + | |
- | + | ||
- | Arguments in Brigadier both parse and error check any inputted arguments. | + | |
- | Minecraft creates some special argument types for it's own use such as the '' | + | |
===== Static Imports ===== | ===== Static Imports ===== | ||
- | You could type out '' | + | In the example above, the use of static imports is used for code simplifying. For a literal |
- | This also works for Minecraft' | + | |
- | And your imports | + | Below is an example of some static |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
// getString(ctx, | // getString(ctx, | ||
Line 201: | Line 142: | ||
// argument(" | // argument(" | ||
import static net.minecraft.server.command.CommandManager.argument; | import static net.minecraft.server.command.CommandManager.argument; | ||
- | // Import everything | + | // Import everything |
import static net.minecraft.server.command.CommandManager.*; | import static net.minecraft.server.command.CommandManager.*; | ||
</ | </ | ||
- | Note: Please be sure you use the '' | + | Note: Please be sure you use the '' |
Brigadier' | Brigadier' | ||
- | Minecraft' | + | Minecraft' |
- | CommandManager is in '' | + | |
- | ===== Advanced concepts | + | ===== Add Requirements |
- | Below are links to the articles about more complex concepts used in brigadier. | + | Let' |
- | + | ||
- | ^ Page ^ Description | + | |
- | | [[tutorials: | + | |
- | | [[tutorials: | + | |
- | | [[tutorials: | + | |
- | | [[tutorials: | + | |
- | | [[tutorials: | + | |
- | | [[tutorials: | + | |
- | + | ||
- | **TODO:** Sections are being moved to sub categories and will be added to their respective articles as they are migrated. | + | |
- | + | ||
- | ====== Sorry for the mess ====== | + | |
- | + | ||
- | __Currently this article is being migrated, so thing may be a mess. Below is are the parts of the article that are yet to be migrated to the new format.__ | + | |
- | + | ||
- | ===== Requirements ===== | + | |
- | + | ||
- | Lets say you have a command you only want operators to be able to execute. This is where the '' | + | |
For example this may look like the following: | For example this may look like the following: | ||
Line 238: | Line 160: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
dispatcher.register(literal(" | dispatcher.register(literal(" | ||
- | .requires(source -> source.hasPermissionLevel(4)) | + | |
- | .executes(ctx -> { | + | .executes(ctx -> { |
- | ctx.getSource().sendFeedback(new LiteralText("You are an operator", | + | ctx.getSource().sendFeedback(() -> Text.literal("You are an operator" |
- | return 1; | + | return 1; |
- | }); | + | }); |
</ | </ | ||
- | This command will only execute if the Source | + | This command will only execute if the source |
- | Nothing prevents someone from specifying calls to permissions implementations within the '' | + | To create commands that only level 4 operators (//not including// command blocks) can execute, use '' |
- | ===== Exceptions | + | ===== Arguments |
- | Brigadier supports command exceptions which can be used to end a command such as if an argument | + | Arguments are used in most of commands. Sometimes they can be optional, which means if you do not provide that argument, the command |
- | All the exceptions from Brigadier are based on the CommandSyntaxException. The two main types of exceptions Brigadier provides are Dynamic and Simple exception types, of which you must '' | + | In this case, we add one integer argument, and calculate |
- | Below is a coin flip command to show an example of exceptions in use. | + | |
- | <code java [enable_line_numbers=" | + | <code java> |
- | dispatcher.register(CommandManager.literal(" | + | dispatcher.register(literal(" |
- | .executes(ctx -> { | + | |
- | | + | |
- | + | final int value = IntegerArgumentType.getInteger(context, " | |
- | | + | final int result = value * value; |
- | ctx.getSource().sendMessage(new TranslateableText("coin.flip.heads")) | + | |
- | return | + | return |
- | } | + | }))); |
- | + | ||
- | throw new SimpleCommandExceptionType(new TranslateableText(" | + | |
- | })); | + | |
</ | </ | ||
- | Though | + | In this case, after the word ''/ |
- | <code java [enable_line_numbers=" | + | Note: for simplicity, '' |
- | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | + | |
- | return new LiteralText(" | + | |
- | }); | + | |
- | </ | + | |
- | There are more Dynamic exception types which each take a different amount of arguments into account ('' | + | Then we add an optional second |
- | You should remember that the Dynamic exceptions takes an object as an argument | + | <code java> |
- | + | dispatcher.register(literal(" | |
- | ===== Redirects (Aliases) ===== | + | .then(argument("value", IntegerArgumentType.integer()) |
- | + | | |
- | Redirects are Brigadier' | + | final int value = IntegerArgumentType.getInteger(context, |
- | + | final int result = value * value; | |
- | <code java [enable_line_numbers=" | + | context.getSource().sendFeedback(() -> Text.literal("%s × %s = %s".formatted(value, value, result)), false); |
- | public static void register(CommandDispatcher< | + | |
- | LiteralCommandNode node = registerMain(dispatcher); | + | }) |
- | dispatcher.register(literal(" | + | |
- | .redirect(node)); // Alias 1, redirect to main command | + | .executes(context |
- | dispatcher.register(literal("w") | + | final int value = IntegerArgumentType.getInteger(context, " |
- | | + | final int value2 = IntegerArgumentType.getInteger(context, "value2"); |
- | } | + | final int result = value * value2; |
- | + | context.getSource().sendFeedback(() -> Text.literal("%s × %s = %s".formatted(value, | |
- | public static LiteralCommandNode registerMain(CommandDispatcher< | + | |
- | | + | |
- | .then(argument("targets", EntityArgumentType.players()) | + | |
- | .then(argument(" | + | |
- | .executes(ctx -> { | + | |
- | | + | |
- | })))); | + | |
- | } | + | |
</ | </ | ||
- | The redirect tells brigadier | + | Now you can type one or two integers. If you give one integer, that square of integer will be calculated. If you provide two integers, their product will be calculated. You may find it unnecessary |
- | ===== Redirects | + | <code java> |
- | Commands such as ''/ | + | public class ExampleMod implements ModInitializer { |
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | | ||
+ | .then(argument(" | ||
+ | .executes(context -> executeMultiply(IntegerArgumentType.getInteger(context, | ||
+ | .then(argument(" | ||
+ | .executes(context -> executeMultiply(IntegerArgumentType.getInteger(context, | ||
+ | } | ||
- | <code java [enable_line_numbers=" | + | private static int executeMultiply(int value, int value2, CommandContext<ServerCommandSource> context) { |
- | LiteralCommandNode< | + | final int result = value * value2; |
- | LiteralCommandNode< | + | |
- | // You can register under the same literal more than once, it will just register new parts of the branch as shown below if you register a duplicate branch an error will popup in console warning of conflicting commands but one will still work. | + | return |
- | .then(literal(" | + | } |
- | .then(literal(" | + | |
- | .redirect(root, this:: | + | |
- | .then(literal(" | + | |
- | .redirect(root, | + | |
- | .then(literal(" | + | |
- | .executes(ctx -> { | + | |
- | ctx.getSource().sendFeedback(new LiteralText(" | + | |
- | return Command.SINGLE_SUCCESS; | + | |
- | }))); | + | |
- | </code> | + | |
- | The redirect can also modify the CommandSource by use of a '' | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | .redirect(rootNode, | + | |
- | | + | |
- | }) | + | |
- | </ | + | |
- | + | ||
- | ===== What can the ServerCommandSource do? ===== | + | |
- | + | ||
- | A server command source provides some additional implementation specific context when a command is run. This includes the ability to get the entity that executed the command, the world the command was ran in or the server the command was run on. | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | final ServerCommandSource source = ctx.getSource(); | + | |
- | // Get the source. This will always work. | + | |
- | + | ||
- | final Entity sender = source.getEntity(); | + | |
- | // Unchecked, may be null if the sender was the console. | + | |
- | + | ||
- | final Entity sender2 = source.getEntityOrThrow(); | + | |
- | // Will end the command if the source of the command was not an Entity. | + | |
- | // The result | + | |
- | // This method will require your methods to throw a CommandSyntaxException. | + | |
- | // The entity options in ServerCommandSource could return a CommandBlock entity, any living entity or a player. | + | |
- | + | ||
- | final ServerPlayerEntity player | + | |
- | // Will end the command if the source of the command was not explicitly a Player. Also will send feedback telling the sender of the command that they must be a player. | + | |
- | + | ||
- | source.getPosition(); | + | |
- | // Get's the sender' | + | |
- | + | ||
- | source.getWorld(); | + | |
- | // Get's the world the sender is within. The console' | + | |
- | + | ||
- | source.getRotation(); | + | |
- | // Get's the sender' | + | |
- | + | ||
- | source.getMinecraftServer(); | + | |
- | // Access to the instance of the MinecraftServer this command was ran on. | + | |
- | + | ||
- | source.getName(); | + | |
- | // The name of the command source. This could be the name of the entity, player, the name of a CommandBlock that has been renamed before being placed down or in the case of the Console, " | + | |
- | + | ||
- | source.hasPermissionLevel(int level); | + | |
- | // Returns true if the source of the command has a certain permission level. This is based on the operator status of the sender. (On an integrated server, the player must have cheats enabled to execute these commands) | + | |
- | </ | + | |
- | + | ||
- | ===== Some example commands examples ===== | + | |
- | + | ||
- | === Broadcast a message === | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | public static void register(CommandDispatcher< | + | |
- | | + | |
- | | + | |
- | .then(argument(" | + | |
- | .then(argument(" | + | |
- | .executes(ctx | + | |
- | } | + | |
- | + | ||
- | public static int broadcast(ServerCommandSource source, Formatting formatting, String message) { | + | |
- | final Text text = new LiteralText(message).formatting(formatting); | + | |
- | + | ||
- | source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, false); | + | |
- | return | + | |
} | } | ||
</ | </ | ||
+ | ===== A sub command ===== | ||
- | ==== / | + | To add a sub command, you register the first literal node of the command normally. |
- | First the basic code where we register " | + | < |
- | + | dispatcher.register(literal(" | |
- | < | + | |
- | public static LiteralCommandNode register(CommandDispatcher< | + | |
- | return | + | |
- | .executes(ctx -> giveDiamond(ctx))); | + | |
- | } | + | |
</ | </ | ||
- | Then since we only want to give to players, we check if the CommandSource is a player. But we can use '' | + | In order to have a sub command, one needs to append |
- | <code java [enable_line_numbers=" | + | This creates |
- | public static int giveDiamond(CommandContext< | + | |
- | final ServerCommandSource source = ctx.getSource(); | + | |
- | + | ||
- | final PlayerEntity self = source.getPlayer(); | + | |
- | </code> | + | |
- | + | ||
- | Then we add to the player's inventory, with a check to see if the inventory is full: | + | |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | if(!player.inventory.insertStack(new ItemStack(Items.DIAMOND))){ | + | dispatcher.register(literal(" |
- | | + | |
- | } | + | |
+ | // For versions below 1.19, use '' | ||
+ | // For versions below 1.20, use directly the '' | ||
+ | context.getSource().sendFeedback(() -> Text.literal(" | ||
- | | + | |
- | } | + | }) |
+ | ) | ||
+ | ); | ||
</ | </ | ||
- | ==== Antioch ==== | + | Similar to arguments, sub command nodes can also be set optional. In the following case, both ''/ |
- | ...lobbest thou thy Holy Hand Grenade of Antioch towards thy foe. | + | |
- | who being naughty in My sight, shall snuff it. | + | |
- | + | ||
- | Aside from the joke this command summons a primed TNT to a specified location or the location of the sender's cursor. | + | |
- | + | ||
- | First create an entry into the CommandDispatcher that takes a literal of antioch with an optional argument of the location to summon the entity at. | + | |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public static void register(CommandDispatcher< | + | dispatcher.register(literal(" |
- | | + | .executes(context -> { |
- | .then(required(" | + | context.getSource().sendFeedback(() -> Text.literal(" |
- | | + | return 1; |
- | .executes(ctx -> antioch(ctx.getSource(), null))); | + | }) |
- | } | + | |
+ | .executes(context | ||
+ | context.getSource().sendFeedback(() -> Text.literal(" | ||
+ | | ||
+ | | ||
+ | ) | ||
+ | ); | ||
</ | </ | ||
+ | ====== Advanced concepts ====== | ||
- | Then the creation and messages behind the joke. | + | Below are links to the articles about more complex concepts used in brigadier. |
- | <code java [enable_line_numbers=" | + | ^ Page ^ Description |
- | public static int antioch(ServerCommandSource source, BlockPos blockPos) throws CommandSyntaxException { | + | | [[command_exceptions |
- | if(blockPos == null) { | + | | [[command_suggestions|Suggestions]] |
- | // For the case of no inputted argument we calculate the cursor position | + | | [[command_redirects|Redirects]] |
- | // This class is used as an example and actually doesn' | + | | [[command_argument_types|Custom Argument Types]] |
- | | + | | [[command_examples|Examples]] | Some example commands | |
- | | + | |
- | final TntEntity tnt = new TntEntity(source.getWorld(), | + | ====== FAQ ====== |
- | tnt.setFuse(3); | + | |
- | + | ||
- | source.getMinecraftServer().getPlayerManager().broadcastChatMessage(new LiteralText(" | + | |
- | source.getMinecraftServer().getPlayerManager().broadcastChatMessage(new LiteralText(" | + | |
- | source.getWorld().spawnEntity(tnt); | + | |
- | return 1; | + | |
- | } | + | |
- | </ | + | |
- | ==== More examples coming soon ==== | + | ===== Why does my code not compile ===== |
- | ===== Custom Argument Types ===== | + | There are several immediate possibilities for why this could occur. |
- | Brigadier | + | * **Catch or throw a CommandSyntaxException: |
+ | * **Issues with generics:** You may have an issue with generics once in a while. If you are registering server command (which is most of the case), make sure you are using '' | ||
+ | * **Check '' | ||
+ | * **'' | ||
- | Warning: Custom arguments require | + | ===== Can I register |
- | For this example we will create | + | Fabric has a '' |
- | First create a class which extends '' | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public class UuidArgumentType implements ArgumentType< | ||
- | </ | ||
- | |||
- | ArgumentType requires you to implement the '' | ||
<code java> | <code java> | ||
- | @Override | + | ClientCommandRegistrationCallback.EVENT.register((dispatcher, |
- | public UUID parse(StringReader reader) throws CommandSyntaxException | + | .executes(context -> { |
+ | context.getSource().sendFeedback(Text.literal(" | ||
+ | return 1; | ||
+ | } | ||
+ | ))); | ||
</ | </ | ||
- | This method is where all of your parsing will occur. Either this method will return the object based on the arguments provided | + | If you need to open a screen |
- | + | ||
- | Next you will store the current position of the cursor, this is so you can substring out only the specific argument. This will always be at the beginning | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | int argBeginning = reader.getCursor(); | + | |
- | if (!reader.canRead()) { | + | |
- | reader.skip(); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Now we grab the entire argument. Depending on your argument type, you may have a different criteria or be similar to some arguments where detecting a '' | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. | + | |
- | reader.skip(); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Then we will ask the StringReader what the current position of the cursor is an substring our argument out of the command line. | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | + | ||
- | Now finally we check if our argument is correct and parse the specific argument to our liking, and throwing an exception if the parsing fails. | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | try { | + | |
- | UUID uuid = UUID.fromString(uuidString); | + | |
- | return uuid; // And we return our type, in this case the parser will consider this argument to have parsed properly and then move on. | + | |
- | } catch (Exception ex) { | + | |
- | // UUIDs can throw an exception when made by a string, so we catch the exception and repackage it into a CommandSyntaxException type. | + | |
- | // Create with context tells Brigadier to supply some context to tell the user where the command failed at. | + | |
- | // Though normal create method could be used. | + | |
- | throw new SimpleCommandExceptionType(new LiteralText(ex.getMessage())).createWithContext(reader); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | The ArgumentType is done, however your client will refuse the parse the argument and throw an error. This is because the server will tell the client what argument type the command node is. And the client will not parse any argument types it does not know how to parse. To fix this we need to register an ArgumentSerializer. | + | |
- | Within your ModInitializer. For more complex argument types, you may need to create your own ArgumentSerializer. | + | |
- | + | ||
- | <code java [enable_line_numbers=" | + | |
- | ArgumentTypes.register(" | + | |
- | // The argument | + | |
- | </ | + | |
- | + | ||
- | And here is the whole ArgumentType: | + | |
- | + | ||
- | <file java UuidArgumentType.java [enable_line_numbers=" | + | |
- | + | ||
- | import com.mojang.brigadier.StringReader; | + | |
- | import com.mojang.brigadier.arguments.ArgumentType; | + | |
- | import com.mojang.brigadier.context.CommandContext; | + | |
- | import com.mojang.brigadier.exceptions.CommandSyntaxException; | + | |
- | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | + | |
- | import net.minecraft.text.LiteralText; | + | |
- | import net.minecraft.util.SystemUtil; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | import java.util.Collection; | + | |
- | import java.util.UUID; | + | |
- | + | ||
- | /** | + | |
- | * Represents an ArgumentType that will return a UUID. | + | |
- | */ | + | |
- | public class UuidArgumentType implements ArgumentType< | + | |
- | public static UuidArgumentType uuid() { | + | |
- | return new UuidArgumentType(); | + | |
- | } | + | |
- | + | ||
- | public static <S> UUID getUuid(String name, CommandContext< | + | |
- | // Note that you should assume the CommandSource wrapped inside of the CommandContext will always be a generic type. | + | |
- | // If you need to access the ServerCommandSource make sure you verify the source is a server command source before casting. | + | |
- | return context.getArgument(name, | + | |
- | } | + | |
- | + | ||
- | private static final Collection< | + | |
- | list.add(" | + | |
- | list.add(" | + | |
- | list.add(" | + | |
- | }); | + | |
- | + | ||
- | @Override | + | |
- | public UUID parse(StringReader reader) throws CommandSyntaxException { | + | |
- | int argBeginning = reader.getCursor(); // The starting position of the cursor is at the beginning of the argument. | + | |
- | if (!reader.canRead()) { | + | |
- | reader.skip(); | + | |
- | } | + | |
- | + | ||
- | // Now we check the contents of the argument till either we hit the end of the command line (When canRead becomes false) | + | |
- | // Otherwise we go till reach reach a space, which signifies the next argument | + | |
- | while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. | + | |
- | reader.skip(); | + | |
- | } | + | |
- | + | ||
- | // Now we substring the specific part we want to see using the starting cursor position and the ends where the next argument starts. | + | |
- | String uuidString = reader.getString().substring(argBeginning, | + | |
- | try { | + | |
- | UUID uuid = UUID.fromString(uuidString); | + | |
- | return uuid; // And we return our type, in this case the parser will consider this argument to have parsed properly and then move on. | + | |
- | } catch (Exception ex) { | + | |
- | // UUIDs can throw an exception when made by a string, so we catch the exception and repackage it into a CommandSyntaxException type. | + | |
- | // Create | + | |
- | // Though normal create method could be used. | + | |
- | throw new SimpleCommandExceptionType(new LiteralText(ex.getMessage())).createWithContext(reader); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public Collection< | + | |
- | return EXAMPLES; | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | ===== FAQ ===== | + | |
- | + | ||
- | === What else can I send feedback to the CommandSource? | + | |
- | + | ||
- | You use the Text classes (LiteralText, | + | |
- | + | ||
- | === Why does my IDE complain saying that a method executed by my command needs to catch or throw a CommandSyntaxException === | + | |
- | + | ||
- | The solution to this is just to make the methods throw a CommandSyntaxException. This is because brigadier handles the exceptions. | + | |
- | + | ||
- | === Can I register commands in runtime? === | + | |
- | You can do this but it is not reccomended. You would get the instance of the CommandManager and add anything you wish to the CommandDispatcher within it. | + | ===== Can I register commands in runtime? ===== |
- | After that you will need to send the command tree to every player again using '' | + | You can do this but it is not recommended. You would get the '' |
- | === Can I unregister commands in runtime? === | + | After that you need to send the command tree to every player again using '' |
- | You can also do this but it is very unstable and could cause unwanted side effects. Lets just say it involves a bunch of Reflection. | + | ===== Can I unregister commands in runtime? ===== |
- | Once again you will need to send the command tree to every player again using '' | + | You can also do this, however it is much less stable than registering commands and could cause unwanted side effects. To keep things simple, you need to use reflection on brigadier and remove the nodes. After this, you need to send the command tree to every player again using '' |
- | === Can I register client side commands? === | + | ===== Can I execute command without typing in game? ===== |
+ | Yes! You can. | ||
- | Well Fabric currently doesn' | ||
- | https:// |
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210