From a91786cf0d8649f4252e37a58ab07006934d7735 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 16 Feb 2019 14:04:41 +0100 Subject: [PATCH] Added support for dynamic help text --- .../java/buttondevteam/lib/chat/Command2.java | 6 ++---- .../java/buttondevteam/lib/chat/ICommand2.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java index 3cd2a35..c5c2f5a 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java @@ -181,10 +181,8 @@ public abstract class Command2 TBMCCoreAPI.SendException("Could not register default handler for command /" + path, e); } for (val method : command.getClass().getMethods()) { - val ann = method.getAnnotation(Subcommand.class); - if (ann != null) { - val cc = command.getClass().getAnnotation(CommandClass.class); - var ht = ann.helpText().length != 0 || cc == null ? ann.helpText() : cc.helpText(); //If cc is null then it's empty array + var ht = command.getHelpText(method); + if (ht != null) { val subcommand = commandChar + path + //Add command path (class name by default) (method.getName().equals("def") ? "" : " " + method.getName().replace('_', ' ').toLowerCase()); //Add method name, unless it's 'def' ht = getHelpText(method, ht, subcommand); diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java index 588c744..7d46821 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java @@ -1,7 +1,9 @@ package buttondevteam.lib.chat; import lombok.Getter; +import lombok.val; +import java.lang.reflect.Method; import java.util.function.Function; public abstract class ICommand2 { @@ -28,6 +30,21 @@ public abstract class ICommand2 { return true; } + /** + * Return null to not add any help text, return an empty array to only print subcommands.
+ * By default, returns null if the Subcommand annotation is not present and returns an empty array if no help text can be found. + * + * @param method The method of the subcommand + * @return The help text, empty array or null + */ + public String[] getHelpText(Method method) { + val ann = method.getAnnotation(Command2.Subcommand.class); + if (ann == null) + return null; + val cc = getClass().getAnnotation(CommandClass.class); + return ann.helpText().length != 0 || cc == null ? ann.helpText() : cc.helpText(); //If cc is null then it's empty array + } + private final String path; @Getter private final Command2 manager; //TIL that if I use a raw type on a variable then none of the type args will work (including what's defined on a method, not on the type)