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 [2019/11/16 04:35] liachtutorial:commands [2020/05/07 20:12] – Amend to use new API i509vcb
Line 8: Line 8:
 If you just want to see how to register commands you've come to the right place here. If you just want to see how to register commands you've come to the right place here.
  
-Registering commands is done through ''CommandRegistry'' with the ''register'' method. +Registering commands is done by registering a new listener in the ''CommandRegistrationCallback''
- +The event should be registered in your mod's initializer.
-The ''register'' method specifies two arguments, the dedicated flag and a consumer representing the ''CommandDispatcher''. These methods should be placed in your mod's initializer+
- +
-The dedicated flag if set to true will tell Fabric to only register the command on a ''DedicatedServer'' (if false than the command will register on both the ''InternalServer'' and ''DedicatedServer'').+
  
 +The dedicated parameter if true will tell event listeners that the server commands are being registered on is a ''dedicated server''. If false than the commands will registered on an ''integrated server''.
 Below are a few examples of how the commands can be registered. Below are a few examples of how the commands can be registered.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-CommandRegistry.INSTANCE.register(false, dispatcher -> TutorialCommands.register(dispatcher)); // All commands are registered in a single class that references every command. +// The actual registration of commands can be delegated to another class via a method reference 
-  +CommandRegistrationCallback.EVENT.register(TutorialCommands::register); 
-CommandRegistry.INSTANCE.register(false, dispatcher -> { // You can also just reference every single class also. There is also the alternative of just using CommandRegistry + 
-    TutorialCommand.register(dispatcher); +// Or you can define every command within the event listener 
-    TutorialHelpCommand.register(dispatcher);+CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { 
 +    TutorialCommand.register(dispatcher); // This command will be registered regardless of the server being dedicated or integrated 
 +    if (dedicated) { 
 +        TutorialHelpCommand.register(dispatcher); // This command will only be registered on a dedicated server 
 +    } else { 
 +        IntegratedTutorialHelpCommand.register(dispatcher); // This command will only be registered on an integrated server. 
 +    }
 }); });
   
-CommandRegistry.INSTANCE.register(true, dispatcher -> { // Or directly registering the command to the dispatcher.+CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { // Or directly registering the command to the dispatcher.
  dispatcher.register(LiteralArgumentBuilder.literal("tutorial").executes(ctx -> execute(ctx)));  dispatcher.register(LiteralArgumentBuilder.literal("tutorial").executes(ctx -> execute(ctx)));
 }); });
Line 449: Line 453:
 First create a class which extends ''ArgumentType''. Note that ArgumentType is a generic, so the generic will define what type the ArgumentType will return First create a class which extends ''ArgumentType''. Note that ArgumentType is a generic, so the generic will define what type the ArgumentType will return
  
-<code java>+<code java [enable_line_numbers="true"]>
 public class UUIDArgumentType implements ArgumentType<UUID> { public class UUIDArgumentType implements ArgumentType<UUID> {
 </code> </code>
Line 463: Line 467:
 Next you will store the current position of the cursor, this is so you can substring out only the specific argument. This will always be at the beginning of where your argument appears on the command line. Next you will store the current position of the cursor, this is so you can substring out only the specific argument. This will always be at the beginning of where your argument appears on the command line.
  
-<code java>+<code java [enable_line_numbers="true"]>
 int argBeginning = reader.getCursor(); // The starting position of the cursor is at the beginning of the argument. int argBeginning = reader.getCursor(); // The starting position of the cursor is at the beginning of the argument.
 if (!reader.canRead()) { if (!reader.canRead()) {
Line 472: Line 476:
 Now we grab the entire argument. Depending on your argument type, you may have a different criteria or be similar to some arguments where detecting a ''{'' on the command line will require it to be closed. For a UUID we will just figure out what cursor position the argument ends at. Now we grab the entire argument. Depending on your argument type, you may have a different criteria or be similar to some arguments where detecting a ''{'' on the command line will require it to be closed. For a UUID we will just figure out what cursor position the argument ends at.
  
-<code java>+<code java [enable_line_numbers="true"]>
 while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position.
     reader.skip(); // Tells the StringReader to move it's cursor to the next position.     reader.skip(); // Tells the StringReader to move it's cursor to the next position.
Line 480: Line 484:
 Then we will ask the StringReader what the current position of the cursor is an substring our argument out of the command line. Then we will ask the StringReader what the current position of the cursor is an substring our argument out of the command line.
  
-<code java>String uuidString = reader.getString().substring(argBeginning, reader.getCursor());</code>+<code java [enable_line_numbers="true"]>String uuidString = reader.getString().substring(argBeginning, reader.getCursor());</code>
  
 Now finally we check if our argument is correct and parse the specific argument to our liking, and throwing an exception if the parsing fails. Now finally we check if our argument is correct and parse the specific argument to our liking, and throwing an exception if the parsing fails.
  
-<code java>+<code java [enable_line_numbers="true"]>
 try { try {
     UUID uuid = UUID.fromString(uuidString); // Now our actual logic.     UUID uuid = UUID.fromString(uuidString); // Now our actual logic.
Line 499: Line 503:
 Within your ModInitializer (Not only client or server) you will add this so the client can recognize the argument when the command tree is sent. For more complex argument types, you may need to create your own ArgumentSerializer. Within your ModInitializer (Not only client or server) you will add this so the client can recognize the argument when the command tree is sent. For more complex argument types, you may need to create your own ArgumentSerializer.
  
-<code java>+<code java [enable_line_numbers="true"]>
 ArgumentTypes.register("mymod:uuid", UUIDArgumentType.class, new ConstantArgumentSerializer(UUIDArgumentType::uuid));  ArgumentTypes.register("mymod:uuid", UUIDArgumentType.class, new ConstantArgumentSerializer(UUIDArgumentType::uuid)); 
 // The argument should be what will create the ArgumentType. // The argument should be what will create the ArgumentType.
Line 506: Line 510:
 And here is the whole ArgumentType: And here is the whole ArgumentType:
  
-<file java UUIDArgumentType.java>+<file java UUIDArgumentType.java [enable_line_numbers="true"]>
  
 import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210