-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMEGasInputBus.java
More file actions
209 lines (181 loc) · 7.72 KB
/
Copy pathMEGasInputBus.java
File metadata and controls
209 lines (181 loc) · 7.72 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
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.me.GridAccessException;
import appeng.util.Platform;
import com.mekeng.github.common.me.data.IAEGasStack;
import com.mekeng.github.common.me.data.impl.AEGasStack;
import com.mekeng.github.common.me.inventory.impl.GasInventory;
import github.kasuminova.mmce.common.tile.base.MEGasBus;
import github.kasuminova.mmce.common.util.IExtendedGasHandler;
import hellfirepvp.modularmachinery.common.crafting.ComponentType;
import hellfirepvp.modularmachinery.common.lib.ComponentTypesMM;
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
import hellfirepvp.modularmachinery.common.machine.IOType;
import hellfirepvp.modularmachinery.common.machine.MachineComponent;
import mekanism.api.gas.GasStack;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
public class MEGasInputBus extends MEGasBus {
private final GasInventory config = new GasInventory(MEGasBus.TANK_SLOT_AMOUNT, this);
@Override
public ItemStack getVisualItemStack() {
return new ItemStack(ItemsMM.meGasInputBus);
}
@Override
public void readCustomNBT(final NBTTagCompound compound) {
super.readCustomNBT(compound);
config.load(compound.getCompoundTag("config"));
}
@Override
public void writeCustomNBT(final NBTTagCompound compound) {
super.writeCustomNBT(compound);
compound.setTag("config", config.save());
upgrades.writeToNBT(compound, "upgrades");
}
public GasInventory getConfig() {
return config;
}
@Nonnull
@Override
public TickingRequest getTickingRequest(@Nonnull final IGridNode node) {
return new TickingRequest(10, 120, !needsUpdate(), true);
}
private boolean needsUpdate() {
int capacity = tanks.getTanks()[0].getMaxGas();
for (int slot = 0; slot < config.size(); slot++) {
GasStack cfgStack = config.getGasStack(slot);
GasStack invStack = tanks.getGasStack(slot);
if (cfgStack == null) {
if (invStack != null) {
return true;
}
continue;
}
if (invStack == null) {
return true;
}
if (!cfgStack.isGasEqual(invStack) || invStack.amount != capacity) {
return true;
}
}
return false;
}
@Nonnull
@Override
public TickRateModulation tickingRequest(@Nonnull final IGridNode node, final int ticksSinceLastCall) {
if (!proxy.isActive()) {
return TickRateModulation.IDLE;
}
inTick = true;
try {
boolean successAtLeastOnce = false;
IMEMonitor<IAEGasStack> inv = proxy.getStorage().getInventory(channel);
int capacity = tanks.getTanks()[0].getMaxGas();
synchronized (tanks) {
for (final int slot : getNeedUpdateSlots()) {
changedSlots[slot] = false;
GasStack cfgStack = config.getGasStack(slot);
GasStack invStack = tanks.getGasStack(slot);
if (cfgStack == null) {
if (invStack == null) {
continue;
}
IAEGasStack left = insertStackToAE(inv, invStack);
tanks.setGas(slot, left == null ? null : left.getGasStack());
continue;
}
if (!cfgStack.isGasEqual(invStack)) {
if (invStack != null) {
IAEGasStack left = insertStackToAE(inv, invStack);
if (left != null) {
tanks.setGas(slot, left.getGasStack());
continue;
}
}
GasStack copied = cfgStack.copy();
copied.amount = capacity;
IAEGasStack stack = extractStackFromAE(inv, copied);
if (stack != null) {
tanks.setGas(slot, stack.getGasStack());
successAtLeastOnce = true;
}
continue;
}
// Because cfgStack is not null and cfgStack.isGasEqual(invStack) is true.
//noinspection DataFlowIssue
if (capacity == invStack.amount) {
continue;
}
if (capacity > invStack.amount) {
int countToReceive = capacity - invStack.amount;
GasStack copied = invStack.copy();
copied.amount = countToReceive;
IAEGasStack stack = extractStackFromAE(inv, copied);
if (stack != null) {
copied = invStack.copy();
copied.amount = (int) (invStack.amount + stack.getStackSize());
tanks.setGas(slot, copied);
successAtLeastOnce = true;
}
} else {
int countToExtract = invStack.amount - capacity;
GasStack copied = invStack.copy();
copied.amount = countToExtract;
IAEGasStack left = insertStackToAE(inv, copied);
if (left == null) {
copied.amount = invStack.amount - countToExtract;
tanks.setGas(slot, copied);
} else {
copied.amount = (int) ((invStack.amount - countToExtract) + left.getStackSize());
tanks.setGas(slot, copied);
}
successAtLeastOnce = true;
}
}
}
inTick = false;
return successAtLeastOnce ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
} catch (GridAccessException e) {
inTick = false;
changedSlots = new boolean[TANK_SLOT_AMOUNT];
return TickRateModulation.IDLE;
}
}
private IAEGasStack extractStackFromAE(final IMEMonitor<IAEGasStack> inv, final GasStack stack) throws GridAccessException {
return Platform.poweredExtraction(proxy.getEnergy(), inv, Objects.requireNonNull(AEGasStack.of(stack)), source);
}
private IAEGasStack insertStackToAE(final IMEMonitor<IAEGasStack> inv, final GasStack stack) throws GridAccessException {
return Platform.poweredInsert(proxy.getEnergy(), inv, Objects.requireNonNull(AEGasStack.of(stack)), source);
}
@Nullable
@Override
public MachineComponent<IExtendedGasHandler> provideComponent() {
return new MachineComponent<>(IOType.INPUT) {
@Override
public ComponentType getComponentType() {
return ComponentTypesMM.COMPONENT_GAS;
}
@Override
public IExtendedGasHandler getContainerProvider() {
return handler;
}
};
}
@Override
public void markNoUpdate() {
if (needsUpdate()) {
try {
proxy.getTick().alertDevice(proxy.getNode());
} catch (GridAccessException e) {
// NO-OP
}
}
super.markNoUpdate();
}
}