tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:commands [2020/06/14 05:13] – Refactor FAQ section i509vcb | tutorial:commands [2020/11/25 08:07] – Move static imports up and some rewords i509vcb | ||
---|---|---|---|
Line 80: | Line 80: | ||
'' | '' | ||
To execute this command, one must type ''/ | To execute this command, one must type ''/ | ||
+ | |||
+ | ===== Static Imports ===== | ||
+ | Typing out '' | ||
+ | |||
+ | Below is an example of some static imports: | ||
+ | <code java [enable_line_numbers=" | ||
+ | // getString(ctx, | ||
+ | import static com.mojang.brigadier.arguments.StringArgumentType.getString; | ||
+ | // word() | ||
+ | import static com.mojang.brigadier.arguments.StringArgumentType.word; | ||
+ | // literal(" | ||
+ | import static net.minecraft.server.command.CommandManager.literal; | ||
+ | // argument(" | ||
+ | import static net.minecraft.server.command.CommandManager.argument; | ||
+ | // Import everything | ||
+ | import static net.minecraft.server.command.CommandManager.*; | ||
+ | </ | ||
+ | |||
+ | Note: Please be sure you use the '' | ||
+ | |||
+ | Brigadier' | ||
+ | |||
+ | Minecraft' | ||
+ | CommandManager is in the package '' | ||
==== A sub command ==== | ==== A sub command ==== | ||
Line 94: | Line 118: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | dispatcher.register(CommandManager.literal(" | + | dispatcher.register(literal(" |
- | .then(CommandManager.literal(" | + | .then(literal(" |
); | ); | ||
</ | </ | ||
Line 106: | Line 130: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | dispatcher.register(CommandManager.literal(" | + | dispatcher.register(literal(" |
- | .then(CommandManager.literal(" | + | .then(literal(" |
.executes(context -> { | .executes(context -> { | ||
System.out.println(" | System.out.println(" | ||
Line 142: | Line 166: | ||
public void onInitialize() { | public void onInitialize() { | ||
CommandRegistrationCallback.EVENT.register((dispatcher, | CommandRegistrationCallback.EVENT.register((dispatcher, | ||
- | dispatcher.register(CommandManager.literal(" | + | dispatcher.register(literal(" |
System.out.println(" | System.out.println(" | ||
return 1; | return 1; | ||
Line 159: | Line 183: | ||
public void onInitialize() { | public void onInitialize() { | ||
CommandRegistrationCallback.EVENT.register((dispatcher, | CommandRegistrationCallback.EVENT.register((dispatcher, | ||
- | if (dispatcher) { | + | if (dedicated) { |
TestDedicatedCommand.register(dispatcher); | TestDedicatedCommand.register(dispatcher); | ||
} | } | ||
Line 174: | Line 198: | ||
public void onInitialize() { | public void onInitialize() { | ||
CommandRegistrationCallback.EVENT.register((dispatcher, | CommandRegistrationCallback.EVENT.register((dispatcher, | ||
- | if (!dispatcher) { | + | if (!dedicated) { |
TestIntegratedCommand.register(dispatcher); | TestIntegratedCommand.register(dispatcher); | ||
} | } | ||
Line 187: | Line 211: | ||
Minecraft creates some special argument types for it's own use such as the '' | Minecraft creates some special argument types for it's own use such as the '' | ||
- | ===== Static Imports ===== | + | **TODO:** Go into more detail on how to use arguments |
- | You could type out '' | + | |
- | This also works for Minecraft' | + | |
- | + | ||
- | And your imports would look something like this: | + | |
- | <code java [enable_line_numbers=" | + | |
- | // getString(ctx, | + | |
- | import static com.mojang.brigadier.arguments.StringArgumentType.getString; | + | |
- | // word() | + | |
- | import static com.mojang.brigadier.arguments.StringArgumentType.word; | + | |
- | // literal(" | + | |
- | import static net.minecraft.server.command.CommandManager.literal; | + | |
- | // argument(" | + | |
- | import static net.minecraft.server.command.CommandManager.argument; | + | |
- | // Import everything | + | |
- | import static net.minecraft.server.command.CommandManager.*; | + | |
- | </ | + | |
- | + | ||
- | Note: Please be sure you use the '' | + | |
- | + | ||
- | Brigadier' | + | |
- | + | ||
- | Minecraft' | + | |
- | CommandManager is in '' | + | |
====== Advanced concepts ====== | ====== Advanced concepts ====== | ||
Line 225: | Line 226: | ||
**TODO:** Sections are being moved to sub categories and will be added to their respective articles as they are migrated. | **TODO:** Sections are being moved to sub categories and will be added to their respective articles as they are migrated. | ||
+ | |||
+ | ====== FAQ ====== | ||
+ | |||
+ | ===== Why does my command not compile ===== | ||
+ | |||
+ | There are two immediate possibilities for why this could occur. | ||
+ | |||
+ | ==== Catch or throw a CommandSyntaxException ==== | ||
+ | |||
+ | The solution to this issue is to make the run or suggest methods throw a '' | ||
+ | |||
+ | ==== Issues with generics ==== | ||
+ | |||
+ | You may have an issue with generic types once in a while. Verify you are using '' | ||
+ | |||
+ | ===== Can I register client side commands? ===== | ||
+ | |||
+ | Fabric doesn' | ||
+ | There is an open pull request to fabric api which adds this. That will be documented on this page in the future. | ||
+ | |||
+ | ===== Dark Arts ===== | ||
+ | |||
+ | A few things we don't recommend, but are possible. | ||
+ | |||
+ | ==== Can I register commands in runtime? ==== | ||
+ | |||
+ | You can do this but it is not recommended. You would get the '' | ||
+ | |||
+ | After that you need to send the command tree to every player again using '' | ||
+ | |||
+ | ==== 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 '' | ||
+ | |||
+ | ---- | ||
====== Sorry for the mess ====== | ====== Sorry for the mess ====== | ||
- | __Currently this article is being migrated, so things may be a mess. Below is are the parts of the article that are yet to be migrated to the new format.__ | + | **__Currently this article is being migrated, so things may be a mess. Below is are the parts of the article that are yet to be migrated to the new format.__** |
===== Requirements ===== | ===== Requirements ===== | ||
Line 595: | Line 631: | ||
} | } | ||
</ | </ | ||
- | ====== FAQ ====== | ||
- | |||
- | ===== Why does my command not compile ===== | ||
- | |||
- | There are two immediate possibilities for why this could occur. | ||
- | |||
- | ==== Catch or throw a CommandSyntaxException ==== | ||
- | |||
- | The solution to this issue is to make the run or suggest methods throw a CommandSyntaxException. Don't worry, brigadier will handle the exceptions and output the proper error message. | ||
- | |||
- | ==== Issues with generics ==== | ||
- | |||
- | You may have an issue with generic types once in a while. Verify you are using '' | ||
- | |||
- | ===== Can I register client side commands? ===== | ||
- | |||
- | Fabric doesn' | ||
- | |||
- | ===== Dark Arts ===== | ||
- | |||
- | A few things we don't recommend, but are possible. | ||
- | |||
- | ==== Can I register commands in runtime? ==== | ||
- | |||
- | You can do this but it is not recommended. You would get the '' | ||
- | |||
- | After that you need to send the command tree to every player again using '' | ||
- | |||
- | ==== 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 '' |
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210