From ea64990b8a4184cc2eaa817396400b616816ba5b Mon Sep 17 00:00:00 2001 From: jmp Date: Sat, 6 Feb 2021 19:09:18 -0800 Subject: [PATCH] fabric: use Velocity's workaround for https://github.com/Mojang/brigadier/issues/46 --- .../FabricCommandRegistrationHandler.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandRegistrationHandler.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandRegistrationHandler.java index c98c23cb8..91ae71d9b 100644 --- a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandRegistrationHandler.java +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandRegistrationHandler.java @@ -28,11 +28,12 @@ import cloud.commandframework.arguments.StaticArgument; import cloud.commandframework.internal.CommandRegistrationHandler; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.RootCommandNode; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.minecraft.command.CommandSource; -import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager.RegistrationEnvironment; import net.minecraft.server.command.ServerCommandSource; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -108,9 +109,43 @@ private void registerCommand(final RootCommandNode dispatch dispatcher.addChild(baseNode); for (final String alias : first.getAlternativeAliases()) { - dispatcher.addChild(CommandManager.literal(alias).redirect(baseNode).build()); + dispatcher.addChild(buildRedirect(alias, baseNode)); } } + /** + * Returns a literal node that redirects its execution to + * the given destination node. + * + *

This method is taken from MIT licensed code in the Velocity project, see + * + * Velocity's BrigadierUtils class

+ * + * @param alias the command alias + * @param destination the destination node + * @return the built node + */ + private static LiteralCommandNode buildRedirect( + final @NonNull String alias, + final @NonNull CommandNode destination + ) { + // Redirects only work for nodes with children, but break the top argument-less command. + // Manually adding the root command after setting the redirect doesn't fix it. + // (See https://github.com/Mojang/brigadier/issues/46) Manually clone the node instead. + LiteralArgumentBuilder builder = LiteralArgumentBuilder + .literal(alias) + .requires(destination.getRequirement()) + .forward( + destination.getRedirect(), + destination.getRedirectModifier(), + destination.isFork() + ) + .executes(destination.getCommand()); + for (final CommandNode child : destination.getChildren()) { + builder.then(child); + } + return builder.build(); + } + } }