Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
InventoryMoveItemEvent.java
1 package org.bukkit.event.inventory;
2 
3 import org.apache.commons.lang.Validate;
5 import org.bukkit.event.Event;
9 
10 /**
11  * Called when some entity or block (e.g. hopper) tries to move items directly
12  * from one inventory to another.
13  * <p>
14  * When this event is called, the initiator may already have removed the item
15  * from the source inventory and is ready to move it into the destination
16  * inventory.
17  * <p>
18  * If this event is cancelled, the items will be returned to the source
19  * inventory, if needed.
20  * <p>
21  * If this event is not cancelled, the initiator will try to put the ItemStack
22  * into the destination inventory. If this is not possible and the ItemStack
23  * has not been modified, the source inventory slot will be restored to its
24  * former state. Otherwise any additional items will be discarded.
25  */
26 public class InventoryMoveItemEvent extends Event implements Cancellable {
27  private static final HandlerList handlers = new HandlerList();
28  private boolean cancelled;
29  private final Inventory sourceInventory;
30  private final Inventory destinationInventory;
31  private ItemStack itemStack;
32  private final boolean didSourceInitiate;
33 
34  public InventoryMoveItemEvent(final Inventory sourceInventory, final ItemStack itemStack, final Inventory destinationInventory, final boolean didSourceInitiate) {
35  Validate.notNull(itemStack, "ItemStack cannot be null");
36  this.sourceInventory = sourceInventory;
37  this.itemStack = itemStack;
38  this.destinationInventory = destinationInventory;
39  this.didSourceInitiate = didSourceInitiate;
40  }
41 
42  /**
43  * Gets the Inventory that the ItemStack is being taken from
44  *
45  * @return Inventory that the ItemStack is being taken from
46  */
47  public Inventory getSource() {
48  return sourceInventory;
49  }
50 
51  /**
52  * Gets the ItemStack being moved; if modified, the original item will not
53  * be removed from the source inventory.
54  *
55  * @return ItemStack
56  */
57  public ItemStack getItem() {
58  return itemStack.clone();
59  }
60 
61  /**
62  * Sets the ItemStack being moved; if this is different from the original
63  * ItemStack, the original item will not be removed from the source
64  * inventory.
65  *
66  * @param itemStack The ItemStack
67  */
68  public void setItem(ItemStack itemStack) {
69  Validate.notNull(itemStack, "ItemStack cannot be null. Cancel the event if you want nothing to be transferred.");
70  this.itemStack = itemStack.clone();
71  }
72 
73  /**
74  * Gets the Inventory that the ItemStack is being put into
75  *
76  * @return Inventory that the ItemStack is being put into
77  */
79  return destinationInventory;
80  }
81 
82  /**
83  * Gets the Inventory that initiated the transfer. This will always be
84  * either the destination or source Inventory.
85  *
86  * @return Inventory that initiated the transfer
87  */
89  return didSourceInitiate ? sourceInventory : destinationInventory;
90  }
91 
92  public boolean isCancelled() {
93  return cancelled;
94  }
95 
96  public void setCancelled(boolean cancel) {
97  this.cancelled = cancel;
98  }
99 
100  @Override
101  public HandlerList getHandlers() {
102  return handlers;
103  }
104 
105  public static HandlerList getHandlerList() {
106  return handlers;
107  }
108 }