Skip to content

Commit

Permalink
Make fluids slots functional
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueWeabo committed Apr 5, 2024
1 parent 8202645 commit c39ebc9
Show file tree
Hide file tree
Showing 20 changed files with 1,024 additions and 179 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ dependencies {
//runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.5.4-GTNH:dev")
// For Thaumcraft runtime
//runtimeOnlyNonPublishable("com.github.GTNewHorizons:Baubles:1.0.4:dev")
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ minimizeShadowedDependencies = true
# If disabled, won't rename the shadowed classes.
relocateShadowedDependencies = true

# Adds the GTNH maven, CurseMaven, IC2/Player maven, and some more well-known 1.7.10 repositories.
# Adds the GTNH maven, CurseMaven, Modrinth, and some more well-known 1.7.10 repositories.
includeWellKnownRepositories = true

# Change these to your Maven coordinates if you want to publish to a custom Maven repository instead of the default GTNH Maven.
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.14'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.21'
}


2 changes: 1 addition & 1 deletion src/main/java/com/cleanroommc/modularui/ModularUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ public void onLoadComplete(FMLLoadCompleteEvent event) {
public void onServerLoad(FMLServerStartingEvent event) {
proxy.onServerLoad(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ default int getCapacity() {

@Override
default FluidStack getFluid() {
if (getRealFluid() == null) return null;
final FluidStack fluid = new FluidStack(getRealFluid(), getFluidAmount());
return fluid;
}
Expand All @@ -68,4 +69,6 @@ default FluidTankInfo getInfo() {
return info;
}

IFluidTankLong copy();

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public IItemStackLong getStackLong() {

@Override
public void putStack(ItemStack stack) {
if (stack == null) {
putStackLong(null);
return;
}
putStackLong(new ItemStackLongDelegate(stack));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public static void sendToPlayer(IPacket packet, EntityPlayerMP player) {
NetHandlerPlayServer handler = ctx.getServerHandler();
return message.executeServer(handler);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.cleanroommc.modularui.network;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.IFluidTankLong;
import com.cleanroommc.modularui.api.IItemStackLong;
import com.cleanroommc.modularui.utils.fluid.FluidTankLong;
import com.cleanroommc.modularui.utils.fluid.FluidTankLongDelegate;
import com.cleanroommc.modularui.utils.item.ItemStackLong;

import cpw.mods.fml.common.FMLCommonHandler;
Expand All @@ -12,9 +15,15 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;

import org.jetbrains.annotations.Nullable;

import static com.google.common.primitives.Ints.saturatedCast;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
Expand Down Expand Up @@ -75,7 +84,12 @@ public static ItemStack readItemStack(PacketBuffer buffer) {
public static void writeItemStackLong(PacketBuffer buffer, IItemStackLong itemStack) {
try {
NBTTagCompound nbt = new NBTTagCompound();
itemStack.writeToNBT(nbt);
if (itemStack == null) {
nbt.setInteger("check", -1);
} else {
itemStack.writeToNBT(nbt);
nbt.setInteger("check", 1);
}
buffer.writeNBTTagCompoundToBuffer(nbt);
} catch (IOException e) {
ModularUI.LOGGER.catching(e);
Expand All @@ -85,6 +99,8 @@ public static void writeItemStackLong(PacketBuffer buffer, IItemStackLong itemSt
public static IItemStackLong readItemStackLong(PacketBuffer buffer) {
try {
NBTTagCompound nbt = buffer.readNBTTagCompoundFromBuffer();
int check = nbt.getInteger("check");
if (check < 0) return null;
return IItemStackLong.loadItemStackFromNBT(nbt);
} catch (IOException e) {
ModularUI.LOGGER.catching(e);
Expand Down Expand Up @@ -119,6 +135,54 @@ public static FluidStack readFluidStack(PacketBuffer buffer) {
}
}

public static void writeFluidTank(PacketBuffer buffer, @Nullable IFluidTankLong tank) {
if (tank == null) {
buffer.writeBoolean(true);
} else {
buffer.writeBoolean(false);
NBTTagCompound fluidStackTag = new NBTTagCompound();
fluidStackTag.setLong("cap", tank.getCapacityLong());
fluidStackTag.setLong("amt", tank.getFluidAmountLong());
if (tank.getRealFluid() != null) {
fluidStackTag.setString("flu", tank.getRealFluid().getName());
}
if (tank instanceof FluidTankLongDelegate) {
fluidStackTag.setByte("type", (byte)1);
} else {
fluidStackTag.setByte("type", (byte)0);
}
try {
buffer.writeNBTTagCompoundToBuffer(fluidStackTag);
} catch (IOException e) {
ModularUI.LOGGER.catching(e);
}
}
}

@Nullable
public static IFluidTankLong readFluidTank(PacketBuffer buffer) {
if (buffer.readBoolean()) {
return null;
}
try {
NBTTagCompound nbt = buffer.readNBTTagCompoundFromBuffer();
byte type = nbt.getByte("type");
Fluid fluid = FluidRegistry.getFluid(nbt.getString("flu"));
long cap = nbt.getLong("cap");
long amt = nbt.getLong("amt");
IFluidTankLong tank;
if (type == 1) {
tank = new FluidTankLongDelegate(new FluidTank(fluid, saturatedCast(cap), saturatedCast(amt)));
} else {
tank = new FluidTankLong(fluid, cap, amt);
}
return tank;
} catch (IOException e) {
ModularUI.LOGGER.catching(e);
return null;
}
}

public static void writeStringSafe(PacketBuffer buffer, String string) {
writeStringSafe(buffer, string, Short.MAX_VALUE, false);
}
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 @@ -130,7 +130,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager 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 @@ -197,7 +197,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
.value(SyncHandlers.intNumber(() -> this.cycleState, val -> this.cycleState = val)))*/
.child(new ItemSlot()
.slot(SyncHandlers.phantomItemSlot(this.inventory, 0).ignoreMaxStackSize(true)))
.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
@@ -0,0 +1,102 @@
package com.cleanroommc.modularui.utils.fluid;

import static com.cleanroommc.modularui.ModularUI.isGT5ULoaded;

import codechicken.nei.recipe.StackInfo;
import gregtech.api.util.GT_Utility;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidTank;

public class FluidInteractions {
/**
* Gets fluid actually stored in item. Used for transferring fluid.
*/
public static FluidStack getFluidForRealItem(ItemStack itemStack) {
FluidStack fluidStack = null;
if (isGT5ULoaded) {
fluidStack = GT_Utility.getFluidForFilledItem(itemStack, true);
}
if (fluidStack == null && itemStack.getItem() instanceof IFluidContainerItem container) {
fluidStack = container.getFluid(itemStack);
}
if (fluidStack == null) {
fluidStack = FluidContainerRegistry.getFluidForFilledItem(itemStack);
}
if (fluidStack == null) {
fluidStack = StackInfo.getFluid(itemStack);
}
return fluidStack;
}

/**
* Gets fluid for use in phantom slot.
*/
public static FluidStack getFluidForPhantomItem(ItemStack itemStack) {
if (isGT5ULoaded) {
return GT_Utility.getFluidFromContainerOrFluidDisplay(itemStack);
} else {
return StackInfo.getFluid(itemStack);
}
}

public static ItemStack fillFluidContainer(FluidStack fluidStack, ItemStack itemStack) {
ItemStack filledContainer = fillFluidContainerWithoutIFluidContainerItem(fluidStack, itemStack);
if (filledContainer == null) {
filledContainer = fillFluidContainerWithIFluidContainerItem(fluidStack, itemStack);
}
if (filledContainer == null) {
filledContainer = FluidContainerRegistry.fillFluidContainer(fluidStack, itemStack);
FluidStack newFluid = getFluidForRealItem(filledContainer);
fluidStack.amount -= newFluid.amount;
}
return filledContainer;
}

public static ItemStack fillFluidContainerWithoutIFluidContainerItem(FluidStack fluidStack, ItemStack itemStack) {
if (isGT5ULoaded) {
return GT_Utility.fillFluidContainer(fluidStack, itemStack, true, false);
}
return null;
}

public static ItemStack fillFluidContainerWithIFluidContainerItem(FluidStack fluidStack, ItemStack itemStack) {
if (itemStack.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem tContainerItem = (IFluidContainerItem) itemStack.getItem();
int tFilledAmount = tContainerItem.fill(itemStack, fluidStack, true);
if (tFilledAmount > 0) {
fluidStack.amount -= tFilledAmount;
return itemStack;
}
}
return null;
}

public static ItemStack getContainerForFilledItem(ItemStack itemStack) {
ItemStack stack = getContainerForFilledItemWithoutIFluidContainerItem(itemStack);
if (stack == null && itemStack.getItem() instanceof IFluidContainerItem container) {
stack = itemStack.copy();
container.drain(stack, Integer.MAX_VALUE, true);
}
if (stack == null) {
stack = FluidContainerRegistry.drainFluidContainer(itemStack.copy());
}
return stack;
}

public static ItemStack getContainerForFilledItemWithoutIFluidContainerItem(ItemStack itemStack) {
if (isGT5ULoaded) {
return GT_Utility.getContainerForFilledItem(itemStack, false);
}
return null;
}

public static int getRealCapacity(IFluidTank fluidTank) {
if (fluidTank instanceof IOverflowableTank) {
return ((IOverflowableTank) fluidTank).getRealCapacity();
}
return fluidTank.getCapacity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public FluidTankLong(Fluid fluid, long capacity) {
this(fluid, capacity, 0);
}

public FluidTankLong(long capacity) {
this(null, capacity, 0);
}

public FluidTankLong(Fluid fluid, long capacity, long amount) {
this.fluid = fluid;
this.capacity = capacity;
this.amount = amount;
}

@Override
public long drainLong(long maxDrain, boolean doDrain) {
long toDrain = Math.min(maxDrain, amount);
Expand Down Expand Up @@ -83,4 +88,9 @@ public void setFluid(Fluid fluid, long amount) {
this.fluid = fluid;
this.amount = amount;
}

@Override
public IFluidTankLong copy() {
return new FluidTankLong(getRealFluid(), getCapacityLong(), getFluidAmountLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;

public class FluidTankLongDelegate implements IFluidTankLong {
Expand Down Expand Up @@ -50,4 +51,9 @@ public void setFluid(Fluid fluid, long amount) {
delegate.drain(Integer.MAX_VALUE, true);
delegate.fill(new FluidStack(fluid, saturatedCast(amount)), true);
}

@Override
public IFluidTankLong copy() {
return new FluidTankLongDelegate(new FluidTank(delegate.getFluid(), delegate.getCapacity()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.utils.fluid;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -15,6 +16,18 @@ public FluidTanksHandler(IFluidTankLong tank) {
fluids = Collections.singletonList(tank);
}

public FluidTanksHandler(int size) {
this(size, 10000);
}

public FluidTanksHandler(int size, long capacity) {
FluidTankLong[] fluids = new FluidTankLong[size];
for (int i = 0; i < fluids.length; i++) {
fluids[i] = new FluidTankLong(capacity);
}
this.fluids = Arrays.asList(fluids);
}

@Override
public int getTanks() {
return fluids.size();
Expand Down
Loading

0 comments on commit c39ebc9

Please sign in to comment.