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/08/22 03:55] – [A very basic command] Change ArgumentBuilders to CommandManager i509vcbtutorial:commands [2019/09/18 00:51] – Add Chainable commands redirect info and open section for custom arguments. i509vcb
Line 211: Line 211:
  
 Redirects do not work in shortened aliases such as ''/mod thing <argument>'' having an alias of ''/thing <argument>'' as Brigadier does not allow forwarding nodes with children. Though you could use alternative methods to reduce the amount of duplicate code for this case. Redirects do not work in shortened aliases such as ''/mod thing <argument>'' having an alias of ''/thing <argument>'' as Brigadier does not allow forwarding nodes with children. Though you could use alternative methods to reduce the amount of duplicate code for this case.
 +
 +==== Redirects (Chainable Commands) ====
 +Commands such as ''/execute as @e[type=player] in the_end run tp ~ ~ ~'' are possible because of redirects. Below is an example of a chainable command:
 +
 +<code java [enable_line_numbers="true"]>
 +LiteralCommandNode<ServerCommandSource> root = 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.
 +    .then(literal("extra")
 +        .then(literal("long")
 +            .redirect(root)) // Return to root for chaining
 +        .then(literal("short")
 +            .redirect(root))) // Return to root for chaining
 +        .then(literal("command")
 +            .executes(ctx -> {
 +                ctx.getSource().sendFeedback(new LiteralText("Chainable Command"), false);
 +                return Command.SINGLE_SUCCESS;
 +})));
 +</code>
 +The redirect can also modify the CommandSource.
 +
 +<code java [enable_line_numbers="true"]>
 +.redirect(rootNode, (commandContext_1x) -> {
 +    return ((ServerCommandSource) commandContext_1x.getSource()).withLookingAt(Vec3ArgumentType.getVec3(commandContext_1x, "pos"));
 +})
 +</code>
  
 ===== ServerCommandSource ===== ===== ServerCommandSource =====
Line 412: Line 438:
     }     }
 </code> </code>
 +
 +===== Custom Arguments (Coming Soon) =====
 +
 +Coming Soon
  
 ===== FAQ ===== ===== FAQ =====
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210