Search code examples
javapluginsminecraftbukkit

Bukkit custom item consuming


In my new Bukkit project, I need to create custom edible Items with a plugin, not a mod.

I want to create new kinds of food, like honey or muffins. I know how to implement the crafting recipe, but not how to detect if the item is consumed.

What can I do in order to detect the consumption of an Item and proc it's effects in the same fashion as a normal food from Minecraft?


Solution

  • I don't believe it's possible to make a "Munching" animation with every Item that's not consumable (IE potions and food). Nevertheless, you can use the following events when a player interacts and when a player eats/drinks something, as follows:


    PlayerInteractEvent

    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
        //Event called when a player interacts with something, AKA right click or left click
        Player player = e.getPlayer();
        ItemStack hand = player.getItemInHand();
        if(/**Hand is honey**/) //DOSOMETHING
    }
    

    PlayerItemConsumeEvent

    @EventHandler
    public void onConsume(PlayerItemConsumeEvent e {
        ItemStack consumed = e.getItem();
        Player consumer = e.getPlayer();
        if (/*Consumed is Honey*/) //DoSomething
    }
    

    From there you can use Saturation to add food bars to your player, and manipulate the ItemStack to remove one from it.