Skip to content

Commit be67af5

Browse files
committed
fix issues with GTFluidSlot and cell behavior
sync changes to tank fluid automatically work on supporting phantom fluid slot
1 parent d20caf9 commit be67af5

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

src/main/java/gregtech/api/mui/GTGuis.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import net.minecraft.item.ItemStack;
1111

12-
import com.cleanroommc.modularui.api.widget.Interactable;
1312
import com.cleanroommc.modularui.factory.GuiManager;
1413
import com.cleanroommc.modularui.screen.ModularPanel;
1514
import com.cleanroommc.modularui.utils.Alignment;
@@ -86,7 +85,7 @@ public static ModularPanel defaultPopupPanel(String name) {
8685
}
8786

8887
public static ModularPanel defaultPopupPanel(String name, boolean disableBelow,
89-
boolean closeOnOutsideClick) {
88+
boolean closeOnOutsideClick) {
9089
return new PopupPanel(name, DEFAULT_WIDTH, DEFAULT_HIEGHT, disableBelow, closeOnOutsideClick);
9190
}
9291

src/main/java/gregtech/api/mui/sync/GTFluidSyncHandler.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package gregtech.api.mui.sync;
22

3+
import com.cleanroommc.modularui.utils.MouseData;
4+
35
import gregtech.api.util.GTUtility;
46

57
import net.minecraft.entity.item.EntityItem;
@@ -9,6 +11,7 @@
911
import net.minecraft.util.SoundCategory;
1012
import net.minecraft.util.SoundEvent;
1113
import net.minecraftforge.fluids.FluidStack;
14+
import net.minecraftforge.fluids.FluidTank;
1215
import net.minecraftforge.fluids.IFluidTank;
1316
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
1417
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
@@ -19,21 +22,51 @@
1922

