Skip to content

Commit b02233a

Browse files
committed
move FixedFluidSlotSH logic into GTFluidSyncHandler
move showAmount to GTFluidSyncHandler use GTFluidSlot for SimpleFluidFilter
1 parent 22912b9 commit b02233a

File tree

3 files changed

+189
-28
lines changed

3 files changed

+189
-28
lines changed

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

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import net.minecraftforge.fluids.FluidTank;
1313
import net.minecraftforge.fluids.IFluidTank;
1414
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
15+
import net.minecraftforge.fluids.capability.IFluidHandler;
1516
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
1617

1718
import com.cleanroommc.modularui.network.NetworkUtils;
19+
import com.cleanroommc.modularui.utils.FluidTankHandler;
1820
import com.cleanroommc.modularui.utils.MouseData;
1921
import com.cleanroommc.modularui.value.sync.SyncHandler;
2022
import org.jetbrains.annotations.NotNull;
@@ -24,16 +26,22 @@ public class GTFluidSyncHandler extends SyncHandler {
2426
public static final int TRY_CLICK_CONTAINER = 1;
2527
public static final int UPDATE_TANK = 2;
2628
public static final int UPDATE_AMOUNT = 3;
29+
public static final int PHANTOM_SCROLL = 4;
30+
public static final int LOCK_FLUID = 5;
2731

2832
private final IFluidTank tank;
33+
private final IFluidHandler handler;
2934
private FluidStack lastFluid;
3035
private FluidStack lockedFluid;
36+
private FluidStack phantomFluid;
3137
private boolean canDrainSlot = true;
3238
private boolean canFillSlot = true;
3339
private boolean phantom;
40+
private boolean showAmount = true;
3441

3542
public GTFluidSyncHandler(IFluidTank tank) {
3643
this.tank = tank;
44+
this.handler = FluidTankHandler.getTankFluidHandler(tank);
3745
}
3846

3947
@Override
@@ -59,6 +67,15 @@ public void setFluid(FluidStack fluid) {
5967
tank.drain(Integer.MAX_VALUE, true);
6068
tank.fill(fluid, true);
6169
}
70+
if (!isPhantom() || fluid == null) return;
71+
if (this.phantomFluid == null || this.phantomFluid.getFluid() != fluid.getFluid()) {
72+
this.phantomFluid = fluid;
73+
}
74+
}
75+
76+
public void lockFluid(FluidStack fluid) {
77+
this.lockedFluid = fluid;
78+
sync(LOCK_FLUID, buffer -> NetworkUtils.writeFluidStack(buffer, fluid));
6279
}
6380

