Solved Detect water bottle

Discussion in 'Spigot Plugin Development' started by JustCedric, Jun 3, 2016.

  1. Code (Text):
    if (e.getItem().getType() == Material.POTION && p.getItemInHand().getData().getData() == 0)
    It still executes when I drink a potion and a water bottle
     
  2. Code (Text):
    @EventHandler
        public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent e){
            Player player = e.getPlayer();
           
            if(player.getItemInHand().getType().equals(Material.POTION) && player.getItemInHand().getData().getData() == 0){
                player.sendMessage("§bYou can't use this item !");
                e.setCancelled(true);
            }
           
        }
    ?
     
  3. Code (Text):
    if (p.getItemInHand().getType().equals(Material.POTION) && p.getItemInHand().getData().getData() == 0) {
                                p.sendMessage("WATER");
    } else if (p.getItemInHand().getType().equals(Material.POTION && p.getItemInHand().getData().getData() != 0) {
    p.sendMessage("POTION");
    }
    When I drink a potion I get the message WATER & I get that message when I drink water.
     
  4. ... What do you want ?
     
  5. It can be released by command blocks...
     
  6. I want to know if someone drinks a water bottle or a potion like regen potion
     
  7. If checking the item durability isn't working, I'm going to guess you're using 1.9, in which case you need to cast the item stack's ItemMeta to PotionMeta and check if potionMeta.getBasePotionData().getType()==PotionType.WATER

    1.8 and earlier used durability, 1.9 and newer does not
     
  8. It's her , I forgot ...
     
  9. Code (Text):

    ItemStack item = new ItemStack(e.getItem().getType(), e.getItem().getAmount());
                    PotionMeta itemMeta = (PotionMeta) item.getItemMeta();
    if (e.getItem().getType() == Material.POTION && itemMeta.getBasePotionData().getType() != PotionType.WATER) {
                                p.sendMessage("POTION");
    } else if (e.getItem().getType() == Material.POTION && itemMeta.getBasePotionData().getType() == PotionType.WATER) {
    p.sendMessage("WATER");
    }
    This is my code and when I drink a potion I get POTION but when I drink a water bottle I also get POTION..
     
  10. Get rid of ItemStack item, there's no reason to create a new item. You want to check the actual item the player has.
    Code (Java):
    if(e.getItem() != null && e.getItem().getItemMeta() != null && e.getItem().getItemMeta() instanceof PotionMeta){
         PotionType potionType = ((PotionMeta)e.getItem().getItemMeta()).getBasePotionData().getType();
         if(potionType==PotionType.WATER){
              //is water
         }else{
              //is mundane/uncraftable/awkward/thick/invis/poison/etc
         }
    }
     
  11. That's it
     
  12. Thanks it works :)
     
    • Friendly Friendly x 1