Potions?

Discussion in 'Plugin Development' started by DoggyCode™, Jul 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    DoggyCode™

    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!
     
  2. Code:
            if(hand.getType() == Material.POTION && hand.getDurability() == (short) 8201) // 8201 is the durability of the potion Strength.
                e.setCancelled(true);
    That should work.
     
  3. Offline

    DoggyCode™

    @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?
     
  4. @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.
     
  5. Offline

    DoggyCode™

  6. Offline

    Eos

    @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");
    
            }
        }
     
  7. Offline

    DoggyCode™

    @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!
     
  8. @DoggyCode™
    PotionMeta meta = (PotionMeta) item.getItemMeta();
    meta.blahBlahBlahWithCamelCase();
     
  9. Offline

    DoggyCode™

    @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)) {
    
     
    Last edited: Jul 25, 2015
  10. @DoggyCode™
    Looks fine, the "item" was just referring to the item you want to check the potion type of.
     
Thread Status:
Not open for further replies.

Share This Page