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/08/13 15:02] – Add line numbers to code, clarify issue with redirects. Add another Command example i509vcbtutorial:commands [2019/09/18 00:51] – Add Chainable commands redirect info and open section for custom arguments. i509vcb
Line 35: Line 35:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 // The root of the command. This must be a literal argument. // The root of the command. This must be a literal argument.
-dispatcher.register(LiteralArgumentBuilder.literal("foo"+dispatcher.register(CommandManager.literal("foo"
 // Then add an argument named bar that is an integer // Then add an argument named bar that is an integer
-    .then(RequiredArgumentBuilder.argument("bar", integer())+    .then(CommandManager.argument("bar", integer())
  // The command to be executed if the command "foo" is entered with the argument "bar"  // The command to be executed if the command "foo" is entered with the argument "bar"
         .executes(ctx -> {          .executes(ctx -> { 
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