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/11/25 08:01] – Specify the class i509vcbtutorial:commands [2020/11/25 08:20] – [Advanced concepts] i509vcb
Line 1: Line 1:
 +Licensing: The code in this article is licensed under the "Creative Commons Zero v1.0 Universal" license. The license grants you the rights to use the code examples shown in this article in your own mods.
 +
 ====== Creating Commands ====== ====== Creating Commands ======
  
Line 80: Line 82:
 ''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 120:
  
 <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 132:
  
 <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 168:
     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 188: Line 214:
  
 **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 221: Line 222:
 | [[tutorials:commands:requirements|Requirements]]               | Only allow users to execute commands in certain scenarios.                      | | [[tutorials:commands:requirements|Requirements]]               | Only allow users to execute commands in certain scenarios.                      |
 | [[tutorials:commands:exceptions  |Exceptions]]                 | Fail execution of a command with a descriptive message and in certain contexts. |                                                                   | [[tutorials:commands:exceptions  |Exceptions]]                 | Fail execution of a command with a descriptive message and in certain contexts. |                                                                  
-| [[tutorials:commands:suggestions |Suggestions]]                | Suggesting input to be sent to the client.                                      |+| [[tutorials:commands:suggestions |Suggestions]]                | Suggesting command input for the client.                                        |
 | [[tutorials:commands:redirects_aliases|Redirects (Aliases)]]   | Allow use of aliases to execute commands.                                       | | [[tutorials:commands:redirects_aliases|Redirects (Aliases)]]   | Allow use of aliases to execute commands.                                       |
 | [[tutorials:commands:redirects_chaining|Redirects (Chaining)]] | Allow commands to have repeating elements and flags.                            | | [[tutorials:commands:redirects_chaining|Redirects (Chaining)]] | Allow commands to have repeating elements and flags.                            |
-| [[tutorials:commands:argument_types|Custom Argument Types]]    | Parse your own arguments and return your own types                            |+| [[tutorials:commands:argument_types|Custom Argument Types]]    | Parse your own arguments into your own objects                                |
  
 **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.
Line 236: Line 237:
 ==== 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 265: Line 267:
  
 **__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.__**
 +
 +Licensing from below onwards is available under the "CC Attribution-Noncommercial-Share Alike 4.0 International" license. This is the current license of the other wiki articles.
  
 ===== Requirements ===== ===== Requirements =====
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210