From 4191f017f1dd8a7d28d2f9127d1623c17a18f5bb Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Wed, 13 Mar 2019 18:54:23 -0700 Subject: [PATCH] [Forge] Fix sub-commands by registering even less --- .../sk89q/worldedit/forge/CommandWrapper.java | 79 ++----------------- 1 file changed, 5 insertions(+), 74 deletions(-) diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java index 6089040be..daab12824 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java @@ -21,19 +21,14 @@ package com.sk89q.worldedit.forge; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.StringArgumentType; -import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.LiteralCommandNode; import com.sk89q.worldedit.util.command.CommandMapping; -import com.sk89q.worldedit.util.command.Parameter; import net.minecraft.command.CommandSource; import net.minecraft.entity.player.EntityPlayerMP; -import java.util.LinkedList; import java.util.function.Predicate; -import static net.minecraft.command.Commands.argument; import static net.minecraft.command.Commands.literal; public final class CommandWrapper { @@ -41,78 +36,14 @@ public final class CommandWrapper { } public static void register(CommandDispatcher dispatcher, CommandMapping command) { - LiteralArgumentBuilder base = literal(command.getPrimaryAlias()); - LinkedList> parameterStack = new LinkedList<>(); - LinkedList> optionalParameterStack = new LinkedList<>(); - boolean hasFlag = false; - for (Parameter parameter : command.getDescription().getParameters()) { - if (parameter.isValueFlag()) { - if (!hasFlag) { - hasFlag = true; - optionalParameterStack.push(argument("flags", StringArgumentType.string())); - } - } else if (parameter.isOptional()) { - optionalParameterStack.push(argument(parameter.getName(), StringArgumentType.string())); - } else { - parameterStack.push(argument(parameter.getName(), StringArgumentType.string())); - } - } - - ArgumentBuilder argument = buildChildNodes(parameterStack, optionalParameterStack, command); - if (argument != null) { - base.then(argument); - } else { - base.executes(commandFor(command)); - } - LiteralCommandNode registered = - dispatcher.register( - base.requires(requirementsFor(command)) - ); for (String alias : command.getAllAliases()) { - dispatcher.register( - literal(alias).redirect(registered) - ); - } - } - - /** - * Make the appropriate {@code then()} and {@code execute()} calls to emulate required and - * optional parameters, given the argument orders. - * - * @param parameterStack required parameters - * @param optionalParameterStack optional parameters - * @return the node with all calls chained - */ - private static ArgumentBuilder buildChildNodes(LinkedList> parameterStack, - LinkedList> optionalParameterStack, - CommandMapping mapping) { - ArgumentBuilder currentChild = null; - Command command = commandFor(mapping); - while (!optionalParameterStack.isEmpty()) { - ArgumentBuilder next = optionalParameterStack.removeLast(); - if (currentChild != null) { - next.then(currentChild.executes(command)); + LiteralArgumentBuilder base = literal(alias) + .executes(FAKE_COMMAND); + if (command.getDescription().getPermissions().size() > 0) { + base.requires(requirementsFor(command)); } - currentChild = next; + dispatcher.register(base); } - boolean requiredExecute = false; - while (!parameterStack.isEmpty()) { - ArgumentBuilder next = parameterStack.removeLast(); - if (currentChild != null) { - next.then(currentChild); - } - if (!requiredExecute) { - // first required parameter also gets execute - requiredExecute = true; - next.executes(command); - } - currentChild = next; - } - return currentChild; - } - - private static Command commandFor(CommandMapping mapping) { - return FAKE_COMMAND; } public static final Command FAKE_COMMAND = ctx -> {