-
Notifications
You must be signed in to change notification settings - Fork 497
/
InventoryUtil.java
69 lines (58 loc) · 2.01 KB
/
InventoryUtil.java
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
package buildcraft.core.utils;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
public class InventoryUtil {
private final IInventory _inventory;
public InventoryUtil(IInventory inventory) {
this._inventory = inventory;
}
public int roomForItem(ItemStack itemToTest) {
if (itemToTest == null)
return 0;
int totalRoom = 0;
for (int i = 0; i < _inventory.getSizeInventory(); i++) {
ItemStack stack = _inventory.getStackInSlot(i);
if (stack == null) {
totalRoom += Math.min(_inventory.getInventoryStackLimit(), itemToTest.getMaxStackSize());
continue;
}
if (itemToTest.itemID != stack.itemID || (!itemToTest.getItem().isDamageable() && itemToTest.getItemDamage() != stack.getItemDamage())) {
continue;
}
totalRoom += (Math.min(_inventory.getInventoryStackLimit(), itemToTest.getMaxStackSize()) - stack.stackSize);
}
return totalRoom;
}
public boolean hasRoomForItem(ItemStack itemToTest) {
return roomForItem(itemToTest) > 0;
}
public int getIdForFirstSlot() {
for (int i = 0; i < _inventory.getSizeInventory(); i++) {
if (_inventory.getStackInSlot(i) != null)
return i;
}
return -1;
}
public boolean isEmpty() {
return (getIdForFirstSlot() >= 0);
}
public ItemStack addToInventory(ItemStack stackToMove) {
for (int i = 0; i < _inventory.getSizeInventory(); i++) {
ItemStack stack = _inventory.getStackInSlot(i);
if (stack == null) {
_inventory.setInventorySlotContents(i, stackToMove);
return null;
}
if (stackToMove.itemID == stack.itemID && (stackToMove.getItem().isDamageable() || stackToMove.getItemDamage() == stack.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, stackToMove)) {
if (stackToMove.stackSize + stack.stackSize <= stack.getMaxStackSize()) {
stack.stackSize += stackToMove.stackSize;
return null;
}
int itemsToMove = stack.getMaxStackSize() - stack.stackSize;
stack.stackSize += itemsToMove;
stackToMove.stackSize -= itemsToMove;
}
}
return stackToMove;
}
}