Added support for dynamic help text

This commit is contained in:
Norbi Peti 2019-02-16 14:04:41 +01:00
parent d245d21a84
commit a91786cf0d
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 19 additions and 4 deletions

View file

@ -181,10 +181,8 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
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);

View file

@ -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<TP extends Command2Sender> {
@ -28,6 +30,21 @@ public abstract class ICommand2<TP extends Command2Sender> {
return true;
}
/**
* Return null to not add any help text, return an empty array to only print subcommands.<br>
* 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<?, TP> 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)