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 revisionBoth sides next revision
tutorial:commands [2019/11/16 17:01] – Line numbers i509vcbtutorial: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)));
 }); });
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210