Does anyone know how I can check if the item in the player's hand is a specific potion? I tried to make a new potion instance and then convert it to ItemStack, but that didn't seem to work. My code: Code: Action a = event.getAction(); Player p = (Player)event.getPlayer(); ItemStack hand = event.getPlayer().getItemInHand(); if(a==Action.RIGHT_CLICK_AIR || a==Action.RIGHT_CLICK_BLOCK) { if(p.hasPermission("potionblocker.strength")) { Potion str = new Potion(PotionType.STRENGTH); ItemStack strItemStack = str.toItemStack(1); if(hand==strItemStack) { event.setCancelled(true); p.sendMessage(ChatColor.RED + "You cannot drink strenght!"); p.sendMessage("s"); return; } } } Event = PlayerInteractEvent Any help would greatly be appreciated!
Code: if(hand.getType() == Material.POTION && hand.getDurability() == (short) 8201) // 8201 is the durability of the potion Strength. e.setCancelled(true); That should work.
@Awesomebanana2002 Ah, cheers, I'll try. EDIT: Yea, that works, but to be able to block all 3 Strength types I have to get 3 different durabilities?
@DoggyCode™ Don't compare 2 objects using ==, only in special cases. You can cast from ItemMeta to PotionMeta and then call PotionMeta#getCustomEffects() to get the potion effects.
@DoggyCode™, this is what I would do It's going to be quite long though. Code: if (item.getType().equals(Material.POTION)) { if ( item.getDurability() == (short) 8201 || item.getDurability() == 8265 /*Continue */ ){ event.setCancelled(true); player.sendMessage("You cannot use this potion"); } }
@Eos Yea, it's what I thought of, but that's why I wanted to know how to exactly use @megamichiel 's method. Thanks anyways!
@megamichiel what exactly is item supposed to be in this case? Give a example xD Like this?: Code: if(p.hasPermission("potionblocker.strength")) { PotionMeta meta = (PotionMeta) hand.getItemMeta(); if(meta.hasCustomEffect(PotionEffectType.SPEED)) {
@DoggyCode™ Looks fine, the "item" was just referring to the item you want to check the potion type of.