tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
tutorial:commands [2023/11/18 10:59] – [Why does my code not compile] solidblock | tutorial:commands [2023/11/18 11:21] – solidblock | ||
---|---|---|---|
Line 4: | Line 4: | ||
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 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 293: | Line 290: | ||
There are several immediate possibilities for why this could occur. | There are several immediate possibilities for why this could occur. | ||
- | * **Catch or throw a CommandSyntaxException: | + | * **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 '' | * **Check '' | ||
* **'' | * **'' | ||
Line 311: | Line 308: | ||
</ | </ | ||
- | If you need to open a screen in the client command execution, instead of calling '' | + | If you need to open a screen in the client command execution, instead of directly |
===== Can I register commands in runtime? ===== | ===== Can I register commands in runtime? ===== |
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210