1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
package org.bukkit.craftbukkit.inventory;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import net.minecraft.server.ContainerAnvilInventory;
import net.minecraft.server.ContainerEnchantTableInventory;
import net.minecraft.server.IHopper;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryCrafting;
import net.minecraft.server.InventoryEnderChest;
import net.minecraft.server.InventoryMerchant;
import net.minecraft.server.PlayerInventory;
import net.minecraft.server.TileEntityBeacon;
import net.minecraft.server.TileEntityBrewingStand;
import net.minecraft.server.TileEntityDispenser;
import net.minecraft.server.TileEntityDropper;
import net.minecraft.server.TileEntityFurnace;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;
public class CraftInventory implements Inventory {
protected final IInventory inventory;
public CraftInventory(IInventory inventory) {
this.inventory = inventory;
}
public IInventory getInventory() {
return inventory;
}
public int getSize() {
return getInventory().getSize();
}
public String getName() {
return getInventory().getInventoryName();
}
public ItemStack getItem(int index) {
net.minecraft.server.ItemStack item = getInventory().getItem(index);
return item == null ? null : CraftItemStack.asCraftMirror(item);
}
public ItemStack[] getContents() {
ItemStack[] items = new ItemStack[getSize()];
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
int size = Math.min(items.length, mcItems.length);
for (int i = 0; i < size; i++) {
items[i] = mcItems[i] == null ? null : CraftItemStack.asCraftMirror(mcItems[i]);
}
return items;
}
public void setContents(ItemStack[] items) {
if (getInventory().getContents().length < items.length) {
throw new IllegalArgumentException("Invalid inventory size; expected " + getInventory().getContents().length + " or less");
}
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
for (int i = 0; i < mcItems.length; i++) {
if (i >= items.length) {
mcItems[i] = null;
} else {
mcItems[i] = CraftItemStack.asNMSCopy(items[i]);
}
}
}
public void setItem(int index, ItemStack item) {
getInventory().setItem(index, ((item == null || item.getTypeId() == 0) ? null : CraftItemStack.asNMSCopy(item)));
}
public boolean contains(int materialId) {
for (ItemStack item : getContents()) {
if (item != null && item.getTypeId() == materialId) {
return true;
}
}
return false;
}
public boolean contains(Material material) {
Validate.notNull(material, "Material cannot be null");
return contains(material.getId());
}
public boolean contains(ItemStack item) {
if (item == null) {
return false;
}
for (ItemStack i : getContents()) {
if (item.equals(i)) {
return true;
}
}
return false;
}
public boolean contains(int materialId, int amount) {
if (amount <= 0) {
return true;
}
for (ItemStack item : getContents()) {
if (item != null && item.getTypeId() == materialId) {
if ((amount -= item.getAmount()) <= 0) {
return true;
}
}
}
return false;
}
public boolean contains(Material material, int amount) {
Validate.notNull(material, "Material cannot be null");
return contains(material.getId(), amount);
}
public boolean contains(ItemStack item, int amount) {
if (item == null) {
return false;
}
if (amount <= 0) {
return true;
}
for (ItemStack i : getContents()) {
if (item.equals(i) && --amount <= 0) {
return true;
}
}
return false;
}
public boolean containsAtLeast(ItemStack item, int amount) {
if (item == null) {
return false;
}
if (amount <= 0) {
return true;
}
for (ItemStack i : getContents()) {
if (item.isSimilar(i) && (amount -= i.getAmount()) <= 0) {
return true;
}
}
return false;
}
public HashMap<Integer, ItemStack> all(int materialId) {
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
ItemStack item = inventory[i];
if (item != null && item.getTypeId() == materialId) {
slots.put(i, item);
}
}
return slots;
}
public HashMap<Integer, ItemStack> all(Material material) {
Validate.notNull(material, "Material cannot be null");
return all(material.getId());
}
public HashMap<Integer, ItemStack> all(ItemStack item) {
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
if (item != null) {
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
if (item.equals(inventory[i])) {
slots.put(i, inventory[i]);
}
}
}
return slots;
}
public int first(int materialId) {
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
ItemStack item = inventory[i];
if (item != null && item.getTypeId() == materialId) {
return i;
}
}
return -1;
}
public int first(Material material) {
Validate.notNull(material, "Material cannot be null");
return first(material.getId());
}
public int first(ItemStack item) {
return first(item, true);
}
private int first(ItemStack item, boolean withAmount) {
if (item == null) {
return -1;
}
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
if (inventory[i] == null) continue;
if (withAmount ? item.equals(inventory[i]) : item.isSimilar(inventory[i])) {
return i;
}
}
return -1;
}
public int firstEmpty() {
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
if (inventory[i] == null) {
return i;
}
}
return -1;
}
public int firstPartial(int materialId) {
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
ItemStack item = inventory[i];
if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
return i;
}
}
return -1;
}
public int firstPartial(Material material) {
Validate.notNull(material, "Material cannot be null");
return firstPartial(material.getId());
}
private int firstPartial(ItemStack item) {
ItemStack[] inventory = getContents();
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
if (item == null) {
return -1;
}
for (int i = 0; i < inventory.length; i++) {
ItemStack cItem = inventory[i];
if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
return i;
}
}
return -1;
}
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
Validate.noNullElements(items, "Item cannot be null");
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
/* TODO: some optimization
* - Create a 'firstPartial' with a 'fromIndex'
* - Record the lastPartial per Material
* - Cache firstEmpty result
*/
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
while (true) {
// Do we already have a stack of it?
int firstPartial = firstPartial(item);
// Drat! no partial stack
if (firstPartial == -1) {
// Find a free spot!
int firstFree = firstEmpty();
if (firstFree == -1) {
// No space at all!
leftover.put(i, item);
break;
} else {
// More than a single stack!
if (item.getAmount() > getMaxItemStack()) {
CraftItemStack stack = CraftItemStack.asCraftCopy(item);
stack.setAmount(getMaxItemStack());
setItem(firstFree, stack);
item.setAmount(item.getAmount() - getMaxItemStack());
} else {
// Just store it
setItem(firstFree, item);
break;
}
}
} else {
// So, apparently it might only partially fit, well lets do just that
ItemStack partialItem = getItem(firstPartial);
int amount = item.getAmount();
int partialAmount = partialItem.getAmount();
int maxAmount = partialItem.getMaxStackSize();
// Check if it fully fits
if (amount + partialAmount <= maxAmount) {
partialItem.setAmount(amount + partialAmount);
break;
}
// It fits partially
partialItem.setAmount(maxAmount);
item.setAmount(amount + partialAmount - maxAmount);
}
}
}
return leftover;
}
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) {
Validate.notNull(items, "Items cannot be null");
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
// TODO: optimization
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
int toDelete = item.getAmount();
while (true) {
int first = first(item, false);
// Drat! we don't have this type in the inventory
if (first == -1) {
item.setAmount(toDelete);
leftover.put(i, item);
break;
} else {
ItemStack itemStack = getItem(first);
int amount = itemStack.getAmount();
if (amount <= toDelete) {
toDelete -= amount;
// clear the slot, all used up
clear(first);
} else {
// split the stack and store
itemStack.setAmount(amount - toDelete);
setItem(first, itemStack);
toDelete = 0;
}
}
// Bail when done
if (toDelete <= 0) {
break;
}
}
}
return leftover;
}
private int getMaxItemStack() {
return getInventory().getMaxStackSize();
}
public void remove(int materialId) {
ItemStack[] items = getContents();
for (int i = 0; i < items.length; i++) {
if (items[i] != null && items[i].getTypeId() == materialId) {
clear(i);
}
}
}
public void remove(Material material) {
Validate.notNull(material, "Material cannot be null");
remove(material.getId());
}
public void remove(ItemStack item) {
ItemStack[] items = getContents();
for (int i = 0; i < items.length; i++) {
if (items[i] != null && items[i].equals(item)) {
clear(i);
}
}
}
public void clear(int index) {
setItem(index, null);
}
public void clear() {
for (int i = 0; i < getSize(); i++) {
clear(i);
}
}
public ListIterator<ItemStack> iterator() {
return new InventoryIterator(this);
}
public ListIterator<ItemStack> iterator(int index) {
if (index < 0) {
index += getSize() + 1; // ie, with -1, previous() will return the last element
}
return new InventoryIterator(this, index);
}
public List<HumanEntity> getViewers() {
return this.inventory.getViewers();
}
public String getTitle() {
return inventory.getInventoryName();
}
public InventoryType getType() {
// Thanks to Droppers extending Dispensers, order is important.
if (inventory instanceof InventoryCrafting) {
return inventory.getSize() >= 9 ? InventoryType.WORKBENCH : InventoryType.CRAFTING;
} else if (inventory instanceof PlayerInventory) {
return InventoryType.PLAYER;
} else if (inventory instanceof TileEntityDropper) {
return InventoryType.DROPPER;
} else if (inventory instanceof TileEntityDispenser) {
return InventoryType.DISPENSER;
} else if (inventory instanceof TileEntityFurnace) {
return InventoryType.FURNACE;
} else if (inventory instanceof ContainerEnchantTableInventory) {
return InventoryType.ENCHANTING;
} else if (inventory instanceof TileEntityBrewingStand) {
return InventoryType.BREWING;
} else if (inventory instanceof CraftInventoryCustom.MinecraftInventory) {
return ((CraftInventoryCustom.MinecraftInventory) inventory).getType();
} else if (inventory instanceof InventoryEnderChest) {
return InventoryType.ENDER_CHEST;
} else if (inventory instanceof InventoryMerchant) {
return InventoryType.MERCHANT;
} else if (inventory instanceof TileEntityBeacon) {
return InventoryType.BEACON;
} else if (inventory instanceof ContainerAnvilInventory) {
return InventoryType.ANVIL;
} else if (inventory instanceof IHopper) {
return InventoryType.HOPPER;
} else {
return InventoryType.CHEST;
}
}
public InventoryHolder getHolder() {
return inventory.getOwner();
}
public int getMaxStackSize() {
return inventory.getMaxStackSize();
}
public void setMaxStackSize(int size) {
inventory.setMaxStackSize(size);
}
}
|