User Tools

Site Tools


tutorial:command_redirects

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:command_redirects [2022/08/08 02:19] – created (migrated from [[tutorial:commands]]) solidblocktutorial:command_redirects [2023/02/20 05:59] – wrap the code solidblock
Line 1: Line 1:
 ======= Command Redirects ======= ======= Command Redirects =======
  
-Redirects are Brigadier's form of aliases and subcommands.+Redirects are Brigadier's form of aliases and subcommands. The ''redirect'' method takes a command node and an optional modifier of context. The command node can be a specific node after a registration of dispathcer, or just the root node.
  
 ===== Aliases ===== ===== Aliases =====
-Below is how Minecraft makes ''/msg'' have an alias of ''/tell'' and ''/w''+In vanilla Minecraft, ''/tell'' and ''/w'' are redirected to ''/msg'', which you can see in ''MessageCommand''
 + 
 +The example shows how ''/foo2'' redirects to ''/foo''.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { +public class ExampleMod implements ModInitializer { 
-    LiteralCommandNode node = dispatcher +  @Override 
-      .register(literal("msg") +  public void onInitialize() { 
-        .then(argument("targets", EntityArgumentType.players()+    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment-> 
-        .then(argument("message", MessageArgumentType.message()) +      final LiteralCommandNode<ServerCommandSource> fooNode = dispatcher.register(literal("foo") 
-        .executes(context -> {... /* code ommited */ })))); +          .executes(context -> { 
-       +            // For versions below 1.19, replace "Text.literal" with "new LiteralText". 
-    dispatcher.register(literal("tell") +            context.getSource().sendMessage(Text.literal("Called /foo with no arguments")); 
-        .redirect(node)); // Alias 1, redirect to main command + 
-    dispatcher.register(literal("w"+            return 1; 
-        .redirect(node)); // Alias 2, redirect to main command+          })); 
 +      dispatcher.register(literal("foo2").redirect(fooNode)); 
 +    }); 
 +  }
 } }
 </code> </code>
Line 24: Line 29:
  
 ===== Chainable Commands ====== ===== Chainable Commands ======
-Commands such as ''/execute as @e[type=player] in the_end run tp ~ ~ ~'' are also possible because of redirects. Below is an example of a chainable command:+Commands such as ''/execute as @e[type=player] in the_end run tp ~ ~ ~'' are also possible because of redirects. 
 + 
 +Let's first consider the vanilla ''/execute'' command. There is a node after the ''%%literal("execute")%%''. After dispatching subcommands, the dispatcher is redirected to that node (with some modifiers), making you possible to, after completing sub-command arguments, type whatever can be typed directly after ''/execute''. In the sub-command ''run'', the dispatcher is redirected to the root node (''dispatcher.getRoot()''), allowing you to type another complete command after the word "''run''"
 + 
 +Below is an example of a chainable command:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 LiteralCommandNode<ServerCommandSource> rootNode = dispatcher.register(literal("fabric_test")); LiteralCommandNode<ServerCommandSource> rootNode = dispatcher.register(literal("fabric_test"));
 LiteralCommandNode<ServerCommandSource> root1 = dispatcher.register(literal("fabric_test" LiteralCommandNode<ServerCommandSource> root1 = dispatcher.register(literal("fabric_test"
-    // You can register under the same literal more than once, it will just register new parts of the branch as shown below if you register a duplicate branch an error will popup in console warning of conflicting commands but one will still work.+    // You can register under the same literal more than once, it will just register new parts  
 +    // of the branch as shown below if you register a duplicate branch an error will popup in  
 +    // console warning of conflicting commands but one will still work.
     .then(literal("extra")     .then(literal("extra")
         .then(literal("long")         .then(literal("long")
Line 42: Line 53:
 </code> </code>
  
-The ''redirect'' can also modify the ''CommandSource'' by use of a "redirect modifier", which is used as a lambda.+The ''redirect'' can also modify the ''CommandSource'' by use of a "redirect modifier" (''SingleRedirectModifier<S>''), which is used as a lambda.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
tutorial/command_redirects.txt · Last modified: 2024/04/15 06:46 by solidblock