Skip to content

Commit 606d671

Browse files
committed
Mixins: Fix redirect() not considering command of target node
Consider the following situation: val node = dispatcher.register(literal("foo") .then(argument("arg", StringArgumentType.string()) .executes { ChatLib.chat("foo with arg") 1 }) .executes { ChatLib.chat("foo without arg") 1 }) dispatcher.register(literal("bar").redirect(node)) With this setup, "/bar test" will work as expected, however "/bar" will fail! Redirect nodes currently do not consider the command of the node they redirect to, only their children. As the comment is this Mixin indicates, this is more of a band-aid fix than a proper one since any redirect modifiers used in the redirect() call will be ignored if there are no arguments following the redirection. This isn't so important for the purposes of the CT-provided API, but may affect commands from other mods. We'll have to keep an eye on this and address it if it becomes a problem. Relevant issue: Mojang/brigadier#46
1 parent 16b1836 commit 606d671

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.chattriggers.ctjs.mixins.commands;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import com.mojang.brigadier.CommandDispatcher;
5+
import com.mojang.brigadier.ParseResults;
6+
import com.mojang.brigadier.StringReader;
7+
import com.mojang.brigadier.context.CommandContextBuilder;
8+
import com.mojang.brigadier.tree.CommandNode;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
13+
14+
@Mixin(value = CommandDispatcher.class, remap = false)
15+
public class CommandDispatcherMixin {
16+
@Inject(
17+
method = "parseNodes",
18+
at = @At(
19+
value = "INVOKE",
20+
target = "Lcom/mojang/brigadier/context/CommandContextBuilder;withCommand(Lcom/mojang/brigadier/Command;)Lcom/mojang/brigadier/context/CommandContextBuilder;",
21+
shift = At.Shift.AFTER
22+
)
23+
)
24+
private <S> void addRedirectCommandToContextIfNecessary(
25+
CommandNode<S> node,
26+
StringReader originalReader,
27+
CommandContextBuilder<S> contextSoFar,
28+
CallbackInfoReturnable<ParseResults<S>> cir,
29+
@Local(ordinal = 1) CommandNode<S> child,
30+
@Local(ordinal = 1) CommandContextBuilder<S> context
31+
) {
32+
// TODO: If there is a redirect modifier, this fix will ignore it.
33+
34+
if (context.getCommand() == null && child.getRedirect() != null && child.getRedirect().getCommand() != null)
35+
context.withCommand(child.getRedirect().getCommand());
36+
}
37+
}

src/main/resources/chattriggers.mixins.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"commands.ClientCommandSourceMixin",
4444
"commands.ClientPlayNetworkHandlerMixin",
4545
"commands.CommandContextAccessor",
46+
"commands.CommandDispatcherMixin",
4647
"commands.CommandNodeAccessor",
4748
"commands.EntitySelectorAccessor",
4849
"sound.SoundAccessor",

0 commit comments

Comments
 (0)