-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathMetaTileEntityFluidHatch.java
More file actions
458 lines (406 loc) · 18.3 KB
/
Copy pathMetaTileEntityFluidHatch.java
File metadata and controls
458 lines (406 loc) · 18.3 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
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
package gregtech.common.metatileentities.multi.multiblockpart;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.IFilter;
import gregtech.api.capability.IFilteredFluidContainer;
import gregtech.api.capability.IGhostSlotConfigurable;
import gregtech.api.capability.impl.FilteredItemHandler;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.GhostCircuitItemStackHandler;
import gregtech.api.capability.impl.NotifiableFluidTank;
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.AbilityInstances;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.metatileentity.multiblock.MultiblockControllerBase;
import gregtech.api.mui.GTGuiTextures;
import gregtech.api.mui.GTGuis;
import gregtech.api.mui.widget.GhostCircuitSlotWidget;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.cube.SimpleOverlayRenderer;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumTank;
import gregtech.common.mui.widget.GTFluidSlot;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.factory.PosGuiData;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.widgets.ItemSlot;
import com.cleanroommc.modularui.widgets.RichTextWidget;
import com.cleanroommc.modularui.widgets.ToggleButton;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MetaTileEntityFluidHatch extends MetaTileEntityMultiblockNotifiablePart
implements IMultiblockAbilityPart<IFluidTank>, IControllable,
IGhostSlotConfigurable {
public static final int INITIAL_INVENTORY_SIZE = 8000;
// only holding this for convenience
protected final HatchFluidTank fluidTank;
protected boolean workingEnabled;
private GhostCircuitItemStackHandler circuitInventory;
// export hatch-only fields
protected boolean locked;
@Nullable
protected FluidStack lockedFluid;
public MetaTileEntityFluidHatch(ResourceLocation metaTileEntityId, int tier, boolean isExportHatch) {
super(metaTileEntityId, tier, isExportHatch);
this.fluidTank = new HatchFluidTank(getInventorySize(), this, isExportHatch);
initializeInventory(); // the fact that this has to be called three times is so dumb
this.workingEnabled = true;
}
@Override
protected void initializeInventory() {
super.initializeInventory();
if (this.hasGhostCircuitInventory()) {
this.circuitInventory = new GhostCircuitItemStackHandler(this);
this.circuitInventory.addNotifiableMetaTileEntity(this);
}
}
@Override
public boolean hasGhostCircuitInventory() {
return !isExportHatch;
}
@Override
public void setGhostCircuitConfig(int config) {
if (this.circuitInventory == null || this.circuitInventory.getCircuitValue() == config) {
return;
}
this.circuitInventory.setCircuitValue(config);
if (!getWorld().isRemote) {
markDirty();
}
}
@Override
public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityFluidHatch(metaTileEntityId, getTier(), isExportHatch);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setBoolean("workingEnabled", workingEnabled);
if (isExportHatch) {
data.setBoolean("IsLocked", locked);
if (locked && lockedFluid != null) {
data.setTag("LockedFluid", lockedFluid.writeToNBT(new NBTTagCompound()));
}
} else {
this.circuitInventory.write(data);
}
return data;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("workingEnabled")) {
this.workingEnabled = data.getBoolean("workingEnabled");
}
if (data.hasKey("ContainerInventory")) {
MetaTileEntityQuantumTank.legacyTankItemHandlerNBTReading(this, data.getCompoundTag("ContainerInventory"),
0, 1);
}
if (isExportHatch) {
this.locked = data.getBoolean("IsLocked");
this.lockedFluid = this.locked ? FluidStack.loadFluidStackFromNBT(data.getCompoundTag("LockedFluid")) :
null;
} else {
this.circuitInventory.read(data);
}
}
@Override
public void update() {
super.update();
if (!getWorld().isRemote) {
if (workingEnabled) {
fillContainerFromInternalTank(fluidTank);
if (isExportHatch) {
pushFluidsIntoNearbyHandlers(getFrontFacing());
} else {
fillInternalTankFromFluidContainer(fluidTank);
pullFluidsFromNearbyHandlers(getFrontFacing());
}
}
}
}
@Override
public void setWorkingEnabled(boolean workingEnabled) {
this.workingEnabled = workingEnabled;
World world = getWorld();
if (world != null && !world.isRemote) {
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(workingEnabled));
}
}
@Override
public boolean isWorkingEnabled() {
return workingEnabled;
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing side) {
if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
return super.getCapability(capability, side);
}
@Override
public void writeInitialSyncData(PacketBuffer buf) {
super.writeInitialSyncData(buf);
buf.writeBoolean(workingEnabled);
if (isExportHatch) {
buf.writeBoolean(locked);
} else {
buf.writeVarInt(this.circuitInventory.getCircuitValue());
}
}
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
super.receiveInitialSyncData(buf);
this.workingEnabled = buf.readBoolean();
if (isExportHatch) {
this.locked = buf.readBoolean();
} else {
setGhostCircuitConfig(buf.readVarInt());
}
}
@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
super.receiveCustomData(dataId, buf);
if (dataId == GregtechDataCodes.WORKING_ENABLED) {
this.workingEnabled = buf.readBoolean();
} else if (dataId == GregtechDataCodes.LOCK_FILL) {
this.lockedFluid = NetworkUtils.readFluidStack(buf);
}
}
@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
super.renderMetaTileEntity(renderState, translation, pipeline);
if (shouldRenderOverlay()) {
SimpleOverlayRenderer renderer = isExportHatch ? Textures.PIPE_OUT_OVERLAY : Textures.PIPE_IN_OVERLAY;
renderer.renderSided(getFrontFacing(), renderState, translation, pipeline);
SimpleOverlayRenderer overlay = isExportHatch ? Textures.FLUID_HATCH_OUTPUT_OVERLAY :
Textures.FLUID_HATCH_INPUT_OVERLAY;
overlay.renderSided(getFrontFacing(), renderState, translation, pipeline);
}
}
protected int getInventorySize() {
return INITIAL_INVENTORY_SIZE * Math.min(Integer.MAX_VALUE, 1 << getTier());
}
@Override
protected FluidTankList createImportFluidHandler() {
return isExportHatch ? new FluidTankList(false) : new FluidTankList(false, fluidTank);
}
@Override
protected FluidTankList createExportFluidHandler() {
return isExportHatch ? new FluidTankList(false, fluidTank) : new FluidTankList(false);
}
@Override
protected IItemHandlerModifiable createImportItemHandler() {
return new FilteredItemHandler(this, 1).setFillPredicate(
FilteredItemHandler.getCapabilityFilter(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY));
}
@Override
protected IItemHandlerModifiable createExportItemHandler() {
return new GTItemStackHandler(this, 1);
}
@Override
public @NotNull List<MultiblockAbility<?>> getAbilities() {
return isExportHatch ?
Collections.singletonList(MultiblockAbility.EXPORT_FLUIDS) :
Arrays.asList(MultiblockAbility.IMPORT_FLUIDS, MultiblockAbility.IMPORT_ITEMS);
}
@Override
public void registerAbilities(@NotNull AbilityInstances abilityInstances) {
if (abilityInstances.isKey(MultiblockAbility.EXPORT_FLUIDS) ||
abilityInstances.isKey(MultiblockAbility.IMPORT_FLUIDS)) {
abilityInstances.add(this.fluidTank);
} else if (abilityInstances.isKey(MultiblockAbility.IMPORT_ITEMS)) {
abilityInstances.add(this.circuitInventory);
}
}
@Override
public boolean usesMui2() {
return true;
}
@Override
public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager) {
var fluidSyncHandler = GTFluidSlot.sync(fluidTank)
.showAmount(false)
.accessibility(true, !isExportHatch)
.handleLocking(() -> this.lockedFluid, fluidStack -> {
setLocked(fluidStack != null);
this.lockedFluid = fluidStack;
this.fluidTank.onContentsChanged();
}, this::setLocked);
return GTGuis.createPanel(this, 176, 166)
.child(IKey.lang(getMetaFullName()).asWidget().pos(6, 6))
// export specific
.childIf(isExportHatch, new ItemSlot()
.pos(90, 44)
.background(GTGuiTextures.SLOT, GTGuiTextures.OUT_SLOT_OVERLAY)
.slot(new ModularSlot(exportItems, 0)
.accessibility(false, true)))
.childIf(isExportHatch, new ToggleButton()
.pos(7, 63)
.overlay(GTGuiTextures.BUTTON_LOCK)
.value(new BooleanSyncValue(this::isLocked, b -> fluidSyncHandler.lockFluid(b, false)))
.addTooltip(true, IKey.lang("gregtech.gui.fluid_lock.tooltip.enabled"))
.addTooltip(false, IKey.lang("gregtech.gui.fluid_lock.tooltip.disabled")))
// import specific
.childIf(!isExportHatch, GTGuiTextures.TANK_ICON.asWidget()
.pos(91, 36)
.size(14, 15))
.childIf(!isExportHatch, new ItemSlot()
.pos(90, 53)
.background(GTGuiTextures.SLOT, GTGuiTextures.OUT_SLOT_OVERLAY)
.slot(new ModularSlot(exportItems, 0)
.accessibility(false, true)))
.childIf(!isExportHatch, new GhostCircuitSlotWidget()
.slot(circuitInventory, 0)
.background(GTGuiTextures.SLOT, GTGuiTextures.INT_CIRCUIT_OVERLAY)
.pos(124, 62))
// common ui
.child(new RichTextWidget()
.size(81 - 6, (isExportHatch ? 46 : 55) - 8)
// .padding(3, 4)
.background(GTGuiTextures.DISPLAY.asIcon().size(81, isExportHatch ? 46 : 55))
.pos(7 + 3, 16 + 4)
.textColor(Color.WHITE.main)
.alignment(Alignment.TopLeft)
.autoUpdate(true)
.textBuilder(richText -> {
richText.addLine(IKey.lang("gregtech.gui.fluid_amount"));
String name = fluidSyncHandler.getFluidLocalizedName();
if (name == null) return;
if (name.length() > 25) name = name.substring(0, 25) + "...";
richText.addLine(IKey.str(name));
richText.addLine(IKey.str(fluidSyncHandler.getFormattedFluidAmount()));
}))
.child(new GTFluidSlot()
.disableBackground()
.pos(69, isExportHatch ? 43 : 52)
.size(18)
.syncHandler(fluidSyncHandler))
.child(new ItemSlot()
.pos(90, 16)
.background(GTGuiTextures.SLOT, GTGuiTextures.IN_SLOT_OVERLAY)
.slot(new ModularSlot(importItems, 0)
.singletonSlotGroup()
.filter(stack -> {
if (!isExportHatch) return true;
var h = FluidUtil.getFluidHandler(stack);
if (h == null) return false;
return h.getTankProperties()[0].getContents() == null;
})
.accessibility(true, true)))
.bindPlayerInventory();
}
@Override
public void addToMultiBlock(MultiblockControllerBase controllerBase) {
super.addToMultiBlock(controllerBase);
if (hasGhostCircuitInventory())
this.circuitInventory.addNotifiableMetaTileEntity(controllerBase);
}
@Override
public void removeFromMultiBlock(MultiblockControllerBase controllerBase) {
super.removeFromMultiBlock(controllerBase);
if (hasGhostCircuitInventory())
this.circuitInventory.removeNotifiableMetaTileEntity(controllerBase);
}
@Override
public void addInformation(ItemStack stack, @Nullable World player, @NotNull List<String> tooltip,
boolean advanced) {
if (this.isExportHatch)
tooltip.add(I18n.format("gregtech.machine.fluid_hatch.export.tooltip"));
else
tooltip.add(I18n.format("gregtech.machine.fluid_hatch.import.tooltip"));
tooltip.add(I18n.format("gregtech.universal.tooltip.fluid_storage_capacity", getInventorySize()));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}
@Override
public void addToolUsages(ItemStack stack, @Nullable World world, List<String> tooltip, boolean advanced) {
tooltip.add(I18n.format("gregtech.tool_action.screwdriver.access_covers"));
tooltip.add(I18n.format("gregtech.tool_action.wrench.set_facing"));
super.addToolUsages(stack, world, tooltip, advanced);
}
private boolean isLocked() {
return this.locked;
}
private void setLocked(boolean locked) {
if (!isExportHatch || this.locked == locked) return;
this.locked = locked;
if (!getWorld().isRemote) markDirty();
if (locked && fluidTank.getFluid() != null) {
this.lockedFluid = fluidTank.getFluid().copy();
this.lockedFluid.amount = 1;
fluidTank.onContentsChanged();
return;
}
this.lockedFluid = null;
fluidTank.onContentsChanged();
}
protected class HatchFluidTank extends NotifiableFluidTank implements IFilteredFluidContainer, IFilter<FluidStack> {
public HatchFluidTank(int capacity, MetaTileEntity entityToNotify, boolean isExport) {
super(capacity, entityToNotify, isExport);
}
@Override
public int fillInternal(FluidStack resource, boolean doFill) {
int accepted = super.fillInternal(resource, doFill);
if (!isExportHatch) return accepted;
if (doFill && locked && lockedFluid == null) {
lockedFluid = resource.copy();
lockedFluid.amount = 1;
writeCustomData(GregtechDataCodes.LOCK_FILL,
buffer -> NetworkUtils.writeFluidStack(buffer, lockedFluid));
}
return accepted;
}
@Override
public boolean canFillFluidType(FluidStack fluid) {
return test(fluid);
}
// override for visibility
@Override
public void onContentsChanged() {
super.onContentsChanged();
}
@Nullable
@Override
public IFilter<FluidStack> getFilter() {
return this;
}
@Override
public boolean test(@NotNull FluidStack fluidStack) {
if (!isExportHatch) return true;
return !locked || lockedFluid == null || fluidStack.isFluidEqual(lockedFluid);
}
@Override
public int getPriority() {
if (!isExportHatch) return IFilter.noPriority();
return !locked || lockedFluid == null ? IFilter.noPriority() : IFilter.whitelistPriority(1);
}
}
}