1

我正在为一个更大的项目编写一个系统,其中不同的类在一个 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
    }
}
4

1 回答 1

0

您正在请求类上的注释,但您请求的内容已放在方法上。要么将注释放在类本身上,要么枚举其方法(getMethods())并分别查询每个类的注释。当然,你做什么取决于你想要什么。

另请注意,注释不会继承到子类。

于 2013-01-13T11:33:50.043 回答