From 45c2a77186132ce45d71e313f37c93ef3d166256 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 10 Apr 2021 14:52:58 +0200 Subject: [PATCH 1/2] Add TypeMapper.map with CommandSender --- .../src/de/steamwar/command/SWCommandUtils.java | 8 ++++---- .../src/de/steamwar/command/SubCommand.java | 6 +++--- .../src/de/steamwar/command/TypeMapper.java | 10 +++++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index da927ad..0d09c7f 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -100,7 +100,7 @@ public class SWCommandUtils { } } - static Object[] generateArgumentArray(TypeMapper[] parameters, String[] args, Class varArgType, String[] subCommand) throws CommandParseException { + static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper[] parameters, String[] args, Class varArgType, String[] subCommand) throws CommandParseException { Object[] arguments = new Object[parameters.length + 1]; int index = 0; while (index < subCommand.length) { @@ -115,7 +115,7 @@ public class SWCommandUtils { arguments[arguments.length - 1] = varArgument; } else { for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { - arguments[i + 1] = parameters[i].map(Arrays.copyOf(args, index), args[index]); + arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]); index++; if (arguments[i + 1] == null) { throw new CommandParseException(); @@ -128,7 +128,7 @@ public class SWCommandUtils { arguments[arguments.length - 1] = varArgument; for (int i = 0; i < length; i++) { - Object value = parameters[parameters.length - 1].map(Arrays.copyOf(args, index), args[index]); + Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]); if (value == null) { throw new CommandParseException(); } @@ -156,7 +156,7 @@ public class SWCommandUtils { public static TypeMapper createMapper(Function mapper, BiFunction> tabCompleter) { return new TypeMapper() { @Override - public T map(String[] previous, String s) { + public T map(CommandSender commandSender, String[] previousArguments, String s) { return mapper.apply(s); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 343cbd0..b7a414b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -88,7 +88,7 @@ class SubCommand { return false; } try { - Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgType, subCommand); + Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, args, varArgType, subCommand); objects[0] = commandSenderFunction.apply(commandSender); method.setAccessible(true); method.invoke(swCommand, objects); @@ -116,7 +116,7 @@ class SubCommand { String s = argsList.remove(0); if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { - if (argument.map(Arrays.copyOf(args, index), s) == null) { + if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { return null; } } catch (Exception e) { @@ -129,7 +129,7 @@ class SubCommand { String s = argsList.remove(0); if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { - if (arguments[arguments.length - 1].map(Arrays.copyOf(args, index), s) == null) { + if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) { return null; } } catch (Exception e) { diff --git a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java index 27b04f7..c0c160c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java +++ b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java @@ -24,7 +24,15 @@ import org.bukkit.command.CommandSender; import java.util.List; public interface TypeMapper { - T map(String[] previousArguments, String s); + default T map(CommandSender commandSender, String[] previousArguments, String s) { + return map(previousArguments, s); + } + + // For backwards compatibility, can be removed later on + @Deprecated(since = "Use the other map Function without calling super!") + default T map(String[] previousArguments, String s) { + throw new SecurityException(); + } List tabCompletes(CommandSender commandSender, String[] previousArguments, String s); } From 2f61d2836bd3256df19da0e15483e5531a68b3b8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 10 Apr 2021 17:33:20 +0200 Subject: [PATCH 2/2] Make SWCommand.register and SWCommand.unregister public --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 72cda65..ae31780 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -186,7 +186,7 @@ public abstract class SWCommand { }); } - protected void unregister() { + public void unregister() { SWCommandUtils.knownCommandMap.remove(command.getName()); for (String alias : command.getAliases()) { SWCommandUtils.knownCommandMap.remove(alias); @@ -194,7 +194,7 @@ public abstract class SWCommand { command.unregister(SWCommandUtils.commandMap); } - protected void register() { + public void register() { SWCommandUtils.commandMap.register("steamwar", this.command); }