-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathOverlayedItemHandler.java
More file actions
160 lines (137 loc) · 5.89 KB
/
OverlayedItemHandler.java
File metadata and controls
160 lines (137 loc) · 5.89 KB
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
package gregtech.api.util;
import gregtech.api.util.hash.ItemStackHashStrategy;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import org.jetbrains.annotations.NotNull;
public class OverlayedItemHandler {
private final OverlayedItemHandlerSlot[] originalSlots;
private final OverlayedItemHandlerSlot[] slots;
private final IItemHandler overlayedHandler;
public OverlayedItemHandler(@NotNull IItemHandler toOverlay) {
this.slots = new OverlayedItemHandlerSlot[toOverlay.getSlots()];
this.originalSlots = new OverlayedItemHandlerSlot[toOverlay.getSlots()];
this.overlayedHandler = toOverlay;
}
/**
* Resets the {slots} array to the state when the handler was
* first mirrored
*/
public void reset() {
for (int i = 0; i < this.originalSlots.length; i++) {
if (this.originalSlots[i] != null) {
this.slots[i] = this.originalSlots[i].copy();
}
}
}
public int getSlots() {
return overlayedHandler.getSlots();
}
/**
* Populates the {@code originalSlots} and {@code slots}arrays with the current state of the inventory.
*
* @param slot the slot to populate
*/
private void initSlot(int slot) {
if (this.originalSlots[slot] == null) {
ItemStack stackToMirror = overlayedHandler.getStackInSlot(slot);
int slotLimit = overlayedHandler.getSlotLimit(slot);
this.originalSlots[slot] = new OverlayedItemHandlerSlot(stackToMirror, slotLimit);
this.slots[slot] = new OverlayedItemHandlerSlot(stackToMirror, slotLimit);
}
}
public int insertStackedItemStack(@NotNull ItemStack stack, int amountToInsert) {
int lastKnownPopulatedSlot = 0;
// loop through all slots, looking for ones matching the key
for (int i = 0; i < this.slots.length; i++) {
// populate the slot if it's not already populated
initSlot(i);
// if it's the same item or there is no item in the slot
ItemStack slotKey = this.slots[i].getItemStack();
if (slotKey.isEmpty() || ItemStackHashStrategy.comparingAllButCount.equals(slotKey, stack)) {
// if the slot is not full
int canInsertUpTo = Math.min(this.slots[i].getSlotLimit() - this.slots[i].getCount(),
stack.getMaxStackSize());
if (canInsertUpTo > 0) {
int insertedAmount = Math.min(canInsertUpTo, amountToInsert);
this.slots[i].setItemStack(stack.copy()); // this copy may not be need, needs further tests
this.slots[i].setCount(this.slots[i].getCount() + insertedAmount);
amountToInsert -= insertedAmount;
}
}
lastKnownPopulatedSlot = i;
// early exit if finished inserting everything
if (amountToInsert == 0) {
return 0;
}
}
// if the amountToInsert is still greater than 0, we need to insert it into a new slot
if (amountToInsert > 0) {
// loop through all slots, starting from after the last seen slot with items in it, looking for empty ones.
for (int i = lastKnownPopulatedSlot + 1; i < this.slots.length; i++) {
OverlayedItemHandlerSlot slot = this.slots[i];
// if the slot is empty
if (slot.getItemStack().isEmpty()) {
int canInsertUpTo = Math.min(stack.getMaxStackSize(), slot.getSlotLimit());
if (canInsertUpTo > 0) {
int insertedAmount = Math.min(canInsertUpTo, amountToInsert);
slot.setItemStack(stack.copy()); // this copy may not be need, needs further tests
slot.setCount(insertedAmount);
amountToInsert -= insertedAmount;
}
if (amountToInsert == 0) {
return 0;
}
}
}
}
// return the amount that wasn't inserted
return amountToInsert;
}
private static class OverlayedItemHandlerSlot {
private ItemStack itemStack = ItemStack.EMPTY;
private int count = 0;
private int slotLimit;
protected OverlayedItemHandlerSlot(@NotNull ItemStack stackToMirror, int slotLimit) {
if (!stackToMirror.isEmpty()) {
this.itemStack = stackToMirror.copy();
this.count = stackToMirror.getCount();
this.slotLimit = Math.min(itemStack.getMaxStackSize(), slotLimit);
} else {
this.slotLimit = slotLimit;
}
}
protected OverlayedItemHandlerSlot(@NotNull ItemStack itemStack, int slotLimit, int count) {
this.itemStack = itemStack;
this.count = count;
this.slotLimit = slotLimit;
}
public int getSlotLimit() {
return slotLimit;
}
public int getCount() {
return count;
}
/**
* Storage of this ItemStack elsewhere will require copying it
*
* @return the stored ItemStack
*/
@NotNull
public ItemStack getItemStack() {
return this.itemStack;
}
public void setItemStack(@NotNull ItemStack itemStack) {
if (!ItemStackHashStrategy.comparingAllButCount.equals(this.itemStack, itemStack)) {
this.itemStack = itemStack;
this.slotLimit = Math.min(itemStack.getMaxStackSize(), slotLimit);
}
}
public void setCount(int count) {
this.count = count;
}
@NotNull
OverlayedItemHandlerSlot copy() {
return new OverlayedItemHandlerSlot(this.itemStack, this.slotLimit, this.count);
}
}
}