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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
package gregtech.api.metatileentity.implementations;
import static gregtech.api.enums.Textures.BlockIcons.FLUID_OUT_SIGN;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_PIPE_OUT;
import java.lang.ref.WeakReference;
import javax.annotation.Nonnull;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import com.gtnewhorizons.modularui.api.math.Alignment;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.TextWidget;
import gregtech.GTMod;
import gregtech.api.gui.modularui.GTUIInfos;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.fluid.IFluidStore;
import gregtech.api.interfaces.metatileentity.IFluidLockable;
import gregtech.api.interfaces.modularui.IAddUIWidgets;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTModHandler;
import gregtech.api.util.GTUtility;
import gregtech.common.gui.modularui.widget.FluidLockWidget;
public class MTEHatchOutput extends MTEHatch implements IFluidStore, IFluidLockable, IAddUIWidgets {
private String lockedFluidName = null;
private WeakReference<EntityPlayer> playerThatLockedfluid = null;
public byte mMode = 0;
public MTEHatchOutput(int aID, String aName, String aNameRegional, int aTier) {
super(
aID,
aName,
aNameRegional,
aTier,
4,
new String[] { "Fluid Output for Multiblocks",
"Capacity: " + GTUtility.formatNumbers(8000L * (1L << aTier)) + "L",
"Right click with screwdriver to restrict output",
"Can be restricted to put out Items and/or Steam/No Steam/1 specific Fluid",
"Restricted Output Hatches are given priority for Multiblock Fluid output" });
}
public MTEHatchOutput(String aName, int aTier, String aDescription, ITexture[][][] aTextures) {
super(aName, aTier, 4, aDescription, aTextures);
}
public MTEHatchOutput(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
super(aName, aTier, 4, aDescription, aTextures);
}
public MTEHatchOutput(int aID, String aName, String aNameRegional, int aTier, String[] aDescription,
int inventorySize) {
super(aID, aName, aNameRegional, aTier, inventorySize, aDescription);
}
public MTEHatchOutput(String name, int tier, int slots, String[] description, ITexture[][][] textures) {
super(name, tier, slots, description, textures);
}
@Override
public ITexture[] getTexturesActive(ITexture aBaseTexture) {
return GTMod.gregtechproxy.mRenderIndicatorsOnHatch
? new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_PIPE_OUT), TextureFactory.of(FLUID_OUT_SIGN) }
: new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_PIPE_OUT) };
}
@Override
public ITexture[] getTexturesInactive(ITexture aBaseTexture) {
return GTMod.gregtechproxy.mRenderIndicatorsOnHatch
? new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_PIPE_OUT), TextureFactory.of(FLUID_OUT_SIGN) }
: new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_PIPE_OUT) };
}
@Override
public boolean isSimpleMachine() {
return true;
}
@Override
public boolean isFacingValid(ForgeDirection facing) {
return true;
}
@Override
public boolean isAccessAllowed(EntityPlayer aPlayer) {
return true;
}
@Override
public boolean isLiquidInput(ForgeDirection side) {
return false;
}
@Override
public MetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTEHatchOutput(mName, mTier, mDescriptionArray, mTextures);
}
@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
GTUIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer);
return true;
}
@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
super.onPostTick(aBaseMetaTileEntity, aTick);
if (aBaseMetaTileEntity.isServerSide() && aBaseMetaTileEntity.isAllowedToWork() && mFluid != null) {
IFluidHandler tTileEntity = aBaseMetaTileEntity
.getITankContainerAtSide(aBaseMetaTileEntity.getFrontFacing());
if (tTileEntity != null) {
GTUtility.moveFluid(
aBaseMetaTileEntity,
tTileEntity,
aBaseMetaTileEntity.getFrontFacing(),
Math.max(1, mFluid.amount),
null);
}
}
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
aNBT.setByte("mMode", mMode);
if (isFluidLocked() && lockedFluidName != null && !lockedFluidName.isEmpty())
aNBT.setString("lockedFluidName", lockedFluidName);
else aNBT.removeTag("lockedFluidName");
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
mMode = aNBT.getByte("mMode");
if (isFluidLocked()) {
lockedFluidName = aNBT.getString("lockedFluidName");
}
lockedFluidName = GTUtility.isStringInvalid(lockedFluidName) ? null : lockedFluidName;
}
@Override
public boolean doesFillContainers() {
return true;
}
@Override
public boolean doesEmptyContainers() {
return false;
}
@Override
public boolean canTankBeFilled() {
return true;
}
@Override
public boolean canTankBeEmptied() {
return true;
}
@Override
public boolean displaysItemStack() {
return true;
}
@Override
public boolean displaysStackSize() {
return false;
}
public int getLockedDisplaySlot() {
return 3;
}
@Override
public boolean isValidSlot(int aIndex) {
// Because getStackDisplaySlot() only allow return one int, this place I only can manually set.
return aIndex != getStackDisplaySlot() && aIndex != getLockedDisplaySlot();
}
@Override
public boolean allowPullStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
return side == aBaseMetaTileEntity.getFrontFacing() && aIndex == 1;
}
@Override
public boolean allowPutStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
return side == aBaseMetaTileEntity.getFrontFacing() && aIndex == 0;
}
@Override
public int getCapacity() {
return 8000 * (1 << mTier);
}
@Override
public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (!getBaseMetaTileEntity().getCoverInfoAtSide(side)
.isGUIClickable()) return;
if (aPlayer.isSneaking()) {
mMode = (byte) ((mMode + 9) % 10);
} else {
mMode = (byte) ((mMode + 1) % 10);
}
final String inBrackets;
switch (mMode) {
case 0 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("108", "Outputs misc. Fluids, Steam and Items"));
this.setLockedFluidName(null);
}
case 1 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("109", "Outputs Steam and Items"));
this.setLockedFluidName(null);
}
case 2 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("110", "Outputs Steam and misc. Fluids"));
this.setLockedFluidName(null);
}
case 3 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("111", "Outputs Steam"));
this.setLockedFluidName(null);
}
case 4 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("112", "Outputs misc. Fluids and Items"));
this.setLockedFluidName(null);
}
case 5 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("113", "Outputs only Items"));
this.setLockedFluidName(null);
}
case 6 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("114", "Outputs only misc. Fluids"));
this.setLockedFluidName(null);
}
case 7 -> {
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("115", "Outputs nothing"));
this.setLockedFluidName(null);
}
case 8 -> {
playerThatLockedfluid = new WeakReference<>(aPlayer);
if (mFluid == null) {
this.setLockedFluidName(null);
inBrackets = GTUtility.trans(
"115.3",
"currently none, will be locked to the next that is put in (or use fluid cell to lock)");
} else {
this.setLockedFluidName(
this.getDrainableStack()
.getFluid()
.getName());
inBrackets = this.getDrainableStack()
.getLocalizedName();
}
GTUtility.sendChatToPlayer(
aPlayer,
String
.format("%s (%s)", GTUtility.trans("151.1", "Outputs items and 1 specific Fluid"), inBrackets));
}
case 9 -> {
playerThatLockedfluid = new WeakReference<>(aPlayer);
if (mFluid == null) {
this.setLockedFluidName(null);
inBrackets = GTUtility.trans(
"115.3",
"currently none, will be locked to the next that is put in (or use fluid cell to lock)");
} else {
this.setLockedFluidName(
this.getDrainableStack()
.getFluid()
.getName());
inBrackets = this.getDrainableStack()
.getLocalizedName();
}
GTUtility.sendChatToPlayer(
aPlayer,
String.format("%s (%s)", GTUtility.trans("151.2", "Outputs 1 specific Fluid"), inBrackets));
}
}
}
private boolean tryToLockHatch(EntityPlayer aPlayer, ForgeDirection side) {
if (!getBaseMetaTileEntity().getCoverInfoAtSide(side)
.isGUIClickable()) return false;
if (!isFluidLocked()) return false;
final ItemStack tCurrentItem = aPlayer.inventory.getCurrentItem();
if (tCurrentItem == null) return false;
FluidStack tFluid = FluidContainerRegistry.getFluidForFilledItem(tCurrentItem);
if (tFluid == null && tCurrentItem.getItem() instanceof IFluidContainerItem)
tFluid = ((IFluidContainerItem) tCurrentItem.getItem()).getFluid(tCurrentItem);
if (tFluid != null) {
if (getLockedFluidName() != null && !getLockedFluidName().equals(
tFluid.getFluid()
.getName())) {
GTUtility.sendChatToPlayer(
aPlayer,
String.format(
"%s %s",
GTUtility.trans(
"151.3",
"Hatch is locked to a different fluid. To change the locking, empty it and made it locked to the next fluid with a screwdriver. Currently locked to"),
StatCollector.translateToLocal(getLockedFluidName())));
} else {
setLockedFluidName(
tFluid.getFluid()
.getName());
if (mMode == 8) GTUtility.sendChatToPlayer(
aPlayer,
String.format(
"%s (%s)",
GTUtility.trans("151.1", "Outputs items and 1 specific Fluid"),
tFluid.getLocalizedName()));
else GTUtility.sendChatToPlayer(
aPlayer,
String.format(
"%s (%s)",
GTUtility.trans("151.2", "Outputs 1 specific Fluid"),
tFluid.getLocalizedName()));
}
return true;
}
return false;
}
public byte getMode() {
return mMode;
}
@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer, ForgeDirection side,
float aX, float aY, float aZ) {
if (tryToLockHatch(aPlayer, side)) return true;
return super.onRightclick(aBaseMetaTileEntity, aPlayer, side, aX, aY, aZ);
}
public boolean outputsSteam() {
return mMode < 4;
}
public boolean outputsLiquids() {
return mMode % 2 == 0 || mMode == 9;
}
public boolean outputsItems() {
return mMode % 4 < 2 && mMode != 9;
}
@Override
public String getLockedFluidName() {
return lockedFluidName;
}
@Override
public void setLockedFluidName(String lockedFluidName) {
this.lockedFluidName = lockedFluidName;
markDirty();
}
@Override
public void lockFluid(boolean lock) {
if (lock) {
if (!isFluidLocked()) {
this.mMode = 9;
markDirty();
}
} else {
this.mMode = 0;
setLockedFluidName(null);
markDirty();
}
}
@Override
public boolean isFluidLocked() {
return mMode == 8 || mMode == 9;
}
@Override
public boolean acceptsFluidLock(String name) {
return true;
}
@Override
public boolean isEmptyAndAcceptsAnyFluid() {
return mMode == 0 && getFluidAmount() == 0;
}
@Override
public boolean canStoreFluid(@Nonnull FluidStack fluidStack) {
if (mFluid != null && !GTUtility.areFluidsEqual(mFluid, fluidStack)) {
return false;
}
if (isFluidLocked()) {
if (lockedFluidName == null) {
return true;
}
return lockedFluidName.equals(
fluidStack.getFluid()
.getName());
}
if (GTModHandler.isSteam(fluidStack)) {
return outputsSteam();
}
return outputsLiquids();
}
@Override
public int getTankPressure() {
return +100;
}
@Override
protected void onEmptyingContainerWhenEmpty() {
if (this.lockedFluidName == null && this.mFluid != null && isFluidLocked()) {
this.setLockedFluidName(
this.mFluid.getFluid()
.getName());
final EntityPlayer player;
if (playerThatLockedfluid == null || (player = playerThatLockedfluid.get()) == null) return;
GTUtility.sendChatToPlayer(
player,
String.format(GTUtility.trans("151.4", "Successfully locked Fluid to %s"), mFluid.getLocalizedName()));
playerThatLockedfluid = null;
}
}
@Override
public boolean isGivingInformation() {
return true;
}
@Override
public String[] getInfoData() {
return new String[] { EnumChatFormatting.BLUE + "Output Hatch" + EnumChatFormatting.RESET, "Stored Fluid:",
EnumChatFormatting.GOLD + (mFluid == null ? "No Fluid" : mFluid.getLocalizedName())
+ EnumChatFormatting.RESET,
EnumChatFormatting.GREEN + GTUtility.formatNumbers(mFluid == null ? 0 : mFluid.amount)
+ " L"
+ EnumChatFormatting.RESET
+ " "
+ EnumChatFormatting.YELLOW
+ GTUtility.formatNumbers(getCapacity())
+ " L"
+ EnumChatFormatting.RESET,
(!isFluidLocked() || lockedFluidName == null) ? "Not Locked"
: ("Locked to " + StatCollector.translateToLocal(
FluidRegistry.getFluidStack(lockedFluidName, 1)
.getUnlocalizedName())) };
}
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
super.addUIWidgets(builder, buildContext);
builder.widget(
new DrawableWidget().setDrawable(GTUITextures.PICTURE_SCREEN_BLACK)
.setPos(98, 16)
.setSize(71, 45))
.widget(new FluidLockWidget(this).setPos(149, 41))
.widget(
new TextWidget("Locked Fluid").setDefaultColor(COLOR_TEXT_WHITE.get())
.setPos(101, 20))
.widget(TextWidget.dynamicString(() -> {
FluidStack fluidStack = FluidRegistry.getFluidStack(lockedFluidName, 1);
return fluidStack != null ? fluidStack.getLocalizedName() : "None";
})
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setTextAlignment(Alignment.CenterLeft)
.setMaxWidth(65)
.setPos(101, 30))
.widget(new FakeSyncWidget.ByteSyncer(() -> mMode, val -> mMode = val));
}
}
|