From 472b6f46d5e21290148c598095cbc55145e9030d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 25 Mar 2021 20:26:44 +0100 Subject: [PATCH] Add help command System to SWCommand --- .../src/de/steamwar/command/SWCommand.java | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index c765932..432dd78 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -22,20 +22,15 @@ 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.Consumer; public abstract class SWCommand { private final Set commandSet = new HashSet<>(); - private Consumer helpMessage = sender -> { - }; + private final Set commandHelpSet = new HashSet<>(); protected SWCommand(String command) { this(command, new String[0]); @@ -50,7 +45,11 @@ public abstract class SWCommand { return false; } } - helpMessage.accept(sender); + for (SubCommand subCommand : commandHelpSet) { + if (subCommand.invoke(sender, args)) { + return false; + } + } return false; } @@ -77,14 +76,14 @@ public abstract class SWCommand { for (Method method : getClass().getDeclaredMethods()) { addMapper(method); addCommand(method, commandMethods); + addHelp(method); } commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method))); } private void addCommand(Method method, Set commandMethods) { - Register register = method.getDeclaredAnnotation(Register.class); - Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class); - if (register == null || methodMapper != null) { + Register register = getAnnotation(method, Register.class); + if (register == null) { return; } @@ -114,10 +113,25 @@ public abstract class SWCommand { commandMethods.add(method); } + 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) { - Register register = method.getDeclaredAnnotation(Register.class); - Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class); - if (register != null || methodMapper == null) { + Mapper mapper = getAnnotation(method, Mapper.class); + if (mapper == null) { return; } @@ -131,19 +145,15 @@ public abstract class SWCommand { try { method.setAccessible(true); Object object = method.invoke(this); - SWCommandUtils.addMapper(methodMapper.value(), (TypeMapper) object); + SWCommandUtils.addMapper(mapper.value(), (TypeMapper) object); } catch (Exception e) { throw new SecurityException(e.getMessage(), e); } } - protected final void setHelpMessage(Consumer helpMessage) { - assert helpMessage != null; - this.helpMessage = helpMessage; - } - - protected final void sendHelpMessage(T sender) { - helpMessage.accept(sender); + private T getAnnotation(Method method, Class annotation) { + if (method.getAnnotations().length != 1) return null; + return method.getAnnotation(annotation); } @Retention(RetentionPolicy.RUNTIME) @@ -152,6 +162,12 @@ public abstract class SWCommand { String[] value() default {}; } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.METHOD}) + protected @interface RegisterHelp { + String[] value() default {}; + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.METHOD}) protected @interface Mapper {