-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMEItemOutputBus.java
More file actions
134 lines (112 loc) · 4.16 KB
/
Copy pathMEItemOutputBus.java
File metadata and controls
134 lines (112 loc) · 4.16 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
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 net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.locks.ReadWriteLock;
public class MEItemOutputBus extends MEItemBus {
@Override
public IOInventory buildInventory() {
int size = 36;
int[] slotIDs = new int[size];
for (int slotID = 0; slotID < slotIDs.length; slotID++) {
slotIDs[slotID] = slotID;
}
IOInventory inv = new IOInventory(this, new int[]{}, slotIDs);
inv.setStackLimit(Integer.MAX_VALUE, slotIDs);
inv.setListener(slot -> {
synchronized (this) {
changedSlots[slot] = true;
}
});
return inv;
}
@Override
public ItemStack getVisualItemStack() {
return new ItemStack(ItemsMM.meItemOutputBus);
}
@Nullable
@Override
public MachineComponent.ItemBus provideComponent() {
return new MachineComponent.ItemBus(IOType.OUTPUT) {
@Override
public IOInventory getContainerProvider() {
return inventory;
}
};
}
@Nonnull
@Override
public TickingRequest getTickingRequest(@Nonnull final IGridNode node) {
return new TickingRequest(5, 60, !hasItem(), true);
}
@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;
}
inTick = true;
boolean successAtLeastOnce = false;
ReadWriteLock rwLock = inventory.getRWLock();
try {
rwLock.writeLock().lock();
IMEMonitor<IAEItemStack> inv = proxy.getStorage().getInventory(channel);
for (final int slot : needUpdateSlots) {
changedSlots[slot] = false;
ItemStack stack = inventory.getStackInSlot(slot);
if (stack.isEmpty()) {
continue;
}
ItemStack extracted = inventory.extractItem(slot, stack.getCount(), false);
IAEItemStack aeStack = channel.createStack(extracted);
if (aeStack == null) {
continue;
}
IAEItemStack left = Platform.poweredInsert(proxy.getEnergy(), inv, aeStack, source);
if (left != null) {
inventory.setStackInSlot(slot, left.createItemStack());
if (aeStack.getStackSize() != left.getStackSize()) {
successAtLeastOnce = true;
}
} else {
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;
}
}
@Override
public void markNoUpdate() {
if (hasChangedSlots()) {
try {
proxy.getTick().alertDevice(proxy.getNode());
} catch (GridAccessException e) {
// NO-OP
}
}
super.markNoUpdate();
}
}