Skip to content

Commit eb9230e

Browse files
authored
Port passthrough hatches to MUI2 and allow them to be togged (#2504)
1 parent c35fcb2 commit eb9230e

File tree

3 files changed

+198
-46
lines changed

3 files changed

+198
-46
lines changed

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityPassthroughHatchFluid.java

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package gregtech.common.metatileentities.multi.multiblockpart;
22

3+
import gregtech.api.capability.GregtechDataCodes;
4+
import gregtech.api.capability.GregtechTileCapabilities;
5+
import gregtech.api.capability.IControllable;
36
import gregtech.api.capability.impl.FilteredFluidHandler;
47
import gregtech.api.capability.impl.FluidHandlerProxy;
58
import gregtech.api.capability.impl.FluidTankList;
69
import gregtech.api.capability.impl.NotifiableItemStackHandler;
7-
import gregtech.api.gui.GuiTextures;
8-
import gregtech.api.gui.ModularUI;
9-
import gregtech.api.gui.widgets.TankWidget;
1010
import gregtech.api.metatileentity.MetaTileEntity;
1111
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
1212
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
1313
import gregtech.api.metatileentity.multiblock.IPassthroughHatch;
1414
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
15+
import gregtech.api.mui.GTGuiTextures;
16+
import gregtech.api.mui.GTGuis;
1517
import gregtech.client.renderer.texture.Textures;
18+
import gregtech.common.mui.widget.GTFluidSlot;
1619

1720
import net.minecraft.client.resources.I18n;
18-
import net.minecraft.entity.player.EntityPlayer;
1921
import net.minecraft.item.ItemStack;
2022
import net.minecraft.nbt.NBTTagCompound;
23+
import net.minecraft.network.PacketBuffer;
2124
import net.minecraft.util.EnumFacing;
2225
import net.minecraft.util.ResourceLocation;
2326
import net.minecraft.world.World;
@@ -29,13 +32,24 @@
2932
import codechicken.lib.render.CCRenderState;
3033
import codechicken.lib.render.pipeline.IVertexOperation;
3134
import codechicken.lib.vec.Matrix4;
35+
import com.cleanroommc.modularui.api.drawable.IKey;
36+
import com.cleanroommc.modularui.api.widget.IWidget;
37+
import com.cleanroommc.modularui.factory.PosGuiData;
38+
import com.cleanroommc.modularui.screen.ModularPanel;
39+
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
40+
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
41+
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
42+
import com.cleanroommc.modularui.widgets.ToggleButton;
43+
import com.cleanroommc.modularui.widgets.layout.Grid;
3244
import org.jetbrains.annotations.NotNull;
3345
import org.jetbrains.annotations.Nullable;
3446

47+
import java.util.ArrayList;
3548
import java.util.List;
3649

3750
public class MetaTileEntityPassthroughHatchFluid extends MetaTileEntityMultiblockPart implements IPassthroughHatch,
38-
IMultiblockAbilityPart<IPassthroughHatch> {
51+
IMultiblockAbilityPart<IPassthroughHatch>,
52+
IControllable {
3953

4054
private static final int TANK_SIZE = 16_000;
4155

@@ -44,8 +58,11 @@ public class MetaTileEntityPassthroughHatchFluid extends MetaTileEntityMultibloc
4458
private IFluidHandler importHandler;
4559
private IFluidHandler exportHandler;
4660

61+
private boolean workingEnabled;
62+
4763
public MetaTileEntityPassthroughHatchFluid(ResourceLocation metaTileEntityId, int tier) {
4864
super(metaTileEntityId, tier);
65+
this.workingEnabled = true;
4966
initializeInventory();
5067
}
5168

@@ -70,11 +87,25 @@ protected void initializeInventory() {
7087
public void update() {
7188
super.update();
7289
if (!getWorld().isRemote && getOffsetTimer() % 5 == 0) {
73-
pushFluidsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
74-
pullFluidsFromNearbyHandlers(getFrontFacing()); // inputs from front
90+
if (workingEnabled) {
91+
pushFluidsIntoNearbyHandlers(getFrontFacing().getOpposite()); // outputs to back
92+
pullFluidsFromNearbyHandlers(getFrontFacing()); // inputs from front
93+
}
94+
}
95+
}
96+
97+
public void setWorkingEnabled(boolean workingEnabled) {
98+
this.workingEnabled = workingEnabled;
99+
World world = getWorld();
100+
if (world != null && !world.isRemote) {
101+
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(workingEnabled));
75102
}
76103
}
77104

105+
public boolean isWorkingEnabled() {
106+
return this.workingEnabled;
107+
}
108+
78109
@Override
79110
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
80111
super.renderMetaTileEntity(renderState, translation, pipeline);
@@ -101,40 +132,62 @@ protected IItemHandlerModifiable createImportItemHandler() {
101132
}
102133

103134
@Override
104-
protected ModularUI createUI(EntityPlayer entityPlayer) {
135+
public boolean usesMui2() {
136+
return true;
137+
}
138+
139+
@Override
140+
public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager) {
105141
int rowSize = (int) Math.sqrt(getTier() + 1);
106-
return createUITemplate(entityPlayer, rowSize)
107-
.build(getHolder(), entityPlayer);
108-
}
109-
110-
private ModularUI.Builder createUITemplate(EntityPlayer player, int rowSize) {
111-
ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, 18 + 18 * rowSize + 94)
112-
.label(6, 6, getMetaFullName());
113-
114-
for (int y = 0; y < rowSize; y++) {
115-
for (int x = 0; x < rowSize; x++) {
116-
int index = y * rowSize + x;
117-
builder.widget(
118-
new TankWidget(fluidTankList.getTankAt(index), 89 - rowSize * 9 + x * 18, 18 + y * 18, 18, 18)
119-
.setBackgroundTexture(GuiTextures.FLUID_SLOT)
120-
.setContainerClicking(true, true)
121-
.setAlwaysShowFull(true));
142+
143+
int backgroundWidth = 9 * 18 + 14;
144+
int backgroundHeight = 18 + 18 * rowSize + 94;
145+
146+
List<List<IWidget>> widgets = new ArrayList<>();
147+
for (int i = 0; i < rowSize; i++) {
148+
widgets.add(new ArrayList<>());
149+
for (int j = 0; j < rowSize; j++) {
150+
widgets.get(i).add(new GTFluidSlot().syncHandler(fluidTankList.getTankAt(i * rowSize + j))
151+
.background(GTGuiTextures.FLUID_SLOT));
122152
}
123153
}
124-
return builder.bindPlayerInventory(player.inventory, GuiTextures.SLOT, 7, 18 + 18 * rowSize + 12);
154+
155+
BooleanSyncValue workingStateValue = new BooleanSyncValue(() -> workingEnabled, val -> workingEnabled = val);
156+
return GTGuis.createPanel(this, backgroundWidth, backgroundHeight)
157+
.child(IKey.lang(getMetaFullName()).asWidget().pos(5, 5))
158+
.child(SlotGroupWidget.playerInventory().left(7).bottom(7))
159+
.child(new Grid()
160+
.top(18).height(rowSize * 18)
161+
.minElementMargin(0, 0)
162+
.minColWidth(18).minRowHeight(18)
163+
.alignX(0.5f)
164+
.matrix(widgets))
165+
.child(new ToggleButton()
166+
.top(18 * 2).left(18 * 8 + 7)
167+
.value(workingStateValue)
168+
.overlay(GTGuiTextures.BUTTON_FLUID_OUTPUT)
169+
.tooltip(t -> t.setAutoUpdate(true))
170+
.tooltipBuilder(t -> t.addLine(workingStateValue.getBoolValue() ?
171+
IKey.lang("gregtech.gui.fluid_passthrough.enabled") :
172+
IKey.lang("gregtech.gui.fluid_passthrough.disabled"))));
125173
}
126174

127175
@Override
128176
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
129177
super.writeToNBT(tag);
130178
tag.setTag("FluidInventory", fluidTankList.serializeNBT());
179+
tag.setBoolean("WorkingEnabled", workingEnabled);
131180
return tag;
132181
}
133182

134183
@Override
135184
public void readFromNBT(NBTTagCompound tag) {
136185
super.readFromNBT(tag);
137186
this.fluidTankList.deserializeNBT(tag.getCompoundTag("FluidInventory"));
187+
// Passthrough hatches before this change won't have WorkingEnabled at all, so we need to check if it exists
188+
if (tag.hasKey("WorkingEnabled")) {
189+
this.workingEnabled = tag.getBoolean("WorkingEnabled");
190+
}
138191
}
139192

140193
@Override
@@ -174,7 +227,23 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
174227
} else if (side == getFrontFacing().getOpposite()) {
175228
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(exportHandler);
176229
} else return null;
230+
} else if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
231+
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
177232
}
178233
return super.getCapability(capability, side);
179234
}
235+
236+
@Override
237+
public void writeInitialSyncData(PacketBuffer buf) {
238+
super.writeInitialSyncData(buf);
239+
240+
buf.writeBoolean(workingEnabled);
241+
}
242+
243+
@Override
244+
public void receiveInitialSyncData(PacketBuffer buf) {
245+
super.receiveInitialSyncData(buf);
246+
247+
this.workingEnabled = buf.readBoolean();
248+
}
180249
}

0 commit comments

Comments
 (0)