From ccb63e16139f3c583a54b8c1ffbf14bfc6ecbdbe Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 26 Mar 2021 09:06:21 +0100 Subject: [PATCH] Optimize SWCommand Optimize SubCommand --- .../src/de/steamwar/command/SWCommand.java | 12 ++++----- .../src/de/steamwar/command/SubCommand.java | 27 +++++-------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 8c24bad..d27e575 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -76,7 +76,6 @@ public abstract class SWCommand { } }); - Set commandMethods = new HashSet<>(); for (Method method : getClass().getDeclaredMethods()) { add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { try { @@ -87,6 +86,11 @@ public abstract class SWCommand { throw new SecurityException(e.getMessage(), e); } }); + add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { + commandHelpSet.add(new SubCommand(this, method, anno.value())); + }); + } + for (Method method : getClass().getDeclaredMethods()) { add(Register.class, method, i -> i == 0, true, null, (anno, parameters) -> { for (int i = 1; i < parameters.length; i++) { Parameter parameter = parameters[i]; @@ -104,13 +108,9 @@ public abstract class SWCommand { } if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; } - commandMethods.add(method); - }); - add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { - commandHelpSet.add(new SubCommand(this, method)); + commandSet.add(new SubCommand(this, method, anno.value())); }); } - commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method))); } private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 053aae3..b807c03 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -39,21 +39,13 @@ class SubCommand { private boolean varArgs = false; private Function commandSenderFunction; - public SubCommand(SWCommand swCommand, Method method) { + public SubCommand(SWCommand swCommand, Method method, String[] subCommand) { this.swCommand = swCommand; this.method = method; Parameter[] parameters = method.getParameters(); commandSenderFunction = sender -> parameters[0].getType().cast(sender); - SWCommand.Register register = SWCommandUtils.getAnnotation(method, SWCommand.Register.class); - SWCommand.RegisterHelp registerHelp = SWCommandUtils.getAnnotation(method, SWCommand.RegisterHelp.class); - if (register != null) { - subCommand = register.value(); - } else if (registerHelp != null) { - subCommand = registerHelp.value(); - } else { - throw new SecurityException(); - } + this.subCommand = subCommand; arguments = new TypeMapper[parameters.length - 1]; for (int i = 1; i < parameters.length; i++) { @@ -71,17 +63,7 @@ class SubCommand { for (Enum enumConstant : enumClass.getEnumConstants()) { tabCompletes.add(enumConstant.name().toLowerCase()); } - arguments[i] = new TypeMapper() { - @Override - public Object map(String s) { - return SWCommandUtils.ENUM_MAPPER.apply(enumClass, s); - } - - @Override - public List tabCompletes(String s) { - return tabCompletes; - } - }; + arguments[i] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); continue; } @@ -112,6 +94,9 @@ class SubCommand { } List tabComplete(CommandSender commandSender, String[] args) { + if (!varArgs && args.length < arguments.length - 1) { + return Collections.emptyList(); + } List argsList = Arrays.asList(args); for (String value : subCommand) { String s = argsList.remove(0);