2013-08-02 53 views
0

所以,我正在使用Bukkit API。基本上,我正在尋找被調用的PlayerInteractEvent,然後我做了一堆事情。但是,當我得到通知我實際上已經踢掉了該塊時,即使它在我的代碼中編譯沒有錯誤,我也沒有收到任何消息。我也沒有從控制檯獲得例外。這裏是我的代碼:爲什麼PlayerInteractEvent不適用於Bukkit API?

@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); 

         } 

        } 

       } 

      } 

     } 

    } 

} 

回答

1

因此,要做到這一點的標準方法是在你創建的

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

} 

形式方法也需要創建一個類,implements Listener

然後一定要告訴插件在哪裏找到您的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 
} 

這應該讓它工作。

+0

我已經這樣做了,所以我不確定還有什麼可能是問題。 – user2646941

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); 
0

你只能有PlayerInteractEvent,否則它不會工作。此外,您不需要提供播放器作爲參數,您可以使用getPlayer()方法查看誰觸發了該事件。

@EventHandler 
public void onPlayerInteract(PlayerInteractEvent e) { 
Player player = e.getPlayer(); 
} 
相關問題