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:17] – Move old categories below FAQ to be migrated 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 188: Line 212:
  
 **TODO:** Go into more detail on how to use arguments **TODO:** Go into more detail on how to use arguments
- 
-===== Static Imports ===== 
-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 236: Line 235:
 ==== Catch or throw a CommandSyntaxException ==== ==== 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.+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 ==== ==== 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''.+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? ===== ===== 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. 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 ===== ===== Dark Arts =====
Line 254: Line 254:
 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''. 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.+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? ==== ==== Can I unregister commands in runtime? ====
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210