tutorial:command_redirects
This is an old revision of the document!
Table of Contents
Command Redirects
Redirects are Brigadier's form of aliases and subcommands.
Aliases
Below is how Minecraft makes /msg
have an alias of /tell
and /w
.
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { LiteralCommandNode node = dispatcher .register(literal("msg") .then(argument("targets", EntityArgumentType.players()) .then(argument("message", MessageArgumentType.message()) .executes(context -> {... /* code ommited */ })))); dispatcher.register(literal("tell") .redirect(node)); // Alias 1, redirect to main command dispatcher.register(literal("w") .redirect(node)); // Alias 2, redirect to main command }
The redirect
tells brigadier to continue parsing the command at another command node.
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:
LiteralCommandNode<ServerCommandSource> rootNode = 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(rootNode, this::lengthen)) // Return to root for chaining .then(literal("short") .redirect(rootNode, this::shorten))) // Return to root for chaining .then(literal("command") .executes(ctx -> { ctx.getSource().sendFeedback(Text.literal("Chainable Command"), false); return Command.SINGLE_SUCCESS; })));
The redirect
can also modify the CommandSource
by use of a “redirect modifier”, which is used as a lambda.
.redirect(rootNode, context -> { // When redirecting, modify the looking direction of the command source. return ((ServerCommandSource) context.getSource()).withLookingAt(Vec3ArgumentType.getVec3(context, "pos")); })
tutorial/command_redirects.1659925157.txt.gz · Last modified: 2022/08/08 02:19 by solidblock