User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

These pages will help you setup a productive development environment and semi-automated releases.

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

tutorial:command_redirects

Command Redirects

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

In vanilla Minecraft, /tell and /w are redirected to /msg, which you can see in MessageCommand.

The example shows how /foo2 redirects to /foo.

  1. public class ExampleMod implements ModInitializer {
  2. @Override
  3. public void onInitialize() {
  4. CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
  5. final LiteralCommandNode<ServerCommandSource> fooNode = dispatcher.register(literal("foo")
  6. .executes(context -> {
  7. // For versions below 1.19, replace "Text.literal" with "new LiteralText".
  8. context.getSource().sendMessage(Text.literal("Called /foo with no arguments"));
  9.  
  10. return 1;
  11. }));
  12. dispatcher.register(literal("foo2").redirect(fooNode));
  13. });
  14. }
  15. }

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.

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 what can be typed directly after /execute. In the sub-command run, the dispatcher is redirected to the rood node (dispatcher.getRoot()), allowing you to type another complete command after the word “run”.

Below is an example of a chainable command:

  1. LiteralCommandNode<ServerCommandSource> rootNode = dispatcher.register(literal("fabric_test"));
  2. LiteralCommandNode<ServerCommandSource> root1 = dispatcher.register(literal("fabric_test")
  3. // 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.
  4. .then(literal("extra")
  5. .then(literal("long")
  6. .redirect(rootNode, this::lengthen)) // Return to root for chaining
  7. .then(literal("short")
  8. .redirect(rootNode, this::shorten))) // Return to root for chaining
  9. .then(literal("command")
  10. .executes(ctx -> {
  11. ctx.getSource().sendFeedback(Text.literal("Chainable Command"), false);
  12. return Command.SINGLE_SUCCESS;
  13. })));

The redirect can also modify the CommandSource by use of a “redirect modifier” (SingleRedirectModifier<S>), which is used as a lambda.

  1. .redirect(rootNode, context -> {
  2. // When redirecting, modify the looking direction of the command source.
  3. return ((ServerCommandSource) context.getSource()).withLookingAt(Vec3ArgumentType.getVec3(context, "pos"));
  4. })
tutorial/command_redirects.txt · Last modified: 2022/08/08 02:54 by solidblock