2023
public class GTFluidSyncHandler extends SyncHandler {
2124

22-
private static final int TRY_CLICK_CONTAINER = 1;
23-
private static final int UPDATE_TANK = 2;
25+
public static final int TRY_CLICK_CONTAINER = 1;
26+
public static final int UPDATE_TANK = 2;
27+
public static final int UPDATE_AMOUNT = 3;
2428

2529
private final IFluidTank tank;
30+
private FluidStack lastFluid;
31+
private FluidStack lockedFluid;
2632
private boolean canDrainSlot = true;
2733
private boolean canFillSlot = true;
34+
private boolean phantom;
2835

2936
public GTFluidSyncHandler(IFluidTank tank) {
3037
this.tank = tank;
3138
}
3239

40+
@Override
41+
public void detectAndSendChanges(boolean init) {
42+
var current = getFluid();
43+
if (current == null && lastFluid == null) return;
44+
if (current == null || lastFluid == null || lastFluid.getFluid() != current.getFluid()) {
45+
lastFluid = current == null ? null : current.copy();
46+
syncToClient(UPDATE_TANK, buffer -> NetworkUtils.writeFluidStack(buffer, current));
47+
} else if (current.amount != lastFluid.amount) {
48+
syncToClient(UPDATE_AMOUNT, buffer -> buffer.writeInt(current.amount));
49+
}
50+
}
51+
3352
public FluidStack getFluid() {
3453
return this.tank.getFluid();
3554
}
3655

56+
public void setFluid(FluidStack fluid) {
57+
if (tank instanceof FluidTank fluidTank) {
58+
fluidTank.setFluid(fluid);
59+
} else {
60+
tank.drain(Integer.MAX_VALUE, true);
61+
tank.fill(fluid, true);
62+
}
63+
}
64+
65+
public void setAmount(int amount) {
66+
if (getFluid() == null) return;
67+
getFluid().amount = amount;
68+
}
69+
3770
public int getCapacity() {
3871
return this.tank.getCapacity();
3972
}
@@ -56,6 +89,15 @@ public boolean canFillSlot() {
5689
return this.canFillSlot;
5790
}
5891

92+
public GTFluidSyncHandler phantom(boolean phantom) {
93+
this.phantom = phantom;
94+
return this;
95+
}
96+
97+
public boolean isPhantom() {
98+
return phantom;
99+
}
100+
59101
public String getFormattedFluidAmount() {
60102
return String.format("%,d", tank.getFluid() == null ? 0 : tank.getFluid().amount);
61103
}
@@ -68,17 +110,16 @@ public String getFluidLocalizedName() {
68110
public void readOnClient(int id, PacketBuffer buf) {
69111
switch (id) {
70112
case TRY_CLICK_CONTAINER -> replaceCursorItemStack(NetworkUtils.readItemStack(buf));
71-
case UPDATE_TANK -> {
72-
tank.drain(Integer.MAX_VALUE, true);
73-
tank.fill(NetworkUtils.readFluidStack(buf), true);
74-
}
113+
case UPDATE_TANK -> setFluid(NetworkUtils.readFluidStack(buf));
114+
case UPDATE_AMOUNT -> setAmount(buf.readInt());
75115
}
76116
}
77117

78118
@Override
79119
public void readOnServer(int id, PacketBuffer buf) {
80120
if (id == TRY_CLICK_CONTAINER) {
81-
var stack = tryClickContainer(buf.readBoolean());
121+
var data = MouseData.readPacket(buf);
122+
var stack = tryClickContainer(data.mouseButton == 0);
82123
if (!stack.isEmpty())
83124
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
84125
}
@@ -162,27 +203,27 @@ private ItemStack drainTankIntoStack(IFluidHandlerItem fluidHandler, FluidStack
162203
ItemStack heldItem = getSyncManager().getCursorItem();
163204
if (heldItem.isEmpty()) return ItemStack.EMPTY;
164205

165-
ItemStack fluidContainer = fluidHandler.getContainer();
206+
ItemStack fluidContainer = ItemStack.EMPTY;
166207
int filled = fluidHandler.fill(tankFluid, false);
208+
int stored = tankFluid.amount;
167209
if (filled > 0) {
168210
fluidHandler.fill(tankFluid, true);
169211
tank.drain(filled, true);
212+
fluidContainer = fluidHandler.getContainer();
170213
if (tryFillAll) {
171214
// Determine how many more items we can fill. One item is already filled.
172215
// Integer division means it will round down, so it will only fill equivalent fluid amounts.
173216
// For example:
174217
// Click with 3 cells, with 2500L of fluid in the tank.
175218
// 2 cells will be filled, and 500L will be left behind in the tank.
176-
int additional = Math.min(heldItem.getCount(), tankFluid.amount / filled) - 1;
219+
int additional = Math.min(heldItem.getCount(), stored / filled) - 1;
177220
tank.drain(filled * additional, true);
178221
fluidContainer.grow(additional);
179222
}
180-
fluidContainer = fluidHandler.getContainer();
181223
replaceCursorItemStack(fluidContainer);
182224
playSound(tankFluid, false);
183-
return fluidContainer;
184225
}
185-
return ItemStack.EMPTY;
226+
return fluidContainer;
186227
}
187228

188229
/**

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import gregtech.api.capability.IControllable;
77
import gregtech.api.capability.impl.FluidTankList;
88
import gregtech.api.capability.impl.NotifiableFluidTank;
9-
import gregtech.api.gui.GuiTextures;
10-
import gregtech.api.gui.ModularUI;
11-
import gregtech.api.gui.widgets.TankWidget;
129
import gregtech.api.metatileentity.MetaTileEntity;
1310
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
1411
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
@@ -19,7 +16,6 @@
1916
import gregtech.common.mui.widget.GTFluidSlot;
2017

2118
import net.minecraft.client.resources.I18n;
22-
import net.minecraft.entity.player.EntityPlayer;
2319
import net.minecraft.item.ItemStack;
2420
import net.minecraft.nbt.NBTTagCompound;
2521
import net.minecraft.network.PacketBuffer;

src/main/java/gregtech/common/mui/widget/GTFluidSlot.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package gregtech.common.mui.widget;
22

3+
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
4+
5+
import com.cleanroommc.modularui.utils.MouseData;
6+
37
import gregtech.api.GTValues;
48
import gregtech.api.mui.sync.GTFluidSyncHandler;
59
import gregtech.api.util.FluidTooltipUtil;
@@ -28,7 +32,8 @@
2832
import org.jetbrains.annotations.NotNull;
2933
import org.jetbrains.annotations.Nullable;
3034

31-
public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactable, JeiIngredientProvider {
35+
public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactable, JeiIngredientProvider,
36+
JeiGhostIngredientSlot<FluidStack> {
3237

3338
private final TextRenderer textRenderer = new TextRenderer();
3439
private GTFluidSyncHandler syncHandler;
@@ -69,7 +74,7 @@ public void onInit() {
6974
}
7075

7176
public GTFluidSlot syncHandler(IFluidTank fluidTank) {
72-
return syncHandler(new GTFluidSyncHandler(fluidTank));
77+
return syncHandler(sync(fluidTank));
7378
}
7479

7580
public GTFluidSlot syncHandler(GTFluidSyncHandler syncHandler) {
@@ -132,7 +137,8 @@ private String getBaseUnit() {
132137
@Override
133138
public Result onMouseTapped(int mouseButton) {
134139
if (this.syncHandler.canFillSlot() || this.syncHandler.canDrainSlot()) {
135-
this.syncHandler.syncToServer(1, buffer -> buffer.writeBoolean(mouseButton == 0));
140+
var data = MouseData.create(mouseButton);
141+
this.syncHandler.syncToServer(GTFluidSyncHandler.TRY_CLICK_CONTAINER, data::writeToPacket);
136142
Interactable.playButtonClickSound();
137143
return Result.SUCCESS;
138144
}
@@ -161,4 +167,14 @@ public static void addIngotMolFluidTooltip(FluidStack fluidStack, RichTooltip to
161167
tooltip.add(TextFormatting.GRAY + LocalizationUtils.format("gregtech.gui.amount_raw") + fluidAmount);
162168
}
163169
}
170+
171+
@Override
172+
public void setGhostIngredient(@NotNull FluidStack ingredient) {
173+
this.syncHandler.setFluid(ingredient);
174+
}
175+
176+
@Override
177+
public @Nullable FluidStack castGhostIngredientIfValid(@NotNull Object ingredient) {
178+
return this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
179+
}
164180
}

0 commit comments

Comments
 (0)