Skip to content

Commit

Permalink
move FixedFluidSlotSH logic into GTFluidSyncHandler
Browse files Browse the repository at this point in the history
move showAmount to GTFluidSyncHandler
use GTFluidSlot for SimpleFluidFilter
  • Loading branch information
ghzdude committed Oct 27, 2024
1 parent 22912b9 commit b02233a
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 28 deletions.
144 changes: 139 additions & 5 deletions src/main/java/gregtech/api/mui/sync/GTFluidSyncHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;

import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.utils.FluidTankHandler;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import org.jetbrains.annotations.NotNull;
Expand All @@ -24,16 +26,22 @@ public class GTFluidSyncHandler extends SyncHandler {
public static final int TRY_CLICK_CONTAINER = 1;
public static final int UPDATE_TANK = 2;
public static final int UPDATE_AMOUNT = 3;
public static final int PHANTOM_SCROLL = 4;
public static final int LOCK_FLUID = 5;

private final IFluidTank tank;
private final IFluidHandler handler;
private FluidStack lastFluid;
private FluidStack lockedFluid;
private FluidStack phantomFluid;
private boolean canDrainSlot = true;
private boolean canFillSlot = true;
private boolean phantom;
private boolean showAmount = true;

public GTFluidSyncHandler(IFluidTank tank) {
this.tank = tank;
this.handler = FluidTankHandler.getTankFluidHandler(tank);
}

@Override
Expand All @@ -59,6 +67,15 @@ public void setFluid(FluidStack fluid) {
tank.drain(Integer.MAX_VALUE, true);
tank.fill(fluid, true);
}
if (!isPhantom() || fluid == null) return;
if (this.phantomFluid == null || this.phantomFluid.getFluid() != fluid.getFluid()) {
this.phantomFluid = fluid;
}
}

public void lockFluid(FluidStack fluid) {
this.lockedFluid = fluid;
sync(LOCK_FLUID, buffer -> NetworkUtils.writeFluidStack(buffer, fluid));
}

public void setAmount(int amount) {
Expand Down Expand Up @@ -90,13 +107,24 @@ public boolean canFillSlot() {

public GTFluidSyncHandler phantom(boolean phantom) {
this.phantom = phantom;
if (phantom && this.tank.getFluid() != null)
this.phantomFluid = this.tank.getFluid().copy();
return this;
}

public boolean isPhantom() {
return phantom;
}

public GTFluidSyncHandler showAmount(boolean showAmount) {
this.showAmount = showAmount;
return this;
}

public boolean showAmount() {
return this.showAmount;
}

public String getFormattedFluidAmount() {
return String.format("%,d", tank.getFluid() == null ? 0 : tank.getFluid().amount);
}
Expand All @@ -111,16 +139,122 @@ public void readOnClient(int id, PacketBuffer buf) {
case TRY_CLICK_CONTAINER -> replaceCursorItemStack(NetworkUtils.readItemStack(buf));
case UPDATE_TANK -> setFluid(NetworkUtils.readFluidStack(buf));
case UPDATE_AMOUNT -> setAmount(buf.readInt());
case LOCK_FLUID -> lockFluid(NetworkUtils.readFluidStack(buf));
}
}

@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == TRY_CLICK_CONTAINER) {
var data = MouseData.readPacket(buf);
var stack = tryClickContainer(data.mouseButton == 0);
if (!stack.isEmpty())
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
switch (id) {
case TRY_CLICK_CONTAINER -> {
var data = MouseData.readPacket(buf);
if (isPhantom()) {
tryClickPhantom(data);
} else {
var stack = tryClickContainer(data.mouseButton == 0);
if (!stack.isEmpty())
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
}
}
case UPDATE_TANK -> {
var fluid = NetworkUtils.readFluidStack(buf);
setFluid(fluid);
}
case PHANTOM_SCROLL -> tryScrollPhantom(MouseData.readPacket(buf));
case LOCK_FLUID -> lockFluid(NetworkUtils.readFluidStack(buf));
}
}

