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:51] – [Add Requirements] 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 '' | If the command fails, instead of calling '' | ||
Line 217: | 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 245: | 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 261: | 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 291: | Line 288: | ||
===== Why does my code not compile ===== | ===== Why does my code not compile ===== | ||
- | There are two immediate possibilities for why this could occur. | + | There are several |
- | * **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 '' | + | * **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 '' | ||
+ | * **'' | ||
===== Can I register client side commands? ===== | ===== Can I register client side commands? ===== | ||
Line 309: | Line 308: | ||
</ | </ | ||
- | If you need to open a screen in the client command | + | If you need to open a screen in the client command |
===== Can I register commands in runtime? ===== | ===== Can I register commands in runtime? ===== | ||
Line 321: | Line 320: | ||
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.1700304665.txt.gz · Last modified: 2023/11/18 10:51 by solidblock