PlayerConsumeItemEvent not working.

Discussion in 'Plugin Development' started by mrdude123, Nov 22, 2015.

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

    mrdude123

    Hi. I'm trying to make a plugin, and when the player eats a god apple, they're supposed to receive a message. When I eat the gapple, the message isn't being sent to the player. What am I doing wrong here? Thanks.
    Code:
    package me.thecerealkill3r.gapple;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        public void onEnable(){
            System.out.print("Gapple is up and running!");
              Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
       
        public void onEat(PlayerItemConsumeEvent e){
            Player p = e.getPlayer();
            ItemStack Gapple = new ItemStack(Material.GOLDEN_APPLE, 1, (short)1);
    
                if(e.getItem() == Gapple){
                    p.sendMessage("You've consumed a god apple!");
                }
               
            }
           
       
    }
    
     
  2. Offline

    Scimiguy

    Don't create a new ItemStack and compare it, this will fail if you have more than one, and in a lot of other cases too.

    To check, just get the item material and check that

    e.getItem().getType() == Material....... etc..
     
  3. Offline

    mrdude123

    Thank you so much, it's working now.
     
  4. Offline

    Scimiguy

    Edit the title of your thread and mark it as solved then please
     
    Last edited: Nov 22, 2015
  5. Offline

    mrdude123

    What about a notch apple?
     
  6. Offline

    Scimiguy

    For Enchanted Golden Apples, you have to check the durability I believe.

    ItemStack#getDurability()
    A durability of 1 should indicate an Enchanted apple
     
    Last edited: Nov 22, 2015
    Xerox262 likes this.
Thread Status:
Not open for further replies.

Share This Page