001    package org.bukkit.event.player;
002    
003    import org.bukkit.Material;
004    import org.bukkit.entity.Player;
005    import org.bukkit.event.Cancellable;
006    import org.bukkit.event.HandlerList;
007    import org.bukkit.inventory.ItemStack;
008    
009    /**
010     * This event will fire when a player is finishing consuming an item (food,
011     * potion, milk bucket).
012     * <br>
013     * If the ItemStack is modified the server will use the effects of the new
014     * item and not remove the original one from the player's inventory.
015     * <br>
016     * If the event is cancelled the effect will not be applied and the item will
017     * not be removed from the player's inventory.
018     */
019    public class PlayerItemConsumeEvent extends PlayerEvent implements Cancellable {
020        private static final HandlerList handlers = new HandlerList();
021        private boolean isCancelled = false;
022        private ItemStack item;
023    
024        /**
025         * @param player the player consuming
026         * @param item the ItemStack being consumed
027         */
028        public PlayerItemConsumeEvent(final Player player, final ItemStack item) {
029            super(player);
030    
031            this.item = item;
032        }
033    
034        /**
035         * Gets the item that is being consumed. Modifying the returned item will
036         * have no effect, you must use {@link
037         * #setItem(org.bukkit.inventory.ItemStack)} instead.
038         *
039         * @return an ItemStack for the item being consumed
040         */
041        public ItemStack getItem() {
042            return item.clone();
043        }
044    
045        /**
046         * Set the item being consumed
047         *
048         * @param item the item being consumed
049         */
050        public void setItem(ItemStack item) {
051            if (item == null) {
052                this.item = new ItemStack(Material.AIR);
053            } else {
054                this.item = item;
055            }
056        }
057    
058        public boolean isCancelled() {
059            return this.isCancelled;
060        }
061    
062        public void setCancelled(boolean cancel) {
063            this.isCancelled = cancel;
064        }
065    
066        @Override
067        public HandlerList getHandlers() {
068            return handlers;
069        }
070    
071        public static HandlerList getHandlerList() {
072            return handlers;
073        }
074    }