From a26457143a8c015d5a3cd44d2d2b41961a806d7b Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Sun, 10 Dec 2023 11:43:55 +0100 Subject: [PATCH] feat: Add support for Fabric fluid storage https://github.com/TwelveIterationMods/CookingForBlockheads/issues/629 --- .../blay09/mods/balm/fabric/FabricBalm.java | 14 +++ .../balm/fabric/fluid/BalmFluidStorage.java | 89 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 fabric/src/main/java/net/blay09/mods/balm/fabric/fluid/BalmFluidStorage.java diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java index 55163971..9e61c35c 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java @@ -4,12 +4,15 @@ import net.blay09.mods.balm.api.config.AbstractBalmConfig; import net.blay09.mods.balm.api.container.BalmContainerProvider; import net.blay09.mods.balm.api.entity.BalmEntity; +import net.blay09.mods.balm.api.fluid.BalmFluidTankProvider; import net.blay09.mods.balm.api.fluid.FluidTank; import net.blay09.mods.balm.config.ExampleConfig; import net.blay09.mods.balm.config.ExampleConfigData; +import net.blay09.mods.balm.fabric.fluid.BalmFluidStorage; import net.blay09.mods.balm.fabric.provider.FabricBalmProviders; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage; import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; import net.minecraft.nbt.CompoundTag; @@ -40,5 +43,16 @@ public void onInitialize() { return null; }); + + FluidStorage.SIDED.registerFallback((world, pos, state, blockEntity, direction) -> { + if (blockEntity instanceof BalmFluidTankProvider fluidTankProvider) { + final var fluidTank = fluidTankProvider.getFluidTank(direction); + if (fluidTank != null) { + return new BalmFluidStorage(fluidTank); + } + } + + return null; + }); } } diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/fluid/BalmFluidStorage.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/fluid/BalmFluidStorage.java new file mode 100644 index 00000000..6f679dd5 --- /dev/null +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/fluid/BalmFluidStorage.java @@ -0,0 +1,89 @@ +package net.blay09.mods.balm.fabric.fluid; + +import com.google.common.primitives.Ints; +import net.blay09.mods.balm.api.fluid.FluidTank; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions; +import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; +import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant; + +public class BalmFluidStorage extends SnapshotParticipant> implements SingleSlotStorage { + + private final FluidTank fluidTank; + + public BalmFluidStorage(FluidTank fluidTank) { + this.fluidTank = fluidTank; + } + + @Override + public long insert(FluidVariant fluidVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(fluidVariant, maxAmount); + + if (getAmount() == 0) { + return fluidTank.fill(fluidVariant.getFluid(), Ints.saturatedCast(maxAmount), false); + } + + if (fluidVariant.isOf(getResource().getFluid())) { + // Otherwise we can only accept the same fluid as the current one. + long amountInserted = Math.min(maxAmount, getCapacity() - getAmount()); + return fluidTank.fill(fluidVariant.getFluid(), Ints.saturatedCast(amountInserted), false); + } else { + return 0; + } + } + + @Override + public long extract(FluidVariant fluidVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(fluidVariant, maxAmount); + + if (fluidVariant.isOf(getResource().getFluid())) { + long currentLevel = getAmount(); + long amountExtracted = Math.min(maxAmount, currentLevel); + return fluidTank.drain(fluidVariant.getFluid(), Ints.saturatedCast(amountExtracted), false); + } + + return 0; + } + + @Override + public boolean isResourceBlank() { + return getResource().isBlank(); + } + + @Override + public FluidVariant getResource() { + return FluidVariant.of(fluidTank.getFluid()); + } + + @Override + public long getAmount() { + return fluidTank.getAmount(); + } + + @Override + public long getCapacity() { + return fluidTank.getCapacity(); + } + + @Override + protected ResourceAmount createSnapshot() { + return new ResourceAmount(getResource(), getAmount()); + } + + @Override + protected void readSnapshot(ResourceAmount snapshot) { + fluidTank.setFluid(snapshot.resource().getFluid(), Ints.saturatedCast(snapshot.amount())); + } + + @Override + public void onFinalCommit() { + fluidTank.setChanged(); + } + + @Override + public String toString() { + return "BalmFluidStorage[" + fluidTank + "]"; + } +}