|
28 | 28 | import cloud.commandframework.arguments.StaticArgument;
|
29 | 29 | import cloud.commandframework.internal.CommandRegistrationHandler;
|
30 | 30 | import com.mojang.brigadier.CommandDispatcher;
|
| 31 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
31 | 32 | import com.mojang.brigadier.tree.CommandNode;
|
| 33 | +import com.mojang.brigadier.tree.LiteralCommandNode; |
32 | 34 | import com.mojang.brigadier.tree.RootCommandNode;
|
33 | 35 | import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
|
34 | 36 | import net.minecraft.command.CommandSource;
|
35 |
| -import net.minecraft.server.command.CommandManager; |
36 | 37 | import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
|
37 | 38 | import net.minecraft.server.command.ServerCommandSource;
|
38 | 39 | import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
@@ -108,9 +109,43 @@ private void registerCommand(final RootCommandNode<ServerCommandSource> dispatch
|
108 | 109 | dispatcher.addChild(baseNode);
|
109 | 110 |
|
110 | 111 | for (final String alias : first.getAlternativeAliases()) {
|
111 |
| - dispatcher.addChild(CommandManager.literal(alias).redirect(baseNode).build()); |
| 112 | + dispatcher.addChild(buildRedirect(alias, baseNode)); |
112 | 113 | }
|
113 | 114 | }
|
114 | 115 |
|
| 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 | + |
115 | 150 | }
|
116 | 151 | }
|
0 commit comments