-
Notifications
You must be signed in to change notification settings - Fork 497
/
TransactorSimple.java
120 lines (105 loc) · 3.19 KB
/
TransactorSimple.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
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
package buildcraft.core.inventory;
import buildcraft.core.inventory.InventoryIterator.IInvSlot;
import buildcraft.core.inventory.filters.IStackFilter;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
public class TransactorSimple extends Transactor {
protected static final StackHelper MERGE_HELPER = new StackHelper();
protected IInventory inventory;
public TransactorSimple(IInventory inventory) {
this.inventory = inventory;
}
@Override
public int inject(ItemStack stack, ForgeDirection orientation, boolean doAdd) {
List<IInvSlot> filledSlots = new ArrayList<IInvSlot>(inventory.getSizeInventory());
List<IInvSlot> emptySlots = new ArrayList<IInvSlot>(inventory.getSizeInventory());
for (IInvSlot slot : InventoryIterator.getIterable(inventory, orientation)) {
if (slot.canPutStackInSlot(stack)) {
if (slot.getStackInSlot() == null) {
emptySlots.add(slot);
} else {
filledSlots.add(slot);
}
}
}
int injected = 0;
injected = tryPut(stack, filledSlots, injected, doAdd);
injected = tryPut(stack, emptySlots, injected, doAdd);
inventory.onInventoryChanged();
return injected;
}
private int tryPut(ItemStack stack, List<IInvSlot> slots, int injected, boolean doAdd) {
if (injected >= stack.stackSize) {
return injected;
}
for (IInvSlot slot : slots) {
ItemStack stackInSlot = slot.getStackInSlot();
if (stackInSlot == null || MERGE_HELPER.canStacksMerge(stackInSlot, stack)) {
int used = addToSlot(slot, stack, injected, doAdd);
if (used > 0) {
injected += used;
if (injected >= stack.stackSize) {
return injected;
}
}
}
}
return injected;
}
/**
*
* @param slot
* @param stack
* @param injected Amount not to move?
* @param doAdd
* @return Return the number of items moved.
*/
protected int addToSlot(IInvSlot slot, ItemStack stack, int injected, boolean doAdd) {
int available = stack.stackSize - injected;
int max = Math.min(stack.getMaxStackSize(), inventory.getInventoryStackLimit());
ItemStack stackInSlot = slot.getStackInSlot();
if (stackInSlot == null) {
int wanted = Math.min(available, max);
if (doAdd) {
stackInSlot = stack.copy();
stackInSlot.stackSize = wanted;
slot.setStackInSlot(stackInSlot);
}
return wanted;
}
if (!MERGE_HELPER.canStacksMerge(stack, stackInSlot)) {
return 0;
}
int wanted = max - stackInSlot.stackSize;
if (wanted <= 0) {
return 0;
}
if (wanted > available) {
wanted = available;
}
if (doAdd) {
stackInSlot.stackSize += wanted;
slot.setStackInSlot(stackInSlot);
}
return wanted;
}
@Override
public ItemStack remove(IStackFilter filter, ForgeDirection orientation, boolean doRemove) {
for (IInvSlot slot : InventoryIterator.getIterable(inventory, orientation)) {
ItemStack stack = slot.getStackInSlot();
if (stack != null && slot.canTakeStackFromSlot(stack) && filter.matches(stack)) {
if (doRemove) {
return slot.decreaseStackInSlot();
} else {
ItemStack output = stack.copy();
output.stackSize = 1;
return output;
}
}
}
return null;
}
}