2016-01-29 20 views
0

我想測試玩家持有的物品是否具有特定名稱。如果是的話那麼我想它做如下:「名稱錯誤」項目名稱的Java Bukkit API測試

Egg egg = player.getWorld().spawn(player.getEyeLocation(), Egg.class); 
      egg.setVelocity(player.getLocation().getDirection().multiply(1.5)); 
      egg.setShooter(player); 

      player.getWorld().playSound(player.getLocation(), Sound.DIG_WOOL, 15, 15); 

如果它不應該返回的東西說喜歡

回答

1

Bukkit在PlayerInventory上有一個名爲getItemInHand()的方法,您可以使用該方法獲取該項目。要獲得PlayerInventory,您可以撥打getInventory上的Player對象。

然後你可以返回的對象上的檢查,如檢查是否爲空,並檢查它的類型:

ItemStack item = player.getInventory().getItemInHand(); 
if(item != null && item.getType() == Material.EGG) { 
    if(item.hasItemMeta() && 
     item.getItemMeta().hasDisplayName() && 
     item.getItemMeta().getDisplayName().equals("PartyEgg")) { 
     .... 
    } 
}