From da5fca4078a44568f06f0544844f36984043efce Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 8 Jul 2021 23:34:09 +0200 Subject: [PATCH] CMDoS because Brigadier --- .../steamwar/command/SWCommandBrigadier.java | 42 ++++++++++++------- .../src/de/steamwar/command/SubCommand.java | 2 +- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandBrigadier.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandBrigadier.java index e928f15..5d30862 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandBrigadier.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandBrigadier.java @@ -23,6 +23,7 @@ import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import net.minecraft.server.v1_15_R1.CommandListenerWrapper; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -67,16 +68,15 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { .collect(Collectors.toList()); } }; - unregister(); - register(); - SWCommandUtils.createList(swCommand, commandList, commandHelpList, localTypeMapper, this::createSubCommand); Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> { - register(command, subCommand, command, aliases); - for (String s : aliases) { - register(s, subCommand, command, aliases); + register(this.command.getName(), subCommand); + for (String s : this.command.getAliases()) { + register(s, subCommand); } }); + // unregister(); + // register(); } private SubCommand createSubCommand(Method method, String[] strings) { @@ -96,7 +96,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { Parameter parameter = current.parameters[i + 1]; Class parameterType = parameter.getType(); ArgumentType argumentType = getArgumentType(parameter, parameterType, current); - RequiredArgumentBuilder requiredArgumentBuilder = RequiredArgumentBuilder.argument(parameter.getName(), argumentType); if (argumentBuilder != null) { argumentBuilder.then(requiredArgumentBuilder); @@ -104,9 +103,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { current.argumentNode = requiredArgumentBuilder; } argumentBuilder = requiredArgumentBuilder; - if (i == current.arguments.length - 1) { - argumentBuilder.executes(commandContext -> 1); - } } if (current.varArgType != null) { // TODO: UNSUPORTED @@ -129,14 +125,29 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { CommandNode commandNode = argumentBuilder.build(); argumentBuilder.redirect(commandNode);*/ } + current.argumentNodeEnd = argumentBuilder; }); } - private void register(String name, SubCommand subCommand, String command, String... aliases) { + private com.mojang.brigadier.Command executes(SubCommand current) { + return commandContext -> { + CommandSender commandSender = ((CommandListenerWrapper) commandContext.getSource()).getBukkitSender(); + List stringList = Arrays.stream(commandContext.getInput().split(" ")).collect(Collectors.toList()); + stringList.remove(0); + String[] args = stringList.toArray(new String[0]); + current.invoke(commandSender, args); + return 0; + }; + } + + private void register(String name, SubCommand subCommand) { LiteralArgumentBuilder literalArgumentBuilder = LiteralArgumentBuilder.literal(name); if (subCommand.argumentNode == null) { + literalArgumentBuilder.executes(executes(subCommand)); + SWCommand.dispatcher.register(literalArgumentBuilder); return; } + subCommand.argumentNodeEnd.executes(executes(subCommand)); literalArgumentBuilder.then(subCommand.argumentNode); SWCommand.dispatcher.register(literalArgumentBuilder); } @@ -174,7 +185,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { @Override public void unregister() { - if (command == null) return; SWCommandUtils.knownCommandMap.remove(command.getName()); command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); command.unregister(SWCommandUtils.commandMap); @@ -182,8 +192,13 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { @Override public void register() { - if (command == null) return; SWCommandUtils.commandMap.register("steamwar", this.command); + Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> { + register(command.getName(), subCommand); + for (String s : command.getAliases()) { + register(s, subCommand); + } + }); } private ArgumentType getArgumentType(Parameter parameter, Class parameterType, SubCommand subCommand) { @@ -208,7 +223,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface { else argumentType = DoubleArgumentType.doubleArg(); } else { argumentType = StringArgumentType.string(); - subCommand.normalTabCompleteNeeded = true; } return argumentType; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 2a2c267..4119686 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -35,7 +35,7 @@ import static de.steamwar.command.SWCommandUtils.*; class SubCommand { ArgumentBuilder argumentNode = null; - boolean normalTabCompleteNeeded = false; + ArgumentBuilder argumentNodeEnd = null; SWCommand swCommand; Parameter[] parameters;