001    package org.bukkit.event.inventory;
002    
003    import org.apache.commons.lang.Validate;
004    import org.bukkit.event.Cancellable;
005    import org.bukkit.event.Event;
006    import org.bukkit.event.HandlerList;
007    import org.bukkit.inventory.Inventory;
008    import org.bukkit.inventory.ItemStack;
009    
010    /**
011     * Called when some entity or block (e.g. hopper) tries to move items directly
012     * from one inventory to another.
013     * <p>
014     * When this event is called, the initiator may already have removed the item
015     * from the source inventory and is ready to move it into the destination
016     * inventory.
017     * <p>
018     * If this event is cancelled, the items will be returned to the source
019     * inventory, if needed.
020     * <p>
021     * If this event is not cancelled, the initiator will try to put the ItemStack
022     * into the destination inventory. If this is not possible and the ItemStack
023     * has not been modified, the source inventory slot will be restored to its
024     * former state. Otherwise any additional items will be discarded.
025     */
026    public class InventoryMoveItemEvent extends Event implements Cancellable {
027        private static final HandlerList handlers = new HandlerList();
028        private boolean cancelled;
029        private final Inventory sourceInventory;
030        private final Inventory destinationInventory;
031        private ItemStack itemStack;
032        private final boolean didSourceInitiate;
033    
034        public InventoryMoveItemEvent(final Inventory sourceInventory, final ItemStack itemStack, final Inventory destinationInventory, final boolean didSourceInitiate) {
035            Validate.notNull(itemStack, "ItemStack cannot be null");
036            this.sourceInventory = sourceInventory;
037            this.itemStack = itemStack;
038            this.destinationInventory = destinationInventory;
039            this.didSourceInitiate = didSourceInitiate;
040        }
041    
042        /**
043         * Gets the Inventory that the ItemStack is being taken from
044         *
045         * @return Inventory that the ItemStack is being taken from
046         */
047        public Inventory getSource() {
048            return sourceInventory;
049        }
050    
051        /**
052         * Gets the ItemStack being moved; if modified, the original item will not
053         * be removed from the source inventory.
054         *
055         * @return ItemStack
056         */
057        public ItemStack getItem() {
058            return itemStack.clone();
059        }
060    
061        /**
062         * Sets the ItemStack being moved; if this is different from the original
063         * ItemStack, the original item will not be removed from the source
064         * inventory.
065         *
066         * @param itemStack The ItemStack
067         */
068        public void setItem(ItemStack itemStack) {
069            Validate.notNull(itemStack, "ItemStack cannot be null.  Cancel the event if you want nothing to be transferred.");
070            this.itemStack = itemStack.clone();
071        }
072    
073        /**
074         * Gets the Inventory that the ItemStack is being put into
075         *
076         * @return Inventory that the ItemStack is being put into
077         */
078        public Inventory getDestination() {
079            return destinationInventory;
080        }
081    
082        /**
083         * Gets the Inventory that initiated the transfer. This will always be
084         * either the destination or source Inventory.
085         *
086         * @return Inventory that initiated the transfer
087         */
088        public Inventory getInitiator() {
089            return didSourceInitiate ? sourceInventory : destinationInventory;
090        }
091    
092        public boolean isCancelled() {
093            return cancelled;
094        }
095    
096        public void setCancelled(boolean cancel) {
097            this.cancelled = cancel;
098        }
099    
100        @Override
101        public HandlerList getHandlers() {
102            return handlers;
103        }
104    
105        public static HandlerList getHandlerList() {
106            return handlers;
107        }
108    }