6481
public void setAmount(int amount) {
@@ -90,13 +107,24 @@ public boolean canFillSlot() {
90107

91108
public GTFluidSyncHandler phantom(boolean phantom) {
92109
this.phantom = phantom;
110+
if (phantom && this.tank.getFluid() != null)
111+
this.phantomFluid = this.tank.getFluid().copy();
93112
return this;
94113
}
95114

96115
public boolean isPhantom() {
97116
return phantom;
98117
}
99118

119+
public GTFluidSyncHandler showAmount(boolean showAmount) {
120+
this.showAmount = showAmount;
121+
return this;
122+
}
123+
124+
public boolean showAmount() {
125+
return this.showAmount;
126+
}
127+
100128
public String getFormattedFluidAmount() {
101129
return String.format("%,d", tank.getFluid() == null ? 0 : tank.getFluid().amount);
102130
}
@@ -111,16 +139,122 @@ public void readOnClient(int id, PacketBuffer buf) {
111139
case TRY_CLICK_CONTAINER -> replaceCursorItemStack(NetworkUtils.readItemStack(buf));
112140
case UPDATE_TANK -> setFluid(NetworkUtils.readFluidStack(buf));
113141
case UPDATE_AMOUNT -> setAmount(buf.readInt());
142+
case LOCK_FLUID -> lockFluid(NetworkUtils.readFluidStack(buf));
114143
}
115144
}
116145

117146
@Override
118147
public void readOnServer(int id, PacketBuffer buf) {
119-
if (id == TRY_CLICK_CONTAINER) {
120-
var data = MouseData.readPacket(buf);
121-
var stack = tryClickContainer(data.mouseButton == 0);
122-
if (!stack.isEmpty())
123-
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
148+
switch (id) {
149+
case TRY_CLICK_CONTAINER -> {
150+
var data = MouseData.readPacket(buf);
151+
if (isPhantom()) {
152+
tryClickPhantom(data);
153+
} else {
154+
var stack = tryClickContainer(data.mouseButton == 0);
155+
if (!stack.isEmpty())
156+
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
157+
}
158+
}
159+
case UPDATE_TANK -> {
160+
var fluid = NetworkUtils.readFluidStack(buf);
161+
setFluid(fluid);
162+
}
163+
case PHANTOM_SCROLL -> tryScrollPhantom(MouseData.readPacket(buf));
164+
case LOCK_FLUID -> lockFluid(NetworkUtils.readFluidStack(buf));
165+
}
166+
}
167+
168+
public void tryClickPhantom(MouseData data) {
169+
EntityPlayer player = getSyncManager().getPlayer();
170+
ItemStack currentStack = player.inventory.getItemStack();
171+
FluidStack currentFluid = this.tank.getFluid();
172+
if (currentStack.getCount() > 1) currentStack = GTUtility.copy(1, currentStack);
173+
IFluidHandlerItem fluidHandlerItem = currentStack
174+
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
175+
176+
switch (data.mouseButton) {
177+
case 0 -> {
178+
if (currentStack.isEmpty() || fluidHandlerItem == null) {
179+
if (this.canDrainSlot()) {
180+
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
181+
}
182+
} else {
183+
FluidStack cellFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, false);
184+
if ((this.showAmount || currentFluid == null) && cellFluid != null) {
185+
if (this.canFillSlot()) {
186+
if (!this.showAmount) {
187+
cellFluid.amount = 1;
188+
}
189+
if (this.tank.fill(cellFluid, true) > 0) {
190+
this.phantomFluid = cellFluid.copy();
191+
}
192+
}
193+
} else {
194+
if (this.canDrainSlot()) {
195+
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
196+
}
197+
}
198+
}
199+
}
200+
case 1 -> {
201+
if (this.canFillSlot()) {
202+
if (currentFluid != null) {
203+
if (this.showAmount) {
204+
FluidStack toFill = currentFluid.copy();
205+
toFill.amount = 1000;
206+
this.tank.fill(toFill, true);
207+
}
208+
} else if (this.phantomFluid != null) {
209+
FluidStack toFill = this.phantomFluid.copy();
210+
toFill.amount = this.showAmount ? 1 : toFill.amount;
211+
this.tank.fill(toFill, true);
212+
}
213+
}
214+
}
215+
case 2 -> {
216+
if (currentFluid != null && canDrainSlot())
217+
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
218+
}
219+
}
220+
}
221+
222+
public void tryScrollPhantom(MouseData mouseData) {
223+
FluidStack currentFluid = this.tank.getFluid();
224+
int amount = mouseData.mouseButton;
225+
if (!this.showAmount()) {
226+
int newAmt = amount == 1 ? 1 : 0;
227+
if (newAmt == 0) {
228+
setFluid(null);
229+
return;
230+
} else if (currentFluid != null && currentFluid.amount != newAmt) {
231+
setAmount(newAmt);
232+
return;
233+
}
234+
}
235+
if (mouseData.shift) {
236+
amount *= 10;
237+
}
238+
if (mouseData.ctrl) {
239+
amount *= 100;
240+
}
241+
if (mouseData.alt) {
242+
amount *= 1000;
243+
}
244+
if (currentFluid == null) {
245+
if (amount > 0 && this.phantomFluid != null) {
246+
FluidStack toFill = this.phantomFluid.copy();
247+
toFill.amount = this.showAmount() ? amount : 1;
248+
this.tank.fill(toFill, true);
249+
}
250+
return;
251+
}
252+
if (amount > 0) {
253+
FluidStack toFill = currentFluid.copy();
254+
toFill.amount = amount;
255+
this.tank.fill(toFill, true);
256+
} else if (amount < 0) {
257+
this.tank.drain(-amount, true);
124258
}
125259
}
126260

src/main/java/gregtech/common/covers/filter/SimpleFluidFilter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import gregtech.api.cover.CoverWithUI;
44
import gregtech.api.mui.GTGuis;
5-
import gregtech.api.mui.sync.FixedFluidSlotSH;
65
import gregtech.common.covers.filter.readers.SimpleFluidFilterReader;
6+
import gregtech.common.mui.widget.GTFluidSlot;
77

88
import net.minecraft.item.ItemStack;
99
import net.minecraftforge.fluids.FluidStack;
1010

1111
import com.cleanroommc.modularui.screen.ModularPanel;
1212
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
1313
import com.cleanroommc.modularui.widget.Widget;
14-
import com.cleanroommc.modularui.widgets.FluidSlot;
1514
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
1615
import com.cleanroommc.modularui.widgets.layout.Flow;
1716
import org.jetbrains.annotations.NotNull;
@@ -58,8 +57,10 @@ public void configureFilterTanks(int amount) {
5857
.matrix("FFF",
5958
"FFF",
6059
"FFF")
61-
.key('F', i -> new FluidSlot()
62-
.syncHandler(new FixedFluidSlotSH(filterReader.getFluidTank(i)).phantom(true)))
60+
.key('F', i -> new GTFluidSlot()
61+
.syncHandler(GTFluidSlot.sync(filterReader.getFluidTank(i))
62+
.phantom(true)
63+
.showAmount(false)))
6364
.build().marginRight(4))
6465
.child(createBlacklistUI());
6566
}

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

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import gregtech.api.GTValues;
44
import gregtech.api.mui.sync.GTFluidSyncHandler;
55
import gregtech.api.util.FluidTooltipUtil;
6+
import gregtech.api.util.GTUtility;
67
import gregtech.api.util.LocalizationUtils;
78
import gregtech.client.utils.TooltipHelper;
89

