Skip to content

Commit

Permalink
fix issues with GTFluidSlot and cell behavior
Browse files Browse the repository at this point in the history
sync changes to tank fluid automatically
work on supporting phantom fluid slot
  • Loading branch information
ghzdude committed Sep 22, 2024
1 parent d20caf9 commit be67af5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/main/java/gregtech/api/mui/GTGuis.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import net.minecraft.item.ItemStack;

import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.factory.GuiManager;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.utils.Alignment;
Expand Down Expand Up @@ -86,7 +85,7 @@ public static ModularPanel defaultPopupPanel(String name) {
}

public static ModularPanel defaultPopupPanel(String name, boolean disableBelow,
boolean closeOnOutsideClick) {
boolean closeOnOutsideClick) {
return new PopupPanel(name, DEFAULT_WIDTH, DEFAULT_HIEGHT, disableBelow, closeOnOutsideClick);
}

Expand Down
65 changes: 53 additions & 12 deletions src/main/java/gregtech/api/mui/sync/GTFluidSyncHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.api.mui.sync;

import com.cleanroommc.modularui.utils.MouseData;

import gregtech.api.util.GTUtility;

import net.minecraft.entity.item.EntityItem;
Expand All @@ -9,6 +11,7 @@
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
Expand All @@ -19,21 +22,51 @@

