1

所以,我正在使用 Bukkit API。基本上,我正在寻找要调用的 PlayerInteractEvent,然后我会做很多事情。但是,当我收到通知说我实际上踢了该块时,我没有收到任何消息,即使它在我的代码中编译时没有错误。我也没有从控制台获得任何例外。这是我的代码:

@EventHandler(priority=EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event, final Player who, final Action action, 
        final ItemStack item, final Block clickedBlock, final BlockFace clickedFace) {

    if (who != null) {

        if (clickedBlock != null) {

            if ((action == Action.LEFT_CLICK_BLOCK) && (clickedBlock.getType() == Material.ANVIL)) {

                if (item == null) {

                    who.sendMessage(ChatColor.YELLOW + "To repair an item, hold it in your inventory and " + 
                            ChatColor.UNDERLINE + "RIGHT CLICK" + ChatColor.RESET + "" + ChatColor.YELLOW + 
                            " the anvil with the item.");

                    event.setCancelled(true);

                }
                else {

                    Material type = item.getType();
                    short durability = item.getDurability();
                    short maximum = type.getMaxDurability();

                    if (maximum == 0) {

                        who.sendMessage(ChatColor.RED + "You can " + ChatColor.UNDERLINE + "NOT" + 
                                ChatColor.RESET + "" + ChatColor.RED + " repair that item.");

                    }
                    else {

                        short add = (short) Math.round(maximum * 0.03);

                        int result = (maximum - durability) / add;

                        int gems = getAmount(who, 388);

                        if (gems < result) {

                            who.sendMessage(ChatColor.RED + "You do " + ChatColor.UNDERLINE + "NOT" + 
                                    ChatColor.RESET + "" + ChatColor.RED + " have enough Gems to repair "
                                    + "that item.");
                            who.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Gems Needed: " + result);

                        }
                        else {

                            who.sendMessage(ChatColor.YELLOW + "It will cost " + ChatColor.WHITE + 
                                    result + "g " + ChatColor.GREEN + "to repair this item.");
                            who.sendMessage(ChatColor.YELLOW + "Continue repairing? " + ChatColor.GREEN + 
                                    "" + ChatColor.BOLD + "Y" + ChatColor.RESET + "" + ChatColor.WHITE + 
                                    " / " + ChatColor.RESET + "" + ChatColor.RED + "" + ChatColor.BOLD + 
                                    "N");

                            map.put(who, item);

                        }

                    }

                }

            }

        }

    }

}
4

3 回答 3

1

所以这样做的标准方法是创建一个类implements Listener

然后在您内部以以下形式创建方法

@EventHandler
public void nameDontMatter(PlayerInteractEvent event) {
 // do stuff, you can get all that information you passed in from the event

}

此外,您需要确保告诉插件在哪里可以找到您的 PlayerListener,因此通常您要做的就是在onEnable()您放置的方法内:

PlayerListenerClass PL = new PlayerListenerClass();
//instantiate an instance of you player listener 

public void onEnable() {
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(InspiredNationsPL, this);
    //Tell the plugin manager where it can find the player listener
}

那应该让它工作。

于 2013-08-02T22:21:37.803 回答
0

您只能拥有 PlayerInteractEvent 否则它将无法工作。此外,您不需要将播放器作为参数提供,您可以使用 getPlayer() 方法查看谁触发了事件。

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
}
于 2013-09-12T00:10:53.300 回答
0

该方法的唯一参数是PlayerIneractEvent. 您不能在此处设置更多参数。

您设置了比方法本身更多的参数。

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event, Player who, ...) {
    ...
}

改用这个:

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    // do stuff here
}

在该方法中,您可以获取您设置为参数的所有其他内容。例如:

event.getPlayer(); // gets the player
event.getAction(); // gets the action

另请注意,您已在插件主类中注册了您的侦听器。

registerEventHandler(yourEventHandler, this);
于 2013-08-15T14:57:50.013 回答