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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
package gregtech.common.covers;
import java.util.Arrays;
import javax.annotation.Nonnull;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import com.google.common.io.ByteArrayDataInput;
import com.gtnewhorizons.modularui.api.math.MathExpression;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.common.widget.TextWidget;
import gregtech.api.gui.modularui.GT_CoverUIBuildContext;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.util.GT_CoverBehaviorBase;
import gregtech.api.util.GT_Utility;
import gregtech.api.util.ISerializableObject;
import gregtech.common.gui.modularui.widget.CoverDataControllerWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollower_TextFieldWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollower_ToggleButtonWidget;
import gregtech.common.tileentities.storage.GT_MetaTileEntity_DigitalTankBase;
import io.netty.buffer.ByteBuf;
/**
* TODO: Implement overlay rendering only with
* {@link GT_CoverBehaviorBase#getSpecialCoverFGTextureImpl(ForgeDirection, int, ISerializableObject, ICoverable)}
*/
public class GT_Cover_LiquidMeter extends GT_CoverBehaviorBase<GT_Cover_LiquidMeter.LiquidMeterData> {
public GT_Cover_LiquidMeter(ITexture coverTexture) {
super(LiquidMeterData.class, coverTexture);
}
@Override
public LiquidMeterData createDataObject(int aLegacyData) {
return new LiquidMeterData(aLegacyData == 0, false, 0);
}
@Override
public LiquidMeterData createDataObject() {
return new LiquidMeterData();
}
@Override
protected boolean isRedstoneSensitiveImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable,
ICoverable aTileEntity, long aTimer) {
return false;
}
public static byte computeSignalBasedOnFluid(ICoverable tileEntity, boolean inverted, int threshold) {
if (tileEntity instanceof IFluidHandler) {
FluidTankInfo[] tanks = ((IFluidHandler) tileEntity).getTankInfo(ForgeDirection.UNKNOWN);
long max = 0;
long used = 0;
if (tanks != null) {
for (FluidTankInfo tank : tanks) {
if (tank != null) {
if (tileEntity instanceof BaseMetaTileEntity && ((BaseMetaTileEntity) tileEntity)
.getMetaTileEntity() instanceof GT_MetaTileEntity_DigitalTankBase) {
max += ((GT_MetaTileEntity_DigitalTankBase) ((BaseMetaTileEntity) tileEntity)
.getMetaTileEntity()).getRealCapacity();
} else max += tank.capacity;
FluidStack tLiquid = tank.fluid;
if (tLiquid != null) {
used += tLiquid.amount;
}
}
}
}
return GT_Utility.convertRatioToRedstone(used, max, threshold, inverted);
} else {
return 0;
}
}
@Override
protected LiquidMeterData doCoverThingsImpl(ForgeDirection side, byte aInputRedstone, int aCoverID,
LiquidMeterData aCoverVariable, ICoverable aTileEntity, long aTimer) {
byte signal = computeSignalBasedOnFluid(aTileEntity, aCoverVariable.inverted, aCoverVariable.threshold);
if (aCoverVariable.strong) {
aTileEntity.setStrongOutputRedstoneSignal(side, signal);
} else {
aTileEntity.setOutputRedstoneSignal(side, signal);
}
return aCoverVariable;
}
@Override
protected LiquidMeterData onCoverScrewdriverClickImpl(ForgeDirection side, int aCoverID,
LiquidMeterData aCoverVariable, ICoverable aTileEntity, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (aCoverVariable.inverted) {
aCoverVariable.inverted = false;
GT_Utility.sendChatToPlayer(aPlayer, GT_Utility.trans("055", "Normal"));
} else {
aCoverVariable.inverted = true;
GT_Utility.sendChatToPlayer(aPlayer, GT_Utility.trans("054", "Inverted"));
}
return aCoverVariable;
}
@Override
protected boolean letsEnergyInImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean letsEnergyOutImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean letsFluidInImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable, Fluid aFluid,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean letsFluidOutImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable, Fluid aFluid,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean letsItemsInImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable, int aSlot,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean letsItemsOutImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable, int aSlot,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean manipulatesSidedRedstoneOutputImpl(ForgeDirection side, int aCoverID,
LiquidMeterData aCoverVariable, ICoverable aTileEntity) {
return true;
}
@Override
protected int getTickRateImpl(ForgeDirection side, int aCoverID, LiquidMeterData aCoverVariable,
ICoverable aTileEntity) {
return 5;
}
// GUI stuff
@Override
public boolean hasCoverGUI() {
return true;
}
@Override
public boolean useModularUI() {
return true;
}
@Override
public ModularWindow createWindow(GT_CoverUIBuildContext buildContext) {
return new LiquidMeterUIFactory(buildContext).createWindow();
}
private class LiquidMeterUIFactory extends UIFactory {
private static final int startX = 10;
private static final int startY = 25;
private static final int spaceX = 18;
private static final int spaceY = 18;
public LiquidMeterUIFactory(GT_CoverUIBuildContext buildContext) {
super(buildContext);
}
@SuppressWarnings("PointlessArithmeticExpression")
@Override
protected void addUIWidgets(ModularWindow.Builder builder) {
final String INVERTED = GT_Utility.trans("INVERTED", "Inverted");
final String NORMAL = GT_Utility.trans("NORMAL", "Normal");
final String STRONG = GT_Utility.trans("STRONG", "Strong");
final String WEAK = GT_Utility.trans("WEAK", "Weak");
final int maxCapacity;
if (getUIBuildContext().getTile() instanceof IFluidHandler) {
FluidTankInfo[] tanks = ((IFluidHandler) getUIBuildContext().getTile())
.getTankInfo(ForgeDirection.UNKNOWN);
maxCapacity = Arrays.stream(tanks)
.mapToInt(tank -> tank.capacity)
.sum();
} else {
maxCapacity = -1;
}
builder.widget(
new CoverDataControllerWidget<>(this::getCoverData, this::setCoverData, GT_Cover_LiquidMeter.this)
.addFollower(
CoverDataFollower_ToggleButtonWidget.ofRedstone(),
coverData -> coverData.inverted,
(coverData, state) -> {
coverData.inverted = state;
return coverData;
},
widget -> widget.addTooltip(0, NORMAL)
.addTooltip(1, INVERTED)
.setPos(spaceX * 0, spaceY * 0))
.addFollower(
CoverDataFollower_ToggleButtonWidget.ofRedstone(),
coverData -> coverData.strong,
(coverData, state) -> {
coverData.strong = state;
return coverData;
},
widget -> widget.addTooltip(0, WEAK)
.addTooltip(1, STRONG)
.setPos(spaceX * 0, spaceY * 1))
.addFollower(
new CoverDataFollower_TextFieldWidget<>(),
coverData -> String.valueOf(coverData.threshold),
(coverData, state) -> {
coverData.threshold = (int) MathExpression.parseMathExpression(state);
return coverData;
},
widget -> widget.setOnScrollNumbers(1000, 100, 100000)
.setNumbers(0, maxCapacity > 0 ? maxCapacity : Integer.MAX_VALUE)
.setFocusOnGuiOpen(true)
.setPos(spaceX * 0, spaceY * 2 + 2)
.setSize(spaceX * 4 + 5, 12))
.setPos(startX, startY))
.widget(
TextWidget
.dynamicString(() -> getCoverData() != null ? getCoverData().inverted ? INVERTED : NORMAL : "")
.setSynced(false)
.setDefaultColor(COLOR_TEXT_GRAY.get())
.setPos(startX + spaceX * 1, 4 + startY + spaceY * 0))
.widget(
TextWidget.dynamicString(() -> getCoverData() != null ? getCoverData().strong ? STRONG : WEAK : "")
.setSynced(false)
.setDefaultColor(COLOR_TEXT_GRAY.get())
.setPos(startX + spaceX * 1, 4 + startY + spaceY * 1))
.widget(
new TextWidget(GT_Utility.trans("222", "Fluid threshold")).setDefaultColor(COLOR_TEXT_GRAY.get())
.setPos(startX + spaceX * 5 - 10, startY + spaceY * 2 + 4));
}
}
public static class LiquidMeterData implements ISerializableObject {
private boolean inverted;
private boolean strong;
/**
* The special value {@code 0} means threshold check is disabled.
*/
private int threshold;
public LiquidMeterData() {
inverted = false;
strong = false;
threshold = 0;
}
public LiquidMeterData(boolean inverted, boolean strong, int threshold) {
this.inverted = inverted;
this.strong = strong;
this.threshold = threshold;
}
@Nonnull
@Override
public ISerializableObject copy() {
return new LiquidMeterData(inverted, strong, threshold);
}
@Nonnull
@Override
public NBTBase saveDataToNBT() {
NBTTagCompound tag = new NBTTagCompound();
tag.setBoolean("invert", inverted);
tag.setBoolean("strong", strong);
tag.setInteger("threshold", threshold);
return tag;
}
@Override
public void writeToByteBuf(ByteBuf aBuf) {
aBuf.writeBoolean(inverted);
aBuf.writeBoolean(strong);
aBuf.writeInt(threshold);
}
@Override
public void loadDataFromNBT(NBTBase aNBT) {
NBTTagCompound tag = (NBTTagCompound) aNBT;
inverted = tag.getBoolean("invert");
strong = tag.getBoolean("strong");
threshold = tag.getInteger("threshold");
}
@Nonnull
@Override
public ISerializableObject readFromPacket(ByteArrayDataInput aBuf, EntityPlayerMP aPlayer) {
inverted = aBuf.readBoolean();
strong = aBuf.readBoolean();
threshold = aBuf.readInt();
return this;
}
}
}
|