Skip to content

Commit

Permalink
Many fixes (#12)
Browse files Browse the repository at this point in the history
* Fix fluid slots not being filled

* Fix fluid not being rendered

* Fix drag&drop for fluid slot not working

* Allow swapping phantom fluid

* Add convenient methods for IFluidTankLong

* Separate FluidSlot & FluidSlotLong implementation

* Add SyncHandlers#fluidSlotLong

* Fix syncHandler without syncKey not receiving changeListener

* Fix CycleButtonWidget not updating tooltip if not using stateTooltip

* Fix ItemSlot crash on dedicated server

* Fix GUI not opening more than once on dedicated server

* Separate ItemSlot & ItemSlotLong implementation
  • Loading branch information
miozune authored Aug 16, 2024
1 parent 0ec218b commit 59f3610
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 137 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/cleanroommc/modularui/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void preInit(FMLPreInitializationEvent event) {
}

FMLCommonHandler.instance().bus().register(new ClientEventHandler());
FMLCommonHandler.instance().bus().register(new GuiManager());

if (ModularUIConfig.enableTestGuis) {
MinecraftForge.EVENT_BUS.register(new EventHandler());
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cleanroommc/modularui/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CommonProxy {

void preInit(FMLPreInitializationEvent event) {
ModularUIConfig.init(event.getSuggestedConfigurationFile());
FMLCommonHandler.instance().bus().register(new GuiManager());
MinecraftForge.EVENT_BUS.register(new GuiManager());

FMLCommonHandler.instance().bus().register(this);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/IFluidTankLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nullable;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
Expand Down Expand Up @@ -32,6 +33,14 @@ public interface IFluidTankLong extends IFluidTank {

Fluid getRealFluid();

default void setFluid(@Nullable FluidStack fluidStack) {
if (fluidStack == null) {
setFluid(null, 0);
} else {
setFluid(fluidStack.getFluid(), fluidStack.amount);
}
}

void setFluid(@Nullable Fluid fluid, long amount);

@Override
Expand Down Expand Up @@ -69,4 +78,7 @@ default FluidTankInfo getInfo() {

IFluidTankLong copy();

IFluidTankLong readFromNBT(NBTTagCompound fluidTag);

NBTTagCompound writeToNBT(NBTTagCompound fluidTag);
}
13 changes: 10 additions & 3 deletions src/main/java/com/cleanroommc/modularui/drawable/GuiDraw.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.cleanroommc.modularui.drawable;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
import com.cleanroommc.modularui.utils.Color;

import com.mitchej123.hodgepodge.textures.IPatchedTextureAtlasSprite;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -340,10 +343,14 @@ public static void drawFluidTexture(FluidStack content, float x0, float y0, floa
}
Fluid fluid = content.getFluid();
IIcon fluidStill = fluid.getIcon(content);
TextureAtlasSprite sprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(fluidStill.toString());
if (ModularUI.isHodgepodgeLoaded && fluidStill instanceof IPatchedTextureAtlasSprite) {
((IPatchedTextureAtlasSprite) fluidStill).markNeedsAnimationUpdate();
}
int fluidColor = fluid.getColor(content);
GL11.glColor4f(Color.getRedF(fluidColor), Color.getGreenF(fluidColor), Color.getBlueF(fluidColor), Color.getAlphaF(fluidColor));
drawTiledTexture(TextureMap.locationBlocksTexture, x0, y0, width, height, sprite.getMinU(), sprite.getMinV(), sprite.getMaxU(), sprite.getMaxV(), sprite.getIconWidth(), sprite.getIconHeight(), z);
float r = Color.getRedF(fluidColor), g = Color.getGreenF(fluidColor), b = Color.getBlueF(fluidColor), a = Color.getAlphaF(fluidColor);
a = a == 0f ? 1f : a;
GL11.glColor4f(r, g, b, a);
drawTiledTexture(TextureMap.locationBlocksTexture, x0, y0, width, height, fluidStill.getMinU(), fluidStill.getMinV(), fluidStill.getMaxU(), fluidStill.getMaxV(), fluidStill.getIconWidth(), fluidStill.getIconHeight(), z);
GL11.glColor4f(1f, 1f, 1f, 1f);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cleanroommc/modularui/test/TestTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
.child(new ButtonWidget<>()
.size(60, 18)
.overlay(IKey.dynamic(() -> "Button " + this.val)))
.child(new FluidSlot<>()
.child(new FluidSlot()
.margin(2)
.syncHandler(SyncHandlers.fluidSlot(this.fluidTank)))
.child(new ButtonWidget<>()
Expand Down Expand Up @@ -220,7 +220,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
.value(SyncHandlers.intNumber(() -> this.cycleState, val -> this.cycleState = val)))*/
.child(new ItemSlot()
.slot(SyncHandlers.itemSlot(this.inventory, 0).ignoreMaxStackSize(true).singletonSlotGroup()))
.child(new FluidSlot<>()
.child(new FluidSlot()
.margin(2)
.width(30)
.syncHandler(SyncHandlers.fluidSlot(this.fluidTankPhantom).phantom(true)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.cleanroommc.modularui.api.IFluidTankLong;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;

import net.minecraftforge.fluids.FluidRegistry;

import org.jetbrains.annotations.Nullable;

public class FluidTankLong implements IFluidTankLong {
Expand Down Expand Up @@ -96,4 +99,22 @@ public void setFluid(Fluid fluid, long amount) {
public IFluidTankLong copy() {
return new FluidTankLong(getRealFluid(), getCapacityLong(), getFluidAmountLong());
}

@Override
public IFluidTankLong readFromNBT(NBTTagCompound nbt) {
fluid = nbt.hasKey("FluidName") ? FluidRegistry.getFluid(nbt.getString("FluidName")) : null;
amount = nbt.getLong("Amount");
capacity = nbt.getLong("Capacity");
return this;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
if (fluid != null) {
nbt.setString("FluidName", FluidRegistry.getFluidName(fluid));
}
nbt.setLong("Amount", getFluidAmountLong());
nbt.setLong("Capacity", getCapacityLong());
return nbt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.cleanroommc.modularui.api.IFluidTankLong;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
Expand Down Expand Up @@ -56,4 +57,20 @@ public void setFluid(Fluid fluid, long amount) {
public IFluidTankLong copy() {
return new FluidTankLongDelegate(new FluidTank(delegate.getFluid(), delegate.getCapacity()));
}

@Override
public IFluidTankLong readFromNBT(NBTTagCompound fluidTag) {
delegate.drain(Integer.MAX_VALUE, true);
delegate.fill(FluidStack.loadFluidStackFromNBT(fluidTag), true);
return this;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound fluidTag) {
if (delegate.getFluid() == null) {
return fluidTag;
}
delegate.getFluid().writeToNBT(fluidTag);
return fluidTag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

import net.minecraft.item.ItemStack;

import javax.annotation.Nullable;

public interface IItemHandlerLong extends IItemHandlerModifiable {

void setStackInSlotLong(int slot, IItemStackLong stack);

@Override
default void setStackInSlot(int slot, ItemStack stack) {
setStackInSlotLong(slot, new ItemStackLong(stack));
default void setStackInSlot(int slot, @Nullable ItemStack stack) {
setStackInSlotLong(slot, stack == null ? null : new ItemStackLong(stack));
}

IItemStackLong extractItemLong(int slot, long amount, boolean simulate);
Expand Down Expand Up @@ -51,8 +53,8 @@ default List<IItemStackLong> getStacksLong() {
IItemStackLong insertItemLong(int slot, IItemStackLong stack, boolean simulate);

@Override
default ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
IItemStackLong item = insertItemLong(slot, new ItemStackLong(stack), simulate);
default ItemStack insertItem(int slot, @Nullable ItemStack stack, boolean simulate) {
IItemStackLong item = insertItemLong(slot, stack == null ? null : new ItemStackLong(stack), simulate);
return item == null ? null : item.getAsItemStack();
}

Expand All @@ -61,8 +63,8 @@ default boolean isItemValidLong(int slot, IItemStackLong stack) {
}

@Override
default boolean isItemValid(int slot, ItemStack stack) {
return isItemValidLong(slot, new ItemStackLong(stack));
default boolean isItemValid(int slot, @Nullable ItemStack stack) {
return isItemValidLong(slot, stack == null ? null : new ItemStackLong(stack));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;

// Changes made here probably should also be made to FluidSlotSyncHandler
public class FluidSlotLongSyncHandler extends ValueSyncHandler<IFluidTankLong> {

@NotNull
Expand Down Expand Up @@ -125,6 +126,12 @@ public void readOnServer(int id, PacketBuffer buf) {
}
} else if (id == 3) {
this.controlsAmount = buf.readBoolean();
} else if (id == 4) {
MouseData mouseData = MouseData.readPacket(buf);
ItemStack draggedStack = NetworkUtils.readItemStack(buf);
if (this.phantom && draggedStack != null) {
tryClickPhantom(mouseData, draggedStack);
}
}
}

Expand Down Expand Up @@ -173,8 +180,12 @@ private void tryClickContainer(MouseData mouseData) {
}

private void tryClickPhantom(MouseData mouseData) {
FluidStack currentFluid = handler.getTank(index).getFluid();
ItemStack heldItem = getSyncManager().getCursorItem();
tryClickPhantom(mouseData, heldItem);
}

private void tryClickPhantom(MouseData mouseData, ItemStack heldItem) {
FluidStack currentFluid = handler.getTank(index).getFluid();

if (mouseData.mouseButton == 0) {
if (heldItem == null) {
Expand All @@ -186,17 +197,14 @@ private void tryClickPhantom(MouseData mouseData) {
heldItemSizedOne.stackSize = 1;
FluidStack heldFluid = FluidInteractions.getFluidForPhantomItem(heldItemSizedOne);
if ((controlsAmount || currentFluid == null) && heldFluid != null) {
if (canFillSlot) {
if (!controlsAmount) {
heldFluid.amount = 1;
}
if (handler.fill(index, heldFluid.getFluid(), heldFluid.amount, false) > 0) {
lastStoredPhantomFluid = heldFluid.getFluid();
}
}
fillPhantom(heldFluid);
} else {
if (canDrainSlot) {
handler.drain(index, mouseData.shift ? Long.MAX_VALUE : 1000, true);
// "Swap" fluid
if (!controlsAmount && heldFluid != null && handler.getTankAmount(index) <= 0) {
fillPhantom(heldFluid);
}
}
}
}
Expand All @@ -217,6 +225,17 @@ private void tryClickPhantom(MouseData mouseData) {
}
}

private void fillPhantom(FluidStack heldFluid) {
if (canFillSlot) {
if (!controlsAmount) {
heldFluid.amount = 1;
}
if (handler.fill(index, heldFluid.getFluid(), heldFluid.amount, true) > 0) {
lastStoredPhantomFluid = heldFluid.getFluid();
}
}
}

protected void drainFluid(boolean processFullStack) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem == null || heldItem.stackSize == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cleanroommc.modularui.value.sync;

import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.utils.FluidTankHandler;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.utils.fluid.FluidInteractions;

Expand All @@ -10,12 +9,12 @@
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.IFluidTank;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

// Changes made here probably should also be made to FluidSlotLongSyncHandler
public class FluidSlotSyncHandler extends ValueSyncHandler<FluidStack> {

public static boolean isFluidEmpty(@Nullable FluidStack fluidStack) {
Expand Down Expand Up @@ -118,6 +117,12 @@ public void readOnServer(int id, PacketBuffer buf) {
}
} else if (id == 3) {
this.controlsAmount = buf.readBoolean();
} else if (id == 4) {
MouseData mouseData = MouseData.readPacket(buf);
ItemStack draggedStack = NetworkUtils.readItemStack(buf);
if (this.phantom && draggedStack != null) {
tryClickPhantom(mouseData, draggedStack);
}
}
}

Expand Down Expand Up @@ -167,8 +172,12 @@ private void tryClickContainer(MouseData mouseData) {
}

private void tryClickPhantom(MouseData mouseData) {
FluidStack currentFluid = fluidTank.getFluid();
ItemStack cursorStack = getSyncManager().getCursorItem();
tryClickPhantom(mouseData, cursorStack);
}

private void tryClickPhantom(MouseData mouseData, ItemStack cursorStack) {
FluidStack currentFluid = fluidTank.getFluid();

if (mouseData.mouseButton == 0) {
if (cursorStack == null) {
Expand All @@ -180,17 +189,14 @@ private void tryClickPhantom(MouseData mouseData) {
heldItemSizedOne.stackSize = 1;
FluidStack heldFluid = FluidInteractions.getFluidForPhantomItem(heldItemSizedOne);
if ((controlsAmount || currentFluid == null) && heldFluid != null) {
if (canFillSlot) {
if (!controlsAmount) {
heldFluid.amount = 1;
}
if (fluidTank.fill(heldFluid, false) > 0) {
lastStoredPhantomFluid = heldFluid.copy();
}
}
fillPhantom(heldFluid);
} else {
if (canDrainSlot) {
fluidTank.drain(mouseData.shift ? Integer.MAX_VALUE : 1000, true);
// "Swap" fluid
if (!controlsAmount && heldFluid != null && fluidTank.getFluidAmount() <= 0) {
fillPhantom(heldFluid);
}
}
}
}
Expand All @@ -213,6 +219,17 @@ private void tryClickPhantom(MouseData mouseData) {
}
}

private void fillPhantom(@NotNull FluidStack heldFluid) {
if (canFillSlot) {
if (!controlsAmount) {
heldFluid.amount = 1;
}
if (fluidTank.fill(heldFluid, true) > 0) {
lastStoredPhantomFluid = heldFluid.copy();
}
}
}

protected void drainFluid(boolean processFullStack) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem == null || heldItem.stackSize == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;

// Changes made here probably should also be made to ItemSlotSH
public class ItemSlotLongSH extends SyncHandler {

private final ModularSlotLong slot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Wraps a slot and handles interactions for phantom slots.
* Use {@link ModularSlot} directly.
*/
// Changes made here probably should also be made to ItemSlotLongSH
public class ItemSlotSH extends SyncHandler {

private final ModularSlot slot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.value.sync;

import com.cleanroommc.modularui.api.IFluidTankLong;
import com.cleanroommc.modularui.utils.item.IItemHandlerModifiable;
import com.cleanroommc.modularui.utils.BooleanConsumer;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
Expand Down Expand Up @@ -52,6 +53,10 @@ public static FluidSlotSyncHandler fluidSlot(IFluidTank fluidTank) {
return new FluidSlotSyncHandler(fluidTank);
}

public static FluidSlotLongSyncHandler fluidSlotLong(IFluidTankLong fluidTank) {
return new FluidSlotLongSyncHandler(fluidTank);
}

public static <T extends Enum<T>> EnumSyncValue<T> enumValue(Class<T> clazz, Supplier<T> getter, Consumer<T> setter) {
return new EnumSyncValue<>(clazz, getter, setter);
}
Expand Down
Loading

0 comments on commit 59f3610

Please sign in to comment.