User Tools

Site Tools


tutorial:commands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:commands [2020/06/14 05:13] – Refactor FAQ section i509vcbtutorial:commands [2020/11/25 08:07] – Move static imports up and some rewords i509vcb
Line 80: Line 80:
 ''CommandManager.literal("foo")'' tells brigadier this command has one node, a **literal** called ''foo''. ''CommandManager.literal("foo")'' tells brigadier this command has one node, a **literal** called ''foo''.
 To execute this command, one must type ''/foo''. If ''/Foo'', ''/FoO'', ''/FOO'', ''/fOO'' or ''/fooo'' is typed instead, the command will not run. To execute this command, one must type ''/foo''. If ''/Foo'', ''/FoO'', ''/FOO'', ''/fOO'' or ''/fooo'' is typed instead, the command will not run.
 +
 +===== Static Imports =====
 +Typing out ''CommandManager.literal("foo")'' every time you want to create a literal may feel redundant. You can clean up your codebase by use of static imports for the arguments. For a literal this would shorten the statement to ''literal("foo")''. This also works for getting the value of an argument. This shortens ''StringArgumentType.getString(ctx, "string")'' to ''getString(ctx, "string")''. This also works for Minecraft's own argument types.
 +
 +Below is an example of some static imports:
 +<code java [enable_line_numbers="true"]>
 +// getString(ctx, "string")
 +import static com.mojang.brigadier.arguments.StringArgumentType.getString;
 +// word()
 +import static com.mojang.brigadier.arguments.StringArgumentType.word;
 + // literal("foo")
 +import static net.minecraft.server.command.CommandManager.literal;
 + // argument("bar", word())
 +import static net.minecraft.server.command.CommandManager.argument;
 +// Import everything
 +import static net.minecraft.server.command.CommandManager.*;
 +</code>
 +
 +Note: Please be sure you use the ''literal'' and ''argument'' from CommandManager or you may have issues with generics when trying to compile.
 +
 +Brigadier's default arguments are at ''com.mojang.brigadier.arguments''
 +
 +Minecraft's arguments are in ''net.minecraft.command.arguments''.
 +CommandManager is in the package ''net.minecraft.server.command''
  
 ==== A sub command ==== ==== A sub command ====
Line 94: Line 118:
  
 <code java [enable_line_numbers="true", highlight_lines_extra="2"]> <code java [enable_line_numbers="true", highlight_lines_extra="2"]>
