-
Notifications
You must be signed in to change notification settings - Fork 497
/
TransactorSimple.java
134 lines (113 loc) · 3.46 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
* <p/>
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.inventory;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.IInvSlot;
import buildcraft.core.lib.inventory.filters.IStackFilter;
public class TransactorSimple extends Transactor {
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.markDirty();
return injected;
}
private int tryPut(ItemStack stack, List<IInvSlot> slots, int injected, boolean doAdd) {
int realInjected = injected;
if (realInjected >= stack.stackSize) {
return realInjected;
}
for (IInvSlot slot : slots) {
ItemStack stackInSlot = slot.getStackInSlot();
if (stackInSlot == null || StackHelper.canStacksMerge(stackInSlot, stack)) {
int used = addToSlot(slot, stack, realInjected, doAdd);
if (used > 0) {
realInjected += used;
if (realInjected >= stack.stackSize) {
return realInjected;
}
}
}
}
return realInjected;
}
/**
*
* @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 (!StackHelper.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(1);
} else {
ItemStack output = stack.copy();
output.stackSize = 1;
return output;
}
}
}
return null;
}
}