Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
InventoryClickEvent.java
1 package org.bukkit.event.inventory;
2 
6 import org.bukkit.entity.Player;
9 import org.bukkit.Location;
12 import org.bukkit.plugin.Plugin;
13 
14 /**
15  * This event is called when a player clicks a slot in an inventory.
16  * <p>
17  * Because InventoryClickEvent occurs within a modification of the Inventory,
18  * not all Inventory related methods are safe to use.
19  * <p>
20  * The following should never be invoked by an EventHandler for
21  * InventoryClickEvent using the HumanEntity or InventoryView associated with
22  * this event:
23  * <ul>
24  * <li>{@link HumanEntity#closeInventory()}
25  * <li>{@link HumanEntity#openInventory(Inventory)}
26  * <li>{@link HumanEntity#openWorkbench(Location, boolean)}
27  * <li>{@link HumanEntity#openEnchanting(Location, boolean)}
28  * <li>{@link InventoryView#close()}
29  * </ul>
30  * To invoke one of these methods, schedule a task using
31  * {@link BukkitScheduler#runTask(Plugin, Runnable)}, which will run the task
32  * on the next tick. Also be aware that this is not an exhaustive list, and
33  * other methods could potentially create issues as well.
34  * <p>
35  * Assuming the EntityHuman associated with this event is an instance of a
36  * Player, manipulating the MaxStackSize or contents of an Inventory will
37  * require an Invocation of {@link Player#updateInventory()}.
38  * <p>
39  * Modifications to slots that are modified by the results of this
40  * InventoryClickEvent can be overwritten. To change these slots, this event
41  * should be cancelled and all desired changes to the inventory applied.
42  * Alternatively, scheduling a task using {@link BukkitScheduler#runTask(
43  * Plugin, Runnable)}, which would execute the task on the next tick, would
44  * work as well.
45  */
47  private static final HandlerList handlers = new HandlerList();
48  private final ClickType click;
49  private final InventoryAction action;
50  private SlotType slot_type;
51  private int whichSlot;
52  private int rawSlot;
53  private ItemStack current = null;
54  private int hotbarKey = -1;
55 
56  @Deprecated
57  public InventoryClickEvent(InventoryView view, SlotType type, int slot, boolean right, boolean shift) {
58  this(view, type, slot, right ? (shift ? ClickType.SHIFT_RIGHT : ClickType.RIGHT) : (shift ? ClickType.SHIFT_LEFT : ClickType.LEFT), InventoryAction.SWAP_WITH_CURSOR);
59  }
60 
61  public InventoryClickEvent(InventoryView view, SlotType type, int slot, ClickType click, InventoryAction action) {
62  super(view);
63  this.slot_type = type;
64  this.rawSlot = slot;
65  this.whichSlot = view.convertSlot(slot);
66  this.click = click;
67  this.action = action;
68  }
69 
70  public InventoryClickEvent(InventoryView view, SlotType type, int slot, ClickType click, InventoryAction action, int key) {
71  this(view, type, slot, click, action);
72  this.hotbarKey = key;
73  }
74 
75  /**
76  * Gets the type of slot that was clicked.
77  *
78  * @return the slot type
79  */
80  public SlotType getSlotType() {
81  return slot_type;
82  }
83 
84  /**
85  * Gets the current ItemStack on the cursor.
86  *
87  * @return the cursor ItemStack
88  */
89  public ItemStack getCursor() {
90  return getView().getCursor();
91  }
92 
93  /**
94  * Gets the ItemStack currently in the clicked slot.
95  *
96  * @return the item in the clicked
97  */
99  if (slot_type == SlotType.OUTSIDE) {
100  return current;
101  }
102  return getView().getItem(rawSlot);
103  }
104 
105  /**
106  * Gets whether or not the ClickType for this event represents a right
107  * click.
108  *
109  * @return true if the ClickType uses the right mouse button.
110  * @see ClickType#isRightClick()
111  */
112  public boolean isRightClick() {
113  return click.isRightClick();
114  }
115 
116  /**
117  * Gets whether or not the ClickType for this event represents a left
118  * click.
119  *
120  * @return true if the ClickType uses the left mouse button.
121  * @see ClickType#isLeftClick()
122  */
123  public boolean isLeftClick() {
124  return click.isLeftClick();
125  }
126 
127  /**
128  * Gets whether the ClickType for this event indicates that the key was
129  * pressed down when the click was made.
130  *
131  * @return true if the ClickType uses Shift or Ctrl.
132  * @see ClickType#isShiftClick()
133  */
134  public boolean isShiftClick() {
135  return click.isShiftClick();
136  }
137 
138  /**
139  * Sets the item on the cursor.
140  *
141  * @param stack the new cursor item
142  * @deprecated This changes the ItemStack in their hand before any
143  * calculations are applied to the Inventory, which has a tendency to
144  * create inconsistencies between the Player and the server, and to
145  * make unexpected changes in the behavior of the clicked Inventory.
146  */
147  @Deprecated
148  public void setCursor(ItemStack stack) {
149  getView().setCursor(stack);
150  }
151 
152  /**
153  * Sets the ItemStack currently in the clicked slot.
154  *
155  * @param stack the item to be placed in the current slot
156  */
157  public void setCurrentItem(ItemStack stack) {
158  if (slot_type == SlotType.OUTSIDE) {
159  current = stack;
160  } else {
161  getView().setItem(rawSlot, stack);
162  }
163  }
164 
165  /**
166  * The slot number that was clicked, ready for passing to
167  * {@link Inventory#getItem(int)}. Note that there may be two slots with
168  * the same slot number, since a view links two different inventories.
169  *
170  * @return The slot number.
171  */
172  public int getSlot() {
173  return whichSlot;
174  }
175 
176  /**
177  * The raw slot number clicked, ready for passing to {@link InventoryView
178  * #getItem(int)} This slot number is unique for the view.
179  *
180  * @return the slot number
181  */
182  public int getRawSlot() {
183  return rawSlot;
184  }
185 
186  /**
187  * If the ClickType is NUMBER_KEY, this method will return the index of
188  * the pressed key (0-8).
189  *
190  * @return the number on the key minus 1 (range 0-8); or -1 if not
191  * a NUMBER_KEY action
192  */
193  public int getHotbarButton() {
194  return hotbarKey;
195  }
196 
197  /**
198  * Gets the InventoryAction that triggered this event.
199  * <p>
200  * This action cannot be changed, and represents what the normal outcome
201  * of the event will be. To change the behavior of this
202  * InventoryClickEvent, changes must be manually applied.
203  *
204  * @return the InventoryAction that triggered this event.
205  */
207  return action;
208  }
209 
210  /**
211  * Gets the ClickType for this event.
212  * <p>
213  * This is insulated against changes to the inventory by other plugins.
214  *
215  * @return the type of inventory click
216  */
217  public ClickType getClick() {
218  return click;
219  }
220 
221  @Override
222  public HandlerList getHandlers() {
223  return handlers;
224  }
225 
226  public static HandlerList getHandlerList() {
227  return handlers;
228  }
229 }
final int convertSlot(int rawSlot)
void setItem(int slot, ItemStack item)
final void setCursor(ItemStack item)