Solved Detect When Player Drinks Potion

Discussion in 'Spigot Plugin Development' started by Donkelyn, Feb 6, 2020.

  1. Hey I'm trying to detect when my players drink a potion but it doesn't seem to work correctly. Here's my current code.

    Code (Text):

    @EventHandler
        public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent e) {
           
            Player p = e.getPlayer();
            Random rand = new Random();
           
            if (e.getItem() != null && e.getItem().hasItemMeta()) {
                if (e.getItem().getItemMeta() instanceof PotionMeta) {
               
           
                    final PotionMeta meta = (PotionMeta) e.getItem().getItemMeta();
                    final PotionData data = meta.getBasePotionData();
                    if(data.getType() == PotionType.MUNDANE) {
                        if (p.hasPermission("ragnarok.alchemist")){
                            p.getInventory().addItem(new ItemStack(Material.SAND, 3));
                            p.getInventory().addItem(new ItemStack(Material.LAPIS_LAZULI, 1));
    Any help would be nice thanks
     
  2. An important thing to learn while programming is how to debug code. Add a bukkit.broadcastMessage() with incrementing numbers after each if statement to see how far your code is getting.
     
  3. Check if the material is Material.POTION
     
  4. I just did this and it isn't detecting when the player consumes anything
     
  5. Did you register the listener in the main class?
     
  6. Code (Text):
    public class AlchemistDetection implements Listener {

       
       
        @EventHandler
        public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent e) {
           
           
            Player p = e.getPlayer();
            //Random rand = new Random();
            p.getInventory().addItem(new ItemStack(Material.ICE, 1)); /*
          }
    }
    This doesn't work
     
  7. Holy shit i am a dumbass
     
    • Funny Funny x 1
  8. No worries xD the amount of times I spend ages debugging to realize I didn’t register the class is too embarrassing to state