910
import net.minecraft.client.renderer.GlStateManager;
11+
import net.minecraft.item.ItemStack;
1012
import net.minecraft.util.text.TextFormatting;
1113
import net.minecraftforge.fluids.FluidStack;
1214
import net.minecraftforge.fluids.IFluidTank;
15+
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
1316

1417
import com.cleanroommc.modularui.api.ITheme;
1518
import com.cleanroommc.modularui.api.drawable.IKey;
@@ -18,6 +21,8 @@
1821
import com.cleanroommc.modularui.drawable.text.TextRenderer;
1922
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
2023
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
24+
import com.cleanroommc.modularui.network.NetworkUtils;
25+
import com.cleanroommc.modularui.screen.ModularScreen;
2126
import com.cleanroommc.modularui.screen.RichTooltip;
2227
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
2328
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -35,9 +40,7 @@ public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactab
3540

3641
private final TextRenderer textRenderer = new TextRenderer();
3742
private GTFluidSyncHandler syncHandler;
38-
private boolean showAmount = true;
3943
private boolean disableBackground = false;
40-
// todo phantom
4144

4245
public GTFluidSlot() {
4346
tooltip().setAutoUpdate(true);
@@ -48,15 +51,17 @@ public GTFluidSlot() {
4851
if (fluid == null) return;
4952

5053
tooltip.add(fluid.getLocalizedName()).newLine();
51-
tooltip.addLine(IKey.lang("gregtech.fluid.amount", fluid.amount, this.syncHandler.getCapacity()));
54+
if (this.syncHandler.showAmount())
55+
tooltip.addLine(IKey.lang("gregtech.fluid.amount", fluid.amount, this.syncHandler.getCapacity()));
5256

5357
// Add various tooltips from the material
5458
for (String s : FluidTooltipUtil.getFluidTooltip(fluid)) {
5559
if (s.isEmpty()) continue;
5660
tooltip.add(s).newLine();
5761
}
5862

59-
addIngotMolFluidTooltip(fluid, tooltip);
63+
if (this.syncHandler.showAmount())
64+
addIngotMolFluidTooltip(fluid, tooltip);
6065
});
6166
}
6267

@@ -69,6 +74,7 @@ public void onInit() {
6974
this.textRenderer.setShadow(true);
7075
this.textRenderer.setScale(0.5f);
7176
this.textRenderer.setColor(Color.WHITE.main);
77+
getContext().getJeiSettings().addJeiGhostIngredientSlot(this);
7278
}
7379

