From 7020d6b8f369393cc44b64fc7e5b659731ac0a75 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 26 Mar 2021 08:57:37 +0100 Subject: [PATCH] Optimize SWCommand --- .../src/de/steamwar/command/SWCommand.java | 108 +++++++----------- 1 file changed, 44 insertions(+), 64 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 725f451..eac71c3 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -22,13 +22,11 @@ package de.steamwar.command; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import java.lang.annotation.*; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; +import java.util.function.IntPredicate; import static de.steamwar.command.SWCommandUtils.getAnnotation; @@ -79,81 +77,63 @@ public abstract class SWCommand { Set commandMethods = new HashSet<>(); for (Method method : getClass().getDeclaredMethods()) { - addMapper(method); - addCommand(method, commandMethods); - addHelp(method); + add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, method1, parameters) -> { + try { + method.setAccessible(true); + Object object = method.invoke(this); + SWCommandUtils.addMapper(anno.value(), (TypeMapper) object); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); + } + }); + add(Register.class, method, i -> i == 0, true, null, (anno, method1, parameters) -> { + for (int i = 1; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + Class clazz = parameter.getType(); + if (parameter.isVarArgs() && i == parameters.length - 1) { + clazz = parameter.getType().getComponentType(); + } + Mapper mapper = parameter.getAnnotation(Mapper.class); + if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { + continue; + } + String name = clazz.getTypeName(); + if (mapper != null) { + name = mapper.value(); + } + if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; + } + commandMethods.add(method); + }); + add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, method1, parameters) -> { + commandHelpSet.add(new SubCommand(this, method)); + }); } commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method))); } - private void addCommand(Method method, Set commandMethods) { - Register register = getAnnotation(method, Register.class); - if (register == null) { + private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, SWConsumer consumer) { + T anno = getAnnotation(method, annotation); + if (anno == null) { return; } Parameter[] parameters = method.getParameters(); - if (parameters.length == 0) { + if (parameterTester.test(parameters.length)) { return; } - if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) { + if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { return; } - for (int i = 1; i < parameters.length; i++) { - Parameter parameter = parameters[i]; - Class clazz = parameter.getType(); - if (parameter.isVarArgs() && i == parameters.length - 1) { - clazz = parameter.getType().getComponentType(); - } - Mapper mapper = parameter.getAnnotation(Mapper.class); - if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { - continue; - } - String name = clazz.getTypeName(); - if (mapper != null) { - name = mapper.value(); - } - if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; + if (returnType != null && method.getReturnType() != returnType) { + return; } - commandMethods.add(method); + consumer.consume(anno, method, parameters); } - private void addHelp(Method method) { - RegisterHelp registerHelp = getAnnotation(method, RegisterHelp.class); - if (registerHelp == null) { - return; - } - - Parameter[] parameters = method.getParameters(); - if (parameters.length != 1) { - return; - } - if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) { - return; - } - commandHelpSet.add(new SubCommand(this, method)); - } - - private void addMapper(Method method) { - Mapper mapper = getAnnotation(method, Mapper.class); - if (mapper == null) { - return; - } - - Parameter[] parameters = method.getParameters(); - if (parameters.length != 0) { - return; - } - if (method.getReturnType() != TypeMapper.class) { - return; - } - try { - method.setAccessible(true); - Object object = method.invoke(this); - SWCommandUtils.addMapper(mapper.value(), (TypeMapper) object); - } catch (Exception e) { - throw new SecurityException(e.getMessage(), e); - } + @FunctionalInterface + private interface SWConsumer { + void consume(T anno, Method method, Parameter[] parameters); } @Retention(RetentionPolicy.RUNTIME)