tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:commands [2023/11/18 10:37] – [A sub command] solidblock | tutorial:commands [2025/03/20 23:37] (current) – added a missing "be" dfnkt | ||
---|---|---|---|
Line 3: | Line 3: | ||
====== Creating Commands ====== | ====== Creating Commands ====== | ||
- | 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. | + | 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. |
- | + | ||
- | Note: All code written here was written for 1.19.2. For old versions, some versions and mappings may differ. | + | |
===== What is Brigadier? ===== | ===== What is Brigadier? ===== | ||
Line 26: | Line 24: | ||
}; | }; | ||
</ | </ | ||
- | |||
- | In vanilla Minecraft, they are usually used as method references, such as static methods named '' | ||
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 '' | 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 '' | ||
Line 100: | Line 96: | ||
.executes(context -> { | .executes(context -> { | ||
// For versions below 1.19, replace " | // For versions below 1.19, replace " | ||
- | | + | // For versions |
- | | + | |
context.getSource().sendFeedback(() -> Text.literal(" | context.getSource().sendFeedback(() -> Text.literal(" | ||
Line 112: | Line 107: | ||
**Please ensure you import the correct static method.** The method '' | **Please ensure you import the correct static method.** The method '' | ||
- | In the '' | + | In the '' |
+ | |||
+ | If the command fails, instead of calling '' | ||
To execute this command, you must type ''/ | To execute this command, you must type ''/ | ||
Line 163: | Line 160: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
dispatcher.register(literal(" | dispatcher.register(literal(" | ||
- | .requires(source -> source.hasPermissionLevel(4)) | + | .requires(source -> source.hasPermissionLevel(2)) |
.executes(ctx -> { | .executes(ctx -> { | ||
ctx.getSource().sendFeedback(() -> Text.literal(" | ctx.getSource().sendFeedback(() -> Text.literal(" | ||
Line 170: | Line 167: | ||
</ | </ | ||
- | This command will only execute if the source of the command is a level 4 operator at minimum. Otherwise, the command is not registered. Also this has the side effect of not showing this command in tab completion to anyone who is not a level 4 operator. This is also why you cannot tab-complete most commands when you did not enable cheating. | + | This command will only execute if the source of the command is a level 2 operator at minimum, // |
+ | |||
+ | To create commands that only level 4 operators (//not including// command blocks) can execute, use '' | ||
===== Arguments ===== | ===== Arguments ===== | ||
Line 213: | Line 212: | ||
</ | </ | ||
- | Now you can type one or two integers. If you give one integer, that integer will be self-multiplied. If you provide two integers, | + | 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, |
<code java> | <code java> | ||
Line 241: | Line 240: | ||
</ | </ | ||
- | In order to have a sub command, one needs to append the next node to the existing node. This creates the command '' | + | In order to have a sub command, one needs to append the next node to the existing node. |
+ | |||
+ | This creates the command '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 257: | Line 258: | ||
</ | </ | ||
- | Similar to arguments, sub command nodes can also be set optional. In the following | + | Similar to arguments, sub command nodes can also be set optional. In the following |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
dispatcher.register(literal(" | dispatcher.register(literal(" | ||
Line 285: | Line 286: | ||
====== FAQ ====== | ====== FAQ ====== | ||
- | ===== Why does my command | + | ===== Why does my code not compile ===== |
- | There are two immediate possibilities for why this could occur. | + | There are several |
- | ==== Catch or throw a CommandSyntaxException | + | * **Catch or throw a CommandSyntaxException:** '' |
- | + | * **Issues with generics:** You may have an issue with generics | |
- | The solution to this issue is to make the '' | + | * **Check '' |
- | + | * **'' | |
- | ==== Issues with generics | + | |
- | + | ||
- | You may have an issue with generic types once in a while. | + | |
===== Can I register client side commands? ===== | ===== Can I register client side commands? ===== | ||
- | Fabric has a ClientCommandManager that can be used to register client side commands. | + | Fabric has a '' |
- | ===== Dark Arts ===== | + | <code java> |
+ | ClientCommandRegistrationCallback.EVENT.register((dispatcher, | ||
+ | .executes(context -> { | ||
+ | context.getSource().sendFeedback(Text.literal(" | ||
+ | return 1; | ||
+ | } | ||
+ | ))); | ||
+ | </ | ||
- | A few things we don't recommend, but are possible. | + | If you need to open a screen in the client command execution, instead of directly calling '' |
- | ==== Can I register commands in runtime? ==== | + | ===== Can I register commands in runtime? |
You can do this but it is not recommended. You would get the '' | You can do this but it is not recommended. You would get the '' | ||
Line 311: | Line 316: | ||
After that you need to send the command tree to every player again using '' | After that you need to send the command tree to every player again using '' | ||
- | ==== Can I unregister commands in runtime? ==== | + | ===== Can I unregister commands in runtime? |
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 '' | 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 execute command without typing in game? ===== | ||
+ | Yes! You can. Before trying the next code, take note it works on Fabric 0.91.6+1.20.2 and it was not tested in other versions. | ||
+ | |||
+ | Here is the code example | ||
+ | |||
+ | <code java> | ||
+ | private void vanillaCommandByPlayer(World world, BlockPos pos, String command) { | ||
+ | PlayerEntity player = world.getClosestPlayer(pos.getX(), | ||
+ | if (player != null) { | ||
+ | CommandManager commandManager = Objects.requireNonNull(player.getServer()).getCommandManager(); | ||
+ | ServerCommandSource commandSource = player.getServer().getCommandSource(); | ||
+ | commandManager.executeWithPrefix(commandSource, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | First, you need a CommandManager< | ||
+ | Second, you need the ServerCommandSource. | ||
+ | Then, u can call some CommandManager public methods like commandeManader.execute. (.execute need ParseResults< | ||
+ | But commandeManader.executeWithPrefix allow you to use String. You can also put the slash (/). | ||
+ | |||
+ | So... have fun! |
tutorial/commands.1700303850.txt.gz · Last modified: 2023/11/18 10:37 by solidblock