public void tryClickPhantom(MouseData data) {
EntityPlayer player = getSyncManager().getPlayer();
ItemStack currentStack = player.inventory.getItemStack();
FluidStack currentFluid = this.tank.getFluid();
if (currentStack.getCount() > 1) currentStack = GTUtility.copy(1, currentStack);
IFluidHandlerItem fluidHandlerItem = currentStack
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);

switch (data.mouseButton) {
case 0 -> {
if (currentStack.isEmpty() || fluidHandlerItem == null) {
if (this.canDrainSlot()) {
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
} else {
FluidStack cellFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, false);
if ((this.showAmount || currentFluid == null) && cellFluid != null) {
if (this.canFillSlot()) {
if (!this.showAmount) {
cellFluid.amount = 1;
}
if (this.tank.fill(cellFluid, true) > 0) {
this.phantomFluid = cellFluid.copy();
}
}
} else {
if (this.canDrainSlot()) {
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
}
}
}
case 1 -> {
if (this.canFillSlot()) {
if (currentFluid != null) {
if (this.showAmount) {
FluidStack toFill = currentFluid.copy();
toFill.amount = 1000;
this.tank.fill(toFill, true);
}
} else if (this.phantomFluid != null) {
FluidStack toFill = this.phantomFluid.copy();
toFill.amount = this.showAmount ? 1 : toFill.amount;
this.tank.fill(toFill, true);
}
}
}
case 2 -> {
if (currentFluid != null && canDrainSlot())
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
}
}

public void tryScrollPhantom(MouseData mouseData) {
FluidStack currentFluid = this.tank.getFluid();
int amount = mouseData.mouseButton;
if (!this.showAmount()) {
int newAmt = amount == 1 ? 1 : 0;
if (newAmt == 0) {
setFluid(null);
return;
} else if (currentFluid != null && currentFluid.amount != newAmt) {
setAmount(newAmt);
return;
}
}
if (mouseData.shift) {
amount *= 10;
}
if (mouseData.ctrl) {
amount *= 100;
}
if (mouseData.alt) {
amount *= 1000;
}
if (currentFluid == null) {
if (amount > 0 && this.phantomFluid != null) {
FluidStack toFill = this.phantomFluid.copy();
toFill.amount = this.showAmount() ? amount : 1;
this.tank.fill(toFill, true);
}
return;
}
if (amount > 0) {
FluidStack toFill = currentFluid.copy();
toFill.amount = amount;
this.tank.fill(toFill, true);
} else if (amount < 0) {
this.tank.drain(-amount, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import gregtech.api.cover.CoverWithUI;
import gregtech.api.mui.GTGuis;
import gregtech.api.mui.sync.FixedFluidSlotSH;
import gregtech.common.covers.filter.readers.SimpleFluidFilterReader;
import gregtech.common.mui.widget.GTFluidSlot;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widgets.FluidSlot;
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
import com.cleanroommc.modularui.widgets.layout.Flow;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,8 +57,10 @@ public void configureFilterTanks(int amount) {
.matrix("FFF",
"FFF",
"FFF")
.key('F', i -> new FluidSlot()
.syncHandler(new FixedFluidSlotSH(filterReader.getFluidTank(i)).phantom(true)))
.key('F', i -> new GTFluidSlot()
.syncHandler(GTFluidSlot.sync(filterReader.getFluidTank(i))
.phantom(true)
.showAmount(false)))
.build().marginRight(4))
.child(createBlacklistUI());
}
Expand Down
64 changes: 45 additions & 19 deletions src/main/java/gregtech/common/mui/widget/GTFluidSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import gregtech.api.GTValues;
import gregtech.api.mui.sync.GTFluidSyncHandler;
import gregtech.api.util.FluidTooltipUtil;
import gregtech.api.util.GTUtility;
import gregtech.api.util.LocalizationUtils;
import gregtech.client.utils.TooltipHelper;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;

import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.drawable.IKey;
Expand All @@ -18,6 +21,8 @@
import com.cleanroommc.modularui.drawable.text.TextRenderer;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
Expand All @@ -35,9 +40,7 @@ public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactab

private final TextRenderer textRenderer = new TextRenderer();
private GTFluidSyncHandler syncHandler;
private boolean showAmount = true;
private boolean disableBackground = false;
// todo phantom

public GTFluidSlot() {
tooltip().setAutoUpdate(true);
Expand All @@ -48,15 +51,17 @@ public GTFluidSlot() {
if (fluid == null) return;

tooltip.add(fluid.getLocalizedName()).newLine();
tooltip.addLine(IKey.lang("gregtech.fluid.amount", fluid.amount, this.syncHandler.getCapacity()));
if (this.syncHandler.showAmount())
tooltip.addLine(IKey.lang("gregtech.fluid.amount", fluid.amount, this.syncHandler.getCapacity()));

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

addIngotMolFluidTooltip(fluid, tooltip);
if (this.syncHandler.showAmount())
addIngotMolFluidTooltip(fluid, tooltip);
});
}

Expand All @@ -69,6 +74,7 @@ public void onInit() {
this.textRenderer.setShadow(true);
this.textRenderer.setScale(0.5f);
this.textRenderer.setColor(Color.WHITE.main);
getContext().getJeiSettings().addJeiGhostIngredientSlot(this);
}

public GTFluidSlot syncHandler(IFluidTank fluidTank) {
Expand All @@ -81,11 +87,6 @@ public GTFluidSlot syncHandler(GTFluidSyncHandler syncHandler) {
return this;
}

public GTFluidSlot showAmount(boolean showAmount) {
this.showAmount = showAmount;
return this;
}

public GTFluidSlot disableBackground() {
this.disableBackground = true;
return this;
Expand All @@ -108,7 +109,7 @@ public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {

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

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

@NotNull
@Override
public Result onMouseTapped(int mouseButton) {
public @NotNull Result onMousePressed(int mouseButton) {
var data = MouseData.create(mouseButton);
if (this.syncHandler.canFillSlot() || this.syncHandler.canDrainSlot()) {
var data = MouseData.create(mouseButton);
this.syncHandler.syncToServer(GTFluidSyncHandler.TRY_CLICK_CONTAINER, data::writeToPacket);
Interactable.playButtonClickSound();
return Result.SUCCESS;
Expand All @@ -144,13 +144,20 @@ public Result onMouseTapped(int mouseButton) {
}

@Override
protected WidgetTheme getWidgetThemeInternal(ITheme theme) {
return theme.getFluidSlotTheme();
public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
if (!this.syncHandler.isPhantom()) return false;
if ((scrollDirection.isUp() && !this.syncHandler.canFillSlot()) ||
(scrollDirection.isDown() && !this.syncHandler.canDrainSlot())) {
return false;
}
MouseData mouseData = MouseData.create(scrollDirection.modifier);
this.syncHandler.syncToServer(GTFluidSyncHandler.PHANTOM_SCROLL, mouseData::writeToPacket);
return true;
}

@Override
public @Nullable Object getIngredient() {
return this.syncHandler.getFluid();
protected WidgetTheme getWidgetThemeInternal(ITheme theme) {
return theme.getFluidSlotTheme();
}

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

@Override
public void setGhostIngredient(@NotNull FluidStack ingredient) {
this.syncHandler.setFluid(ingredient);
if (this.syncHandler.isPhantom()) {
this.syncHandler.setFluid(ingredient);
this.syncHandler.syncToServer(GTFluidSyncHandler.UPDATE_TANK,
buffer -> NetworkUtils.writeFluidStack(buffer, ingredient));
} else {
this.syncHandler.lockFluid(ingredient);
}
}

@Override
public @Nullable FluidStack castGhostIngredientIfValid(@NotNull Object ingredient) {
return this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
if (ingredient instanceof FluidStack stack) {
return stack;
} else if (ingredient instanceof ItemStack stack &&
stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
var handler = GTUtility.copy(1, stack)
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
return handler == null ? null : handler.drain(Integer.MAX_VALUE, true);
}
return null;
}

@Override
public @Nullable Object getIngredient() {
return this.syncHandler.getFluid();
}
}

0 comments on commit b02233a

Please sign in to comment.