Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
InventoryView.java
1 package org.bukkit.inventory;
2 
3 import org.bukkit.GameMode;
6 
7 /**
8  * Represents a view linking two inventories and a single player (whose
9  * inventory may or may not be one of the two).
10  * <p>
11  * Note: If you implement this interface but fail to satisfy the expected
12  * contracts of certain methods, there's no guarantee that the game will work
13  * as it should.
14  */
15 public abstract class InventoryView {
16  public final static int OUTSIDE = -999;
17  /**
18  * Represents various extra properties of certain inventory windows.
19  */
20  public enum Property {
21  /**
22  * The progress of the down-pointing arrow in a brewing inventory.
23  */
25  /**
26  * The progress of the right-pointing arrow in a furnace inventory.
27  */
29  /**
30  * The progress of the flame in a furnace inventory.
31  */
33  /**
34  * How many total ticks the current fuel should last.
35  */
37  /**
38  * In an enchanting inventory, the top button's experience level
39  * value.
40  */
42  /**
43  * In an enchanting inventory, the middle button's experience level
44  * value.
45  */
47  /**
48  * In an enchanting inventory, the bottom button's experience level
49  * value.
50  */
52  int id;
53  InventoryType style;
54  private Property(int id, InventoryType appliesTo) {
55  this.id = id;
56  style = appliesTo;
57  }
58 
59  public InventoryType getType() {
60  return style;
61  }
62 
63  /**
64  *
65  * @deprecated Magic value
66  */
67  @Deprecated
68  public int getId() {
69  return id;
70  }
71  }
72  /**
73  * Get the upper inventory involved in this transaction.
74  *
75  * @return the inventory
76  */
77  public abstract Inventory getTopInventory();
78 
79  /**
80  * Get the lower inventory involved in this transaction.
81  *
82  * @return the inventory
83  */
84  public abstract Inventory getBottomInventory();
85 
86  /**
87  * Get the player viewing.
88  *
89  * @return the player
90  */
91  public abstract HumanEntity getPlayer();
92 
93  /**
94  * Determine the type of inventory involved in the transaction. This
95  * indicates the window style being shown. It will never return PLAYER,
96  * since that is common to all windows.
97  *
98  * @return the inventory type
99  */
100  public abstract InventoryType getType();
101 
102  /**
103  * Sets one item in this inventory view by its raw slot ID.
104  * <p>
105  * Note: If slot ID -999 is chosen, it may be expected that the item is
106  * dropped on the ground. This is not required behaviour, however.
107  *
108  * @param slot The ID as returned by InventoryClickEvent.getRawSlot()
109  * @param item The new item to put in the slot, or null to clear it.
110  */
111  public void setItem(int slot, ItemStack item) {
112  if (slot != OUTSIDE) {
113  if (slot < getTopInventory().getSize()) {
114  getTopInventory().setItem(convertSlot(slot),item);
115  } else {
116  getBottomInventory().setItem(convertSlot(slot),item);
117  }
118  } else {
119  getPlayer().getWorld().dropItemNaturally(getPlayer().getLocation(), item);
120  }
121  }
122 
123  /**
124  * Gets one item in this inventory view by its raw slot ID.
125  *
126  * @param slot The ID as returned by InventoryClickEvent.getRawSlot()
127  * @return The item currently in the slot.
128  */
129  public ItemStack getItem(int slot) {
130  if (slot == OUTSIDE) {
131  return null;
132  }
133  if (slot < getTopInventory().getSize()) {
134  return getTopInventory().getItem(convertSlot(slot));
135  } else {
136  return getBottomInventory().getItem(convertSlot(slot));
137  }
138  }
139 
140  /**
141  * Sets the item on the cursor of one of the viewing players.
142  *
143  * @param item The item to put on the cursor, or null to remove the item
144  * on their cursor.
145  */
146  public final void setCursor(ItemStack item) {
147  getPlayer().setItemOnCursor(item);
148  }
149 
150  /**
151  * Get the item on the cursor of one of the viewing players.
152  *
153  * @return The item on the player's cursor, or null if they aren't holding
154  * one.
155  */
156  public final ItemStack getCursor() {
157  return getPlayer().getItemOnCursor();
158  }
159 
160  /**
161  * Converts a raw slot ID into its local slot ID into whichever of the two
162  * inventories the slot points to.
163  * <p>
164  * If the raw slot refers to the upper inventory, it will be returned
165  * unchanged and thus be suitable for getTopInventory().getItem(); if it
166  * refers to the lower inventory, the output will differ from the input
167  * and be suitable for getBottomInventory().getItem().
168  *
169  * @param rawSlot The raw slot ID.
170  * @return The converted slot ID.
171  */
172  public final int convertSlot(int rawSlot) {
173  int numInTop = getTopInventory().getSize();
174  if (rawSlot < numInTop) {
175  return rawSlot;
176  }
177  int slot = rawSlot - numInTop;
178  if (getPlayer().getGameMode() == GameMode.CREATIVE && getType() == InventoryType.PLAYER) {
179  return slot;
180  }
181  if (getType() == InventoryType.CRAFTING) {
182  if(slot < 4) return 39 - slot;
183  else slot -= 4;
184  }
185  if (slot >= 27) slot -= 27;
186  else slot += 9;
187  return slot;
188  }
189 
190  /**
191  * Closes the inventory view.
192  */
193  public final void close() {
195  }
196 
197  /**
198  * Check the total number of slots in this view, combining the upper and
199  * lower inventories.
200  * <p>
201  * Note though that it's possible for this to be greater than the sum of
202  * the two inventories if for example some slots are not being used.
203  *
204  * @return The total size
205  */
206  public final int countSlots() {
208  }
209 
210  /**
211  * Sets an extra property of this inventory if supported by that
212  * inventory, for example the state of a progress bar.
213  *
214  * @param prop the window property to update
215  * @param value the new value for the window property
216  * @return true if the property was updated successfully, false if the
217  * property is not supported by that inventory
218  */
219  public final boolean setProperty(Property prop, int value) {
220  return getPlayer().setWindowProperty(prop, value);
221  }
222 
223  /**
224  * Get the title of this inventory window.
225  *
226  * @return The title.
227  */
228  public final String getTitle() {
229  return getTopInventory().getTitle();
230  }
231 }
abstract HumanEntity getPlayer()
void setItemOnCursor(ItemStack item)
final boolean setProperty(Property prop, int value)
abstract InventoryType getType()
final int convertSlot(int rawSlot)
void setItem(int slot, ItemStack item)
Item dropItemNaturally(Location location, ItemStack item)
ItemStack getItem(int index)
final void setCursor(ItemStack item)
abstract Inventory getTopInventory()
void setItem(int index, ItemStack item)
boolean setWindowProperty(InventoryView.Property prop, int value)
abstract Inventory getBottomInventory()