-1

我目前正在使用PlayerCommandPreprocessEvent. 我使用这样的语法:

@EventHandler(priority = EventPriority.HIGHEST)
public void PlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
    String cmd;
    String args;
    if(event.getMessage().indexOf(" ") == -1){
        cmd = event.getMessage().substring(1);
        args = "";
        getLogger().info(String.format("%s used '%s'",event.getPlayer().getDisplayName(), cmd));
    }else{
        cmd = event.getMessage().substring(0, event.getMessage().indexOf(" ")).substring(1);
        args = event.getMessage().substring(event.getMessage().indexOf(" ")+1);
        getLogger().info(String.format("%s used '%s' with args '%s'",event.getPlayer().getDisplayName(), cmd, args));
    }

    switch(cmd){
        case "serverinfo":
            event.getPlayer().sendMessage("This server is running some sort of new plugin that is not finished yet.");
            event.setCancelled(true);
            break;
        case "spawn":
            event.getPlayer().sendMessage("So you wanted to teleport to spawn to save time, right?");
            event.getPlayer().sendMessage("This will teach you, cheater!");
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.POISON,1200,1));
            event.setCancelled(true);
            break;
        default:
            event.setCancelled(false);
            break;
    }
}

我想在 class 中使用单独的方法PlayerCommands。由于这将是服务器上使用的唯一插件,我想捕获所有命令,评估它们是否具有PlayerCommands.cmd_<cmdname>()并调用它。

我知道这可以通过java.lang.reflect但现在我只是不知道如何在 Bukkit 服务器上实现它。

4

1 回答 1

0

我只是用来Class.forName()遍历方法并注册它们。

于 2012-09-08T22:12:41.960 回答