-dispatcher.register(CommandManager.literal("foo"+dispatcher.register(literal("foo"
-    .then(CommandManager.literal("bar"))+    .then(literal("bar"))
 ); );
 </code> </code>
Line 106: Line 130:
  
 <code java [enable_line_numbers="true", highlight_lines_extra="3,4,5,6,7"]> <code java [enable_line_numbers="true", highlight_lines_extra="3,4,5,6,7"]>
-dispatcher.register(CommandManager.literal("foo"+dispatcher.register(literal("foo"
-    .then(CommandManager.literal("bar")+    .then(literal("bar")
         .executes(context -> {         .executes(context -> {
             System.out.println("Called foo with bar");             System.out.println("Called foo with bar");
Line 142: Line 166:
     public void onInitialize() {     public void onInitialize() {
         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
-            dispatcher.register(CommandManager.literal("foo").executes(context -> {+            dispatcher.register(literal("foo").executes(context -> {
                 System.out.println("foo");                 System.out.println("foo");
                 return 1;                 return 1;
Line 159: Line 183:
     public void onInitialize() {     public void onInitialize() {
         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
-            if (dispatcher) {+            if (dedicated) {
                 TestDedicatedCommand.register(dispatcher);                 TestDedicatedCommand.register(dispatcher);
             }             }
Line 174: Line 198:
     public void onInitialize() {     public void onInitialize() {
         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {         CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
-            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 ''EntityArgumentType'' which represents the in-game entity selectors ''@a, @r, @p, @e[type=!player, limit=1, distance=..2]'', or an ''NbtTagArgumentType'' that parses stringified nbt (snbt) and verifies that the input is the correct syntax. Minecraft creates some special argument types for it's own use such as the ''EntityArgumentType'' which represents the in-game entity selectors ''@a, @r, @p, @e[type=!player, limit=1, distance=..2]'', or an ''NbtTagArgumentType'' that parses stringified nbt (snbt) and verifies that the input is the correct syntax.
  
-===== Static Imports ===== +**TODO:** Go into more detail on how to use arguments
-You could type out ''CommandManager.literal("foo")'' every time you want to create a literal. This works, but you can statically import the arguments and shorten the statement to ''literal("foo")''. This also works for getting the value of an argument. This shortens ''StringArgumentType.getString(ctx, "string")'' to ''getString(ctx, "string")''+
-This also works for Minecraft's own argument types. +
- +
-And your imports would look something like this: +
-<code java [enable_line_numbers="true"]> +
-// getString(ctx, "string"+
-import static com.mojang.brigadier.arguments.StringArgumentType.getString; +
-// word() +
-import static com.mojang.brigadier.arguments.StringArgumentType.word; +
- // literal("foo"+
-import static net.minecraft.server.command.CommandManager.literal; +
- // argument("bar", word()) +
-import static net.minecraft.server.command.CommandManager.argument; +
-// Import everything +
-import static net.minecraft.server.command.CommandManager.*+
-</code> +
- +
-Note: Please be sure you use the ''literal'' and ''argument'' from CommandManager or you may have issues with generics when trying to compile. +
- +
-Brigadier's default arguments are at ''com.mojang.brigadier.arguments'' +
- +
-Minecraft's arguments are in ''net.minecraft.command.arguments''+
-CommandManager is in ''net.minecraft.server.command''+
  
 ====== 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 ''CommandSyntaxException''. Brigadier will handle the checked exceptions and forward the proper error message in game for you.
 +
 +==== Issues with generics ====
 +
 +You may have an issue with generic types once in a while. Verify you are using ''CommandManager.literal(...)'' or ''CommandManager.argument(...)'' instead ''LiteralArgumentBuilder'' or ''RequiredArgumentBuilder'' in your static imports.
 +
 +===== Can I register client side commands? =====
 +
 +Fabric doesn't currently support client side commands. There is a [[https://github.com/CottonMC/ClientCommands|third-party mod]] by the Cotton team that adds this functionality.
 +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 ''CommandManager'' from the server and add anything commands you wish to it's ''CommandDispatcher''.
 +
 +After that you need to send the command tree to every player again using ''CommandManager.sendCommandTree(ServerPlayerEntity)''. This is required because the client locally caches the command tree it receives during login (or when operator packets are sent) for local completions rich error messages.
 +
 +==== 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 ''sendCommandTree(ServerPlayerEntity)''. If you don't send the updated command tree, the client may think a command still exists, even though the server will fail execution.
 +
 +----
  
 ====== 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:
 } }
 </file> </file>
-====== 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 ''CommandManager.literal(...)'' or ''CommandManager.argument(...)'' instead ''LiteralArgumentBuilder'' or ''RequiredArgumentBuilder''. 
- 
-===== Can I register client side commands? ===== 
- 
-Fabric doesn't currently support client side commands. There is a [[https://github.com/CottonMC/ClientCommands|third-party mod]] by the Cotton team that adds this functionality. 
- 
-===== 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 ''CommandManager'' from the server and add anything commands you wish to it's ''CommandDispatcher''. 
- 
-After that you need to send the command tree to every player again using ''sendCommandTree(ServerPlayerEntity)''. This is required because the client locally caches the command tree it receives during login (or when operator packets are sent) for local completions rich error messages. 
- 
-==== 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 ''sendCommandTree(ServerPlayerEntity)''. If you don't send the updated command tree, the client may think a command still exists, even though the server will fail execution. 
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210