7480
public GTFluidSlot syncHandler(IFluidTank fluidTank) {
@@ -81,11 +87,6 @@ public GTFluidSlot syncHandler(GTFluidSyncHandler syncHandler) {
8187
return this;
8288
}
8389

84-
public GTFluidSlot showAmount(boolean showAmount) {
85-
this.showAmount = showAmount;
86-
return this;
87-
}
88-
8990
public GTFluidSlot disableBackground() {
9091
this.disableBackground = true;
9192
return this;
@@ -108,7 +109,7 @@ public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
108109

109110
GuiDraw.drawFluidTexture(content, 1, 1, getArea().w() - 2, getArea().h() - 2, 0);
110111

111-
if (content != null && showAmount) {
112+
if (content != null && this.syncHandler.showAmount()) {
112113
String s = NumberFormat.formatWithMaxDigits(getBaseUnitAmount(content.amount)) + getBaseUnit();
113114
this.textRenderer.setAlignment(Alignment.CenterRight, getArea().width - 1f);
114115
this.textRenderer.setPos(0, 12);
@@ -131,11 +132,10 @@ private String getBaseUnit() {
131132
return "kL";
132133
}
133134

134-
@NotNull
135135
@Override
136-
public Result onMouseTapped(int mouseButton) {
136+
public @NotNull Result onMousePressed(int mouseButton) {
137+
var data = MouseData.create(mouseButton);
137138
if (this.syncHandler.canFillSlot() || this.syncHandler.canDrainSlot()) {
138-
var data = MouseData.create(mouseButton);
139139
this.syncHandler.syncToServer(GTFluidSyncHandler.TRY_CLICK_CONTAINER, data::writeToPacket);
140140
Interactable.playButtonClickSound();
141141
return Result.SUCCESS;
@@ -144,13 +144,20 @@ public Result onMouseTapped(int mouseButton) {
144144
}
145145

146146
@Override
147-
protected WidgetTheme getWidgetThemeInternal(ITheme theme) {
148-
return theme.getFluidSlotTheme();
147+
public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
148+
if (!this.syncHandler.isPhantom()) return false;
149+
if ((scrollDirection.isUp() && !this.syncHandler.canFillSlot()) ||
150+
(scrollDirection.isDown() && !this.syncHandler.canDrainSlot())) {
151+
return false;
152+
}
153+
MouseData mouseData = MouseData.create(scrollDirection.modifier);
154+
this.syncHandler.syncToServer(GTFluidSyncHandler.PHANTOM_SCROLL, mouseData::writeToPacket);
155+
return true;
149156
}
150157

151158
@Override
152-
public @Nullable Object getIngredient() {
153-
return this.syncHandler.getFluid();
159+
protected WidgetTheme getWidgetThemeInternal(ITheme theme) {
160+
return theme.getFluidSlotTheme();
154161
}
155162

156163
public static void addIngotMolFluidTooltip(FluidStack fluidStack, RichTooltip tooltip) {
@@ -168,11 +175,30 @@ public static void addIngotMolFluidTooltip(FluidStack fluidStack, RichTooltip to
168175

169176
@Override
170177
public void setGhostIngredient(@NotNull FluidStack ingredient) {
171-
this.syncHandler.setFluid(ingredient);
178+
if (this.syncHandler.isPhantom()) {
179+
this.syncHandler.setFluid(ingredient);
180+
this.syncHandler.syncToServer(GTFluidSyncHandler.UPDATE_TANK,
181+
buffer -> NetworkUtils.writeFluidStack(buffer, ingredient));
182+
} else {
183+
this.syncHandler.lockFluid(ingredient);
184+
}
172185
}
173186

174187
@Override
175188
public @Nullable FluidStack castGhostIngredientIfValid(@NotNull Object ingredient) {
176-
return this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
189+
if (ingredient instanceof FluidStack stack) {
190+
return stack;
191+
} else if (ingredient instanceof ItemStack stack &&
192+
stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
193+
var handler = GTUtility.copy(1, stack)
194+
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
195+
return handler == null ? null : handler.drain(Integer.MAX_VALUE, true);
196+
}
197+
return null;
198+
}
199+
200+
@Override
201+
public @Nullable Object getIngredient() {
202+
return this.syncHandler.getFluid();
177203
}
178204
}

0 commit comments

Comments
 (0)