Bukkit - EventHandler方法出错

时间:2014-07-09 23:02:51

标签: java bukkit eventhandler

出于某些原因,当我使用@EventHandler注释执行某些操作时,我一直收到错误。我正在使用Bukkit 1.7.9。这是我的代码的一部分:

@EventHandler
public void onSignChange(SignChangeEvent event, CommandSender sender, Command cmd, String commandLabel){
    Player player = event.getPlayer();
    if(player.hasPermission("CustomJoinSigns.Create")) {
        if(event.getLine(0).equalsIgnoreCase("Minigame")) {
            player.sendMessage(ChatColor.BLUE + "Please type in the minigame name:");
            player.sendMessage(ChatColor.GOLD + "1)" + ChatColor.RED + "Do " + ChatColor.YELLOW + "/cjs setminigame [Minigame]");
            player.sendMessage(ChatColor.GOLD + "2)" + ChatColor.RED + "Do " + ChatColor.YELLOW + "/cjs cancel");
            if(commandLabel.equalsIgnoreCase("cjs setminigame" + minvar));
            player.sendMessage("Test Successful");
        }
    }
}

这是我的错误日志:

ERROR CustomJoinSigns v1 attempted to register an invalid EventHandler method signature 
"public void me.pvpcraftnetwork.customjoinsigns.Main.onSignChange(org.bukkit.event.block.SignChangeEvent,org.bukkit.command.CommandSender,org.bukkit.command.Command,java.lang.String)"
in class me.pvpcraftnetwork.customjoinsigns.Main

对我做错了什么建议?

1 个答案:

答案 0 :(得分:3)

您的Listener方法中只能有一个参数。它应该是这样的:

@EventHandler
public void onSignChange(SignChangeEvent event){
  Player player = event.getPlayer();
  if(player.hasPermission("CustomJoinSigns.Create")) {
    if(event.getLine(0).equalsIgnoreCase("Minigame")) {
        player.sendMessage(ChatColor.BLUE + "Please type in the minigame name:");
        player.sendMessage(ChatColor.GOLD + "1)" + ChatColor.RED + "Do " + ChatColor.YELLOW + "/cjs setminigame [Minigame]");
        player.sendMessage(ChatColor.GOLD + "2)" + ChatColor.RED + "Do " + ChatColor.YELLOW + "/cjs cancel");
        if(commandLabel.equalsIgnoreCase("cjs setminigame" + minvar));
        player.sendMessage("Test Successful");
    }
  }
}

在命令中使用参数CommandSender sender, Command cmd, String commandLabel ...所以也许您认为注册事件就像是在玩家运行命令时监听?

同样,所有@EventHandler方法只能有一个参数,这是您正在侦听的事件。因此,如果您正在收听PlayerJoinEvent,请使用:

@EventHandler
public void playerJoin(PlayerJoinEvent event){
  //code here
}