From 4b23274f8df4eeaf408f4ff40c3e0d6942c36631 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:09:26 +0200 Subject: [PATCH] Add SubCommand.description --- .../src/de/steamwar/command/SWCommand.java | 60 ++++++++----------- .../src/de/steamwar/command/SubCommand.java | 4 +- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 06757f5..f7cc6e0 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -92,16 +92,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - if (method.getName().equals("internalHelp")) { - System.out.println(method + " " + hasHelp); - if (hasHelp) { - return; - } - } else if (!hasHelp) { - commandHelpList.clear(); - hasHelp = true; - } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); + commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>())); }); } for (Method method : methods) { @@ -123,7 +114,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper)); + commandList.add(new SubCommand(this, method, anno.description(), anno.value(), localTypeMapper)); }); this.commandList.sort((o1, o2) -> { @@ -171,15 +162,8 @@ public abstract class SWCommand { } private List methods() { - Class current = getClass(); - List methods = new ArrayList<>(); - int count = 0; - while (current != null && count < 4) { - methods.addAll(Arrays.asList(current.getDeclaredMethods())); - current = current.getSuperclass(); - count++; - } - System.out.println(methods); + List methods = Arrays.asList(getClass().getDeclaredMethods()); + methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); return methods; } @@ -209,28 +193,30 @@ public abstract class SWCommand { commandList.forEach(subCommand -> { StringBuilder st = new StringBuilder(); st.append("§8/§7").append(command.getName()).append(" "); - st.append("§7").append(String.join(" ", subCommand.subCommand)).append(" "); - int i = 1; - while (i < subCommand.parameters.length) { - Parameter parameter = subCommand.parameters[i]; - Name name = parameter.getAnnotation(Name.class); - st.append("§8[§7"); - if (name != null) { - st.append(name.name()); - } else { - st.append(parameter.getName()); - } - st.append("§8]"); - i++; - } + st.append("§7").append(String.join(" ", subCommand.subCommand)); + String cmd = Arrays.stream(subCommand.parameters) + .skip(1) + .map(parameter -> { + Name name = parameter.getAnnotation(Name.class); + if (name != null) { + return name.name(); + } else { + return parameter.getName(); + } + }) + .map(param -> "§8[§e" + param + "§8]") + .collect(Collectors.joining(" ")); + st.append(cmd); if (subCommand.varArgType != null) { st.append("§7..."); } - st.append("§8 - §7Description Here"); + if (!subCommand.description.isEmpty()) { + st.append("§8 - §7").append(subCommand.description); + } help.add(st.toString()); }); } - sender.sendMessage("§7----==== §e" + command.getName() + "§7====----"); + sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); help.forEach(sender::sendMessage); } @@ -240,6 +226,8 @@ public abstract class SWCommand { protected @interface Register { String[] value() default {}; + String description() default ""; + boolean help() default false; @Retention(RetentionPolicy.RUNTIME) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 29642a4..d530b1a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -34,6 +34,7 @@ class SubCommand { private SWCommand swCommand; private Method method; + String description; Parameter[] parameters; String[] subCommand; TypeMapper[] arguments; @@ -41,9 +42,10 @@ class SubCommand { private Function commandSenderFunction; Class varArgType = null; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper) { + SubCommand(SWCommand swCommand, Method method, String description, String[] subCommand, Map> localTypeMapper) { this.swCommand = swCommand; this.method = method; + this.description = description; parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass());