Skip to content

Commit ea64990

Browse files
committed
fabric: use Velocity's workaround for Mojang/brigadier#46
1 parent 4ee1113 commit ea64990

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandRegistrationHandler.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import cloud.commandframework.arguments.StaticArgument;
2929
import cloud.commandframework.internal.CommandRegistrationHandler;
3030
import com.mojang.brigadier.CommandDispatcher;
31+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
3132
import com.mojang.brigadier.tree.CommandNode;
33+
import com.mojang.brigadier.tree.LiteralCommandNode;
3234
import com.mojang.brigadier.tree.RootCommandNode;
3335
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
3436
import net.minecraft.command.CommandSource;
35-
import net.minecraft.server.command.CommandManager;
3637
import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
3738
import net.minecraft.server.command.ServerCommandSource;
3839
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -108,9 +109,43 @@ private void registerCommand(final RootCommandNode<ServerCommandSource> dispatch
108109
dispatcher.addChild(baseNode);
109110

110111
for (final String alias : first.getAlternativeAliases()) {
111-
dispatcher.addChild(CommandManager.literal(alias).redirect(baseNode).build());
112+
dispatcher.addChild(buildRedirect(alias, baseNode));
112113
}
113114
}
114115

116+
/**
117+
* Returns a literal node that redirects its execution to
118+
* the given destination node.
119+
*
120+
* <p>This method is taken from MIT licensed code in the Velocity project, see
121+
* <a href="https://github.com/VelocityPowered/Velocity/blob/b88c573eb11839a95bea1af947b0c59a5956368b/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java#L33">
122+
* Velocity's BrigadierUtils class</a></p>
123+
*
124+
* @param alias the command alias
125+
* @param destination the destination node
126+
* @return the built node
127+
*/
128+
private static <S> LiteralCommandNode<S> buildRedirect(
129+
final @NonNull String alias,
130+
final @NonNull CommandNode<S> destination
131+
) {
132+
// Redirects only work for nodes with children, but break the top argument-less command.
133+
// Manually adding the root command after setting the redirect doesn't fix it.
134+
// (See https://github.com/Mojang/brigadier/issues/46) Manually clone the node instead.
135+
LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder
136+
.<S>literal(alias)
137+
.requires(destination.getRequirement())
138+
.forward(
139+
destination.getRedirect(),
140+
destination.getRedirectModifier(),
141+
destination.isFork()
142+
)
143+
.executes(destination.getCommand());
144+
for (final CommandNode<S> child : destination.getChildren()) {
145+
builder.then(child);
146+
}
147+
return builder.build();
148+
}
149+
115150
}
116151
}

0 commit comments

Comments
 (0)