From ffef7f0bcb9dff5c79544beb5e2993b0ae493372 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 14:37:15 +0200 Subject: [PATCH] Add SubCommand.parameters Add SWCommand.internalHelp --- .../src/de/steamwar/command/SWCommand.java | 47 +++++++++++++++++++ .../src/de/steamwar/command/SubCommand.java | 3 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ab72fc5..700153c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -34,6 +34,9 @@ import java.util.stream.Collectors; public abstract class SWCommand { + private boolean hasHelp = false; + private final List help = new ArrayList<>(); + private final Command command; private final List commandList = new ArrayList<>(); private final List commandHelpList = new ArrayList<>(); @@ -87,6 +90,14 @@ 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") && method.getDeclaringClass() == SWCommand.class) { + if (hasHelp) { + return; + } + } else if (!hasHelp) { + commandHelpList.clear(); + hasHelp = true; + } commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); }); } @@ -166,6 +177,36 @@ public abstract class SWCommand { SWCommandUtils.commandMap.register("steamwar", this.command); } + @Register + private void internalHelp(CommandSender sender, String... args){ + if (help.isEmpty()) { + 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++; + } + if (subCommand.varArgType != null) { + st.append("§7..."); + } + help.add(st.toString()); + }); + } + sender.sendMessage("§7Help§8: §e" + command.getName()); + help.forEach(sender::sendMessage); + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Repeatable(Register.Registeres.class) @@ -196,4 +237,10 @@ public abstract class SWCommand { boolean local() default false; } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + protected @interface Name { + String name(); + } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 15dad9a..29642a4 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; + Parameter[] parameters; String[] subCommand; TypeMapper[] arguments; private Predicate commandSenderPredicate; @@ -44,7 +45,7 @@ class SubCommand { this.swCommand = swCommand; this.method = method; - Parameter[] parameters = method.getParameters(); + parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); commandSenderFunction = sender -> parameters[0].getType().cast(sender); this.subCommand = subCommand;