-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMEItemInputBus.java
More file actions
284 lines (246 loc) · 9.79 KB
/
Copy pathMEItemInputBus.java
File metadata and controls
284 lines (246 loc) · 9.79 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
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
package github.kasuminova.mmce.common.tile;
import appeng.api.networking.IGridNode;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.me.GridAccessException;
import appeng.util.Platform;
import github.kasuminova.mmce.common.tile.base.MEItemBus;
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
import hellfirepvp.modularmachinery.common.machine.IOType;
import hellfirepvp.modularmachinery.common.machine.MachineComponent;
import hellfirepvp.modularmachinery.common.util.IOInventory;
import hellfirepvp.modularmachinery.common.util.ItemUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReadWriteLock;
public class MEItemInputBus extends MEItemBus {
// A simple cache for AEItemStack.
private static final Map<ItemStack, IAEItemStack> AE_STACK_CACHE = new WeakHashMap<>();
private IOInventory configInventory = buildConfigInventory();
@Override
public IOInventory buildInventory() {
int size = 16;
int[] slotIDs = new int[size];
for (int slotID = 0; slotID < size; slotID++) {
slotIDs[slotID] = slotID;
}
IOInventory inv = new IOInventory(this, slotIDs, new int[]{});
inv.setStackLimit(Integer.MAX_VALUE, slotIDs);
inv.setListener(slot -> {
synchronized (this) {
changedSlots[slot] = true;
}
});
return inv;
}
@Override
public ItemStack getVisualItemStack() {
return new ItemStack(ItemsMM.meItemInputBus);
}
public IOInventory buildConfigInventory() {
int size = 16;
int[] slotIDs = new int[size];
for (int slotID = 0; slotID < size; slotID++) {
slotIDs[slotID] = slotID;
}
IOInventory inv = new IOInventory(this, new int[]{}, new int[]{});
inv.setStackLimit(Integer.MAX_VALUE, slotIDs);
inv.setMiscSlots(slotIDs);
inv.setListener(slot -> {
synchronized (this) {
changedSlots[slot] = true;
}
});
return inv;
}
@Override
public void readCustomNBT(final NBTTagCompound compound) {
super.readCustomNBT(compound);
if (compound.hasKey("configInventory")) {
readConfigInventoryNBT(compound.getCompoundTag("configInventory"));
}
}
@Override
public void writeCustomNBT(final NBTTagCompound compound) {
super.writeCustomNBT(compound);
compound.setTag("configInventory", configInventory.writeNBT());
}
public IOInventory getConfigInventory() {
return configInventory;
}
@Nullable
@Override
public MachineComponent.ItemBus provideComponent() {
return new MachineComponent.ItemBus(IOType.INPUT) {
@Override
public IOInventory getContainerProvider() {
return inventory;
}
};
}
@Nonnull
@Override
public TickingRequest getTickingRequest(@Nonnull final IGridNode node) {
return new TickingRequest(10, 120, !needsUpdate(), true);
}
private boolean needsUpdate() {
for (int slot = 0; slot < configInventory.getSlots(); slot++) {
ItemStack cfgStack = configInventory.getStackInSlot(slot);
ItemStack invStack = inventory.getStackInSlot(slot);
if (cfgStack.isEmpty()) {
if (!invStack.isEmpty()) {
return true;
}
continue;
}
if (invStack.isEmpty()) {
return true;
}
if (!ItemUtils.matchStacks(cfgStack, invStack) || invStack.getCount() != cfgStack.getCount()) {
return true;
}
}
return false;
}
@Nonnull
@Override
public TickRateModulation tickingRequest(@Nonnull final IGridNode node, final int ticksSinceLastCall) {
if (!proxy.isActive()) {
return TickRateModulation.IDLE;
}
int[] needUpdateSlots = getNeedUpdateSlots();
if (needUpdateSlots.length == 0) {
return TickRateModulation.SLOWER;
}
ReadWriteLock rwLock = inventory.getRWLock();
try {
rwLock.writeLock().lock();
boolean successAtLeastOnce = false;
inTick = true;
IMEMonitor<IAEItemStack> inv = proxy.getStorage().getInventory(channel);
for (final int slot : needUpdateSlots) {
changedSlots[slot] = false;
ItemStack cfgStack = configInventory.getStackInSlot(slot);
ItemStack invStack = inventory.getStackInSlot(slot);
if (cfgStack.isEmpty()) {
if (invStack.isEmpty()) {
continue;
}
inventory.setStackInSlot(slot, insertStackToAE(inv, invStack));
continue;
}
if (!ItemUtils.matchStacks(cfgStack, invStack)) {
if (invStack.isEmpty() || insertStackToAE(inv, invStack).isEmpty()) {
ItemStack stack = extractStackFromAE(inv, cfgStack);
inventory.setStackInSlot(slot, stack);
if (!stack.isEmpty()) {
successAtLeastOnce = true;
}
}
continue;
}
if (cfgStack.getCount() == invStack.getCount()) {
continue;
}
if (cfgStack.getCount() > invStack.getCount()) {
int countToReceive = cfgStack.getCount() - invStack.getCount();
ItemStack stack = extractStackFromAE(inv, ItemUtils.copyStackWithSize(invStack, countToReceive));
if (!stack.isEmpty()) {
int newCount = invStack.getCount() + stack.getCount();
inventory.setStackInSlot(slot, ItemUtils.copyStackWithSize(invStack, newCount));
successAtLeastOnce = true;
failureCounter[slot] = 0;
} else {
// If AE doesn't have enough item?
failureCounter[slot]++;
}
} else {
int countToExtract = invStack.getCount() - cfgStack.getCount();
ItemStack stack = insertStackToAE(inv, ItemUtils.copyStackWithSize(invStack, countToExtract));
if (stack.isEmpty()) {
inventory.setStackInSlot(slot, ItemUtils.copyStackWithSize(
invStack, invStack.getCount() - countToExtract)
);
} else {
inventory.setStackInSlot(slot, ItemUtils.copyStackWithSize(
invStack, invStack.getCount() - countToExtract + stack.getCount())
);
}
successAtLeastOnce = true;
}
}
inTick = false;
rwLock.writeLock().unlock();
return successAtLeastOnce ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
} catch (GridAccessException e) {
inTick = false;
changedSlots = new boolean[changedSlots.length];
rwLock.writeLock().unlock();
return TickRateModulation.IDLE;
}
}
private ItemStack extractStackFromAE(final IMEMonitor<IAEItemStack> inv, final ItemStack stack) throws GridAccessException {
IAEItemStack aeStack = createStack(stack);
if (aeStack == null) {
return ItemStack.EMPTY;
}
IAEItemStack extracted = Platform.poweredExtraction(proxy.getEnergy(), inv, aeStack, source);
if (extracted == null) {
return ItemStack.EMPTY;
}
return extracted.createItemStack();
}
private ItemStack insertStackToAE(final IMEMonitor<IAEItemStack> inv, final ItemStack stack) throws GridAccessException {
IAEItemStack aeStack = createStack(stack);
if (aeStack == null) {
return stack;
}
IAEItemStack left = Platform.poweredInsert(proxy.getEnergy(), inv, aeStack, source);
if (left == null) {
return ItemStack.EMPTY;
}
return left.createItemStack();
}
private IAEItemStack createStack(final ItemStack stack) {
return AE_STACK_CACHE.computeIfAbsent(stack, v -> channel.createStack(stack));
}
@Override
public void markNoUpdate() {
if (hasChangedSlots()) {
try {
proxy.getTick().alertDevice(proxy.getNode());
} catch (GridAccessException e) {
// NO-OP
}
}
super.markNoUpdate();
}
public boolean configInvHasItem() {
for (int i = 0; i < configInventory.getSlots(); i++) {
ItemStack stack = configInventory.getStackInSlot(i);
if (!stack.isEmpty()) {
return true;
}
}
return false;
}
public void readConfigInventoryNBT(final NBTTagCompound compound) {
configInventory = IOInventory.deserialize(this, compound);
configInventory.setListener(slot -> {
synchronized (this) {
changedSlots[slot] = true;
}
});
int[] slotIDs = new int[configInventory.getSlots()];
for (int slotID = 0; slotID < slotIDs.length; slotID++) {
slotIDs[slotID] = slotID;
}
configInventory.setStackLimit(Integer.MAX_VALUE, slotIDs);
}
}