我正在为一个更大的项目编写一个系统,其中不同的类在一个 CommandHandler 中注册命令。命令是带有要执行的代码的类。
我的问题:CommandHandler 需要一些关于类、名称、权限和用法的信息。
我已经尝试过 @interfaces 但这总是给我null。我应该以其他方式执行此操作还是可以解决此问题?
代码:CommandHandler中的寄存器
public void register(Class<? extends Command> c) {
CommandInfo info = c.getAnnotation(CommandInfo.class);
if (info == null) return;
try {
commands.put(info.pattern(), c.newInstance());
}
catch (Exception e) {
e.printStackTrace();
}
}
@CommandInfo
@Retention(RetentionPolicy.RUNTIME)
public @interface CommandInfo
{
/**
* The actual name of the command. Not really used anywhere.
*/
public String name();
/**
* A regex pattern that allows minor oddities and alternatives to the
* command name.
*/
public String pattern();
/**
* The usage message, i.e. how the command should be used.
*/
public String usage();
/**
* A description of what the command does.
*/
public String desc();
/**
* The permission required to execute this command.
*/
public String permission();
}
还有一个命令:
public class SetPortPoint implements Command
{
@CommandInfo(
name = "setportpoint",
pattern = "setportpoint|spp",
usage = "/maa setportpoint <arena> <wavenumber>",
desc = "set a Port point for a Arena at a given Wave",
permission = "mobarenaaddon.porter.setportpoint"
)
public boolean execute(){
//The Code to do
}
}