Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
InventoryDragEvent.java
1 package org.bukkit.event.inventory;
2 
3 import java.util.Collections;
4 import java.util.Map;
5 import java.util.Set;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Location;
10 import org.bukkit.entity.Player;
15 import org.bukkit.plugin.Plugin;
17 
18 import com.google.common.collect.ImmutableSet;
19 
20 /**
21  * This event is called when the player drags an item in their cursor across
22  * the inventory. The ItemStack is distributed across the slots the
23  * HumanEntity dragged over. The method of distribution is described by the
24  * DragType returned by {@link #getType()}.
25  * <p>
26  * Canceling this event will result in none of the changes described in
27  * {@link #getNewItems()} being applied to the Inventory.
28  * <p>
29  * Because InventoryDragEvent occurs within a modification of the Inventory,
30  * not all Inventory related methods are safe to use.
31  * <p>
32  * The following should never be invoked by an EventHandler for
33  * InventoryDragEvent using the HumanEntity or InventoryView associated with
34  * this event.
35  * <ul>
36  * <li>{@link HumanEntity#closeInventory()}
37  * <li>{@link HumanEntity#openInventory(Inventory)}
38  * <li>{@link HumanEntity#openWorkbench(Location, boolean)}
39  * <li>{@link HumanEntity#openEnchanting(Location, boolean)}
40  * <li>{@link InventoryView#close()}
41  * </ul>
42  * To invoke one of these methods, schedule a task using
43  * {@link BukkitScheduler#runTask(Plugin, Runnable)}, which will run the task
44  * on the next tick. Also be aware that this is not an exhaustive list, and
45  * other methods could potentially create issues as well.
46  * <p>
47  * Assuming the EntityHuman associated with this event is an instance of a
48  * Player, manipulating the MaxStackSize or contents of an Inventory will
49  * require an Invocation of {@link Player#updateInventory()}.
50  * <p>
51  * Any modifications to slots that are modified by the results of this
52  * InventoryDragEvent will be overwritten. To change these slots, this event
53  * should be cancelled and the changes applied. Alternatively, scheduling a
54  * task using {@link BukkitScheduler#runTask(Plugin, Runnable)}, which would
55  * execute the task on the next tick, would work as well.
56  */
58  private static final HandlerList handlers = new HandlerList();
59  private final DragType type;
60  private final Map<Integer, ItemStack> addedItems;
61  private final Set<Integer> containerSlots;
62  private final ItemStack oldCursor;
63  private ItemStack newCursor;
64 
65  public InventoryDragEvent(InventoryView what, ItemStack newCursor, ItemStack oldCursor, boolean right, Map<Integer, ItemStack> slots) {
66  super(what);
67 
68  Validate.notNull(oldCursor);
69  Validate.notNull(slots);
70 
71  type = right ? DragType.SINGLE : DragType.EVEN;
72  this.newCursor = newCursor;
73  this.oldCursor = oldCursor;
74  this.addedItems = slots;
75  ImmutableSet.Builder<Integer> b = ImmutableSet.builder();
76  for (Integer slot : slots.keySet()) {
77  b.add(what.convertSlot(slot));
78  }
79  this.containerSlots = b.build();
80  }
81 
82  /**
83  * Gets all items to be added to the inventory in this drag.
84  *
85  * @return map from raw slot id to new ItemStack
86  */
87  public Map<Integer, ItemStack> getNewItems() {
88  return Collections.unmodifiableMap(addedItems);
89  }
90 
91  /**
92  * Gets the raw slot ids to be changed in this drag.
93  *
94  * @return list of raw slot ids, suitable for getView().getItem(int)
95  */
96  public Set<Integer> getRawSlots() {
97  return addedItems.keySet();
98  }
99 
100  /**
101  * Gets the slots to be changed in this drag.
102  *
103  * @return list of converted slot ids, suitable for {@link
104  * org.bukkit.inventory.Inventory#getItem(int)}.
105  */
106  public Set<Integer> getInventorySlots() {
107  return containerSlots;
108  }
109 
110  /**
111  * Gets the result cursor after the drag is done. The returned value is
112  * mutable.
113  *
114  * @return the result cursor
115  */
116  public ItemStack getCursor() {
117  return newCursor;
118  }
119 
120  /**
121  * Sets the result cursor after the drag is done.
122  * <p>
123  * Changing this item stack changes the cursor item. Note that changing
124  * the affected "dragged" slots does not change this ItemStack, nor does
125  * changing this ItemStack affect the "dragged" slots.
126  *
127  * @param newCursor the new cursor ItemStack
128  */
129  public void setCursor(ItemStack newCursor) {
130  this.newCursor = newCursor;
131  }
132 
133  /**
134  * Gets an ItemStack representing the cursor prior to any modifications
135  * as a result of this drag.
136  *
137  * @return the original cursor
138  */
140  return oldCursor.clone();
141  }
142 
143  /**
144  * Gets the DragType that describes the behavior of ItemStacks placed
145  * after this InventoryDragEvent.
146  * <p>
147  * The ItemStacks and the raw slots that they're being applied to can be
148  * found using {@link #getNewItems()}.
149  *
150  * @return the DragType of this InventoryDragEvent
151  */
152  public DragType getType() {
153  return type;
154  }
155 
156  @Override
157  public HandlerList getHandlers() {
158  return handlers;
159  }
160 
161  public static HandlerList getHandlerList() {
162  return handlers;
163  }
164 }
final int convertSlot(int rawSlot)