public class GTFluidSyncHandler extends SyncHandler {

private static final int TRY_CLICK_CONTAINER = 1;
private static final int UPDATE_TANK = 2;
public static final int TRY_CLICK_CONTAINER = 1;
public static final int UPDATE_TANK = 2;
public static final int UPDATE_AMOUNT = 3;

private final IFluidTank tank;
private FluidStack lastFluid;
private FluidStack lockedFluid;
private boolean canDrainSlot = true;
private boolean canFillSlot = true;
private boolean phantom;

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

@Override
public void detectAndSendChanges(boolean init) {
var current = getFluid();
if (current == null && lastFluid == null) return;
if (current == null || lastFluid == null || lastFluid.getFluid() != current.getFluid()) {
lastFluid = current == null ? null : current.copy();
syncToClient(UPDATE_TANK, buffer -> NetworkUtils.writeFluidStack(buffer, current));
} else if (current.amount != lastFluid.amount) {
syncToClient(UPDATE_AMOUNT, buffer -> buffer.writeInt(current.amount));
}
}

public FluidStack getFluid() {
return this.tank.getFluid();
}

public void setFluid(FluidStack fluid) {
if (tank instanceof FluidTank fluidTank) {
fluidTank.setFluid(fluid);
} else {
tank.drain(Integer.MAX_VALUE, true);
tank.fill(fluid, true);
}
}

public void setAmount(int amount) {
if (getFluid() == null) return;
getFluid().amount = amount;
}

public int getCapacity() {
return this.tank.getCapacity();
}
Expand All @@ -56,6 +89,15 @@ public boolean canFillSlot() {
return this.canFillSlot;
}

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

public boolean isPhantom() {
return phantom;
}

public String getFormattedFluidAmount() {
return String.format("%,d", tank.getFluid() == null ? 0 : tank.getFluid().amount);
}
Expand All @@ -68,17 +110,16 @@ public String getFluidLocalizedName() {
public void readOnClient(int id, PacketBuffer buf) {
switch (id) {
case TRY_CLICK_CONTAINER -> replaceCursorItemStack(NetworkUtils.readItemStack(buf));
case UPDATE_TANK -> {
tank.drain(Integer.MAX_VALUE, true);
tank.fill(NetworkUtils.readFluidStack(buf), true);
}
case UPDATE_TANK -> setFluid(NetworkUtils.readFluidStack(buf));
case UPDATE_AMOUNT -> setAmount(buf.readInt());
}
}

@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == TRY_CLICK_CONTAINER) {
var stack = tryClickContainer(buf.readBoolean());
var data = MouseData.readPacket(buf);
var stack = tryClickContainer(data.mouseButton == 0);
if (!stack.isEmpty())
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
}
Expand Down Expand Up @@ -162,27 +203,27 @@ private ItemStack drainTankIntoStack(IFluidHandlerItem fluidHandler, FluidStack
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem.isEmpty()) return ItemStack.EMPTY;

ItemStack fluidContainer = fluidHandler.getContainer();
ItemStack fluidContainer = ItemStack.EMPTY;
int filled = fluidHandler.fill(tankFluid, false);
int stored = tankFluid.amount;
if (filled > 0) {
fluidHandler.fill(tankFluid, true);
tank.drain(filled, true);
fluidContainer = fluidHandler.getContainer();
if (tryFillAll) {
// Determine how many more items we can fill. One item is already filled.
// Integer division means it will round down, so it will only fill equivalent fluid amounts.
// For example:
// Click with 3 cells, with 2500L of fluid in the tank.
// 2 cells will be filled, and 500L will be left behind in the tank.
int additional = Math.min(heldItem.getCount(), tankFluid.amount / filled) - 1;
int additional = Math.min(heldItem.getCount(), stored / filled) - 1;
tank.drain(filled * additional, true);
fluidContainer.grow(additional);
}
fluidContainer = fluidHandler.getContainer();
replaceCursorItemStack(fluidContainer);
playSound(tankFluid, false);
return fluidContainer;
}
return ItemStack.EMPTY;
return fluidContainer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import gregtech.api.capability.IControllable;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.NotifiableFluidTank;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.TankWidget;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
Expand All @@ -19,7 +16,6 @@
import gregtech.common.mui.widget.GTFluidSlot;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/gregtech/common/mui/widget/GTFluidSlot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package gregtech.common.mui.widget;

import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;

import com.cleanroommc.modularui.utils.MouseData;

import gregtech.api.GTValues;
import gregtech.api.mui.sync.GTFluidSyncHandler;
import gregtech.api.util.FluidTooltipUtil;
Expand Down Expand Up @@ -28,7 +32,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactable, JeiIngredientProvider {
public final class GTFluidSlot extends Widget<GTFluidSlot> implements Interactable, JeiIngredientProvider,
JeiGhostIngredientSlot<FluidStack> {

private final TextRenderer textRenderer = new TextRenderer();
private GTFluidSyncHandler syncHandler;
Expand Down Expand Up @@ -69,7 +74,7 @@ public void onInit() {
}

public GTFluidSlot syncHandler(IFluidTank fluidTank) {
return syncHandler(new GTFluidSyncHandler(fluidTank));
return syncHandler(sync(fluidTank));
}

public GTFluidSlot syncHandler(GTFluidSyncHandler syncHandler) {
Expand Down Expand Up @@ -132,7 +137,8 @@ private String getBaseUnit() {
@Override
public Result onMouseTapped(int mouseButton) {
if (this.syncHandler.canFillSlot() || this.syncHandler.canDrainSlot()) {
this.syncHandler.syncToServer(1, buffer -> buffer.writeBoolean(mouseButton == 0));
var data = MouseData.create(mouseButton);
this.syncHandler.syncToServer(GTFluidSyncHandler.TRY_CLICK_CONTAINER, data::writeToPacket);
Interactable.playButtonClickSound();
return Result.SUCCESS;
}
Expand Down Expand Up @@ -161,4 +167,14 @@ public static void addIngotMolFluidTooltip(FluidStack fluidStack, RichTooltip to
tooltip.add(TextFormatting.GRAY + LocalizationUtils.format("gregtech.gui.amount_raw") + fluidAmount);
}
}

@Override
public void setGhostIngredient(@NotNull FluidStack ingredient) {
this.syncHandler.setFluid(ingredient);
}

@Override
public @Nullable FluidStack castGhostIngredientIfValid(@NotNull Object ingredient) {
return this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
}
}

0 comments on commit be67af5

Please sign in to comment.