-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMEItemOutputBus.java
More file actions
200 lines (166 loc) · 6.24 KB
/
Copy pathMEItemOutputBus.java
File metadata and controls
200 lines (166 loc) · 6.24 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
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 net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.locks.ReadWriteLock;
public class MEItemOutputBus extends MEItemBus implements SettingsTransfer {
private int configuredStackSize;
@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[0], slotIDs);
configuredStackSize = Integer.MAX_VALUE;
inv.setStackLimit(this.configuredStackSize, 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;
failureCounter[slot] = 0;
} else {
failureCounter[slot] = Math.min(failureCounter[slot] + 1, 10);
}
} else {
successAtLeastOnce = true;
failureCounter[slot] = 0;
}
}
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();
}
public int getConfiguredStackSize() {
return this.configuredStackSize;
}
public void setConfiguredStackSize(int size) {
this.configuredStackSize = Math.max(1, size);
this.applyStackSizeToInventory();
this.markForUpdate();
}
private void applyStackSizeToInventory() {
if (this.inventory != null) {
int[] slotIDs = new int[this.inventory.getSlots()];
for (int slotID = 0; slotID < slotIDs.length; slotID++) {
slotIDs[slotID] = slotID;
}
this.inventory.setStackLimit(this.configuredStackSize, slotIDs);
}
}
@Override
public void readCustomNBT(final NBTTagCompound compound) {
super.readCustomNBT(compound);
if (compound.hasKey("configuredStackSize")) {
this.configuredStackSize = compound.getInteger("configuredStackSize");
}
this.applyStackSizeToInventory();
}
@Override
public void writeCustomNBT(final NBTTagCompound compound) {
super.writeCustomNBT(compound);
compound.setInteger("configuredStackSize", this.configuredStackSize);
}
@Override
public NBTTagCompound downloadSettings() {
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("configuredStackSize", this.configuredStackSize);
return tag;
}
@Override
public void uploadSettings(NBTTagCompound settings) {
if (settings.hasKey("configuredStackSize")) {
setConfiguredStackSize(settings.getInteger("configuredStackSize"));
try {
proxy.getTick().alertDevice(proxy.getNode());
} catch (GridAccessException e) {
// NO-OP
}
}
}
}