From 96630e13d27c80d7027deb3994aaca0e062d491b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 21:19:30 +0200 Subject: [PATCH] Simplify SWCommand --- .../src/de/steamwar/command/SWCommand.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 0df0148..14bb850 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -79,16 +79,17 @@ public abstract class SWCommand { addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper); }); - add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { + add(Register.class, method, i -> i != 2, true, null, (anno, parameters) -> { if (!anno.help()) return; - if (parameters.length != 2) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); - } + List errors = new ArrayList<>(); if (!parameters[parameters.length - 1].isVarArgs()) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); + errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); } if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); + errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); + } + if (!errors.isEmpty()) { + errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); return; } commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>())); @@ -130,8 +131,8 @@ public abstract class SWCommand { if (compare != 0) { return compare; } else { - return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 0 : 1, - o2.method.getDeclaringClass() == SWCommand.class ? 0 : 1); + return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0, + o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0); } }); } @@ -142,16 +143,18 @@ public abstract class SWCommand { if (anno == null || anno.length == 0) return; Parameter[] parameters = method.getParameters(); + List errors = new ArrayList<>(); if (!parameterTester.test(parameters.length)) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); - return; + errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); } if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); - return; + errors.add("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); } if (returnType != null && method.getReturnType() != returnType) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); + errors.add("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); + } + if (!errors.isEmpty()) { + errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); return; } Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));