Reputation: 33
I want to test if an item a player is holding has a certain name. If it does then I want it to do the following:
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);
If it doesn't it should return something say like "Wrong Name!"
Upvotes: 0
Views: 1383
Reputation: 18834
Bukkit has a method called getItemInHand()
on a PlayerInventory
that you can use to get the item in hand. To get a PlayerInventory
, you can call getInventory
on a Player
object.
You can then do checks on the returned object, like checking if it is null, and checking its type:
ItemStack item = player.getInventory().getItemInHand();
if(item != null && item.getType() == Material.EGG) {
if(item.hasItemMeta() &&
item.getItemMeta().hasDisplayName() &&
item.getItemMeta().getDisplayName().equals("PartyEgg")) {
....
}
}
Upvotes: 1