User Tools

Site Tools


tutorial:command_redirects

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
tutorial:command_redirects [2023/02/20 05:58] – [Chainable Commands] solidblocktutorial:command_redirects [2024/04/15 06:46] (current) – [Chainable Commands] solidblock
Line 15: Line 15:
       final LiteralCommandNode<ServerCommandSource> fooNode = dispatcher.register(literal("foo")       final LiteralCommandNode<ServerCommandSource> fooNode = dispatcher.register(literal("foo")
           .executes(context -> {           .executes(context -> {
-            // For versions below 1.19, replace "Text.literal" with "new LiteralText"+            context.getSource().sendFeedback(() -> Text.literal("Called /foo with no arguments"), true);
-            context.getSource().sendMessage(Text.literal("Called /foo with no arguments"));+
  
             return 1;             return 1;
Line 26: Line 25:
 </code> </code>
  
-The ''redirect'' tells brigadier to continue parsing the command at another command node.+The ''redirect'' tells brigadier to continue parsing the command at another command node. That's to say, what's after the "foo2" node is right what's after the "foo" node. 
 + 
 +The redirection target may not always be the root node. Commans can be redirected to sub-nodes, including arguments.
  
 ===== Chainable Commands ====== ===== Chainable Commands ======
Line 38: Line 39:
 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 46: Line 49:
         .then(literal("command")         .then(literal("command")
             .executes(ctx -> {             .executes(ctx -> {
-                ctx.getSource().sendFeedback(Text.literal("Chainable Command"), false);+                ctx.getSource().sendFeedback(() -> Text.literal("Chainable Command"), true);
                 return Command.SINGLE_SUCCESS;                 return Command.SINGLE_SUCCESS;
-})));+            })));
 </code> </code>
  
-The ''redirect'' can also modify the ''CommandSource'' by use of a "redirect modifier" (''SingleRedirectModifier<S>''), which is used as a lambda.+The ''redirect'' can also modify the ''CommandSource'' by use of a "redirect modifier" (''SingleRedirectModifier<S>''), which is usually used as a lambda.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-.redirect(rootNode, context -> { +            .redirect(rootNode, context -> { 
-    // When redirecting, modify the looking direction of the command source. +                // When redirecting, modify the looking direction of the command source. 
-    return ((ServerCommandSource) context.getSource()).withLookingAt(Vec3ArgumentType.getVec3(context, "pos")); +                return context.getSource().withLookingAt(Vec3ArgumentType.getVec3(context, "pos")); 
-})+            })
 </code> </code>
  
 +:!: In the redirect modifier, only arguments after the redirected nodes can be used. Meanwhile, arguments before the redirect modifier cannot be used in subsequent redirections (if any) or the final execution.
tutorial/command_redirects.1676872729.txt.gz · Last modified: 2023/02/20 05:58 by solidblock