GD Vicious bee
GD Vicious bee

Reputation: 47

Removal of items in minecraft spigot plugin

So I'm trying to make a mushroom stew that when you drink it, it gives you back 6 food, but when I tried to remove the stew via remove(Material.MUSHROOM_STEW); I realised it can remove all the stew you have in your inventory when you drink it. May I know how to remove 1 item only when you drink it? For the code just label the food meter going up as // do stuff

Upvotes: 0

Views: 1432

Answers (1)

borisnliscool
borisnliscool

Reputation: 140

you can do something like this. On the PlayerItemConsumeEvent we check what item the player ate. Then if it's a stew, we cancel the eating event then we give the player 6 extra food levels and remove the item from their inventory.

@EventHandler
public void PlayerItemConsumeEvent(PlayerItemConsumeEvent e) {
    if(e.getItem().equals(new ItemStack(Material.MUSHROOM_STEW))) {
        // player ate stew
        Player player = e.getPlayer();
        e.setCancelled(true);
        player.setFoodLevel(player.getFoodLevel() + 6);
        player.getInventory().getItemInMainHand().setAmount(player.getInventory().getItemInMainHand().getAmount() - 1);
        return;
    }
    // player ate something else than stew
}

Upvotes: 1

Related Questions