Added support for dynamic help text
This commit is contained in:
parent
d245d21a84
commit
a91786cf0d
2 changed files with 19 additions and 4 deletions
|
@ -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);
|
TBMCCoreAPI.SendException("Could not register default handler for command /" + path, e);
|
||||||
}
|
}
|
||||||
for (val method : command.getClass().getMethods()) {
|
for (val method : command.getClass().getMethods()) {
|
||||||
val ann = method.getAnnotation(Subcommand.class);
|
var ht = command.getHelpText(method);
|
||||||
if (ann != null) {
|
if (ht != 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
|
|
||||||
val subcommand = commandChar + path + //Add command path (class name by default)
|
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'
|
(method.getName().equals("def") ? "" : " " + method.getName().replace('_', ' ').toLowerCase()); //Add method name, unless it's 'def'
|
||||||
ht = getHelpText(method, ht, subcommand);
|
ht = getHelpText(method, ht, subcommand);
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package buttondevteam.lib.chat;
|
package buttondevteam.lib.chat;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.val;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class ICommand2<TP extends Command2Sender> {
|
public abstract class ICommand2<TP extends Command2Sender> {
|
||||||
|
@ -28,6 +30,21 @@ public abstract class ICommand2<TP extends Command2Sender> {
|
||||||
return true;
|
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;
|
private final String path;
|
||||||
@Getter
|
@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)
|
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)
|
||||||
|
|
Loading…
Reference in a new issue