Skip to content

Commit

Permalink
feat: Add support for Fabric fluid storage TwelveIterationMods/Cookin…
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 10, 2023
1 parent 833636e commit a264571
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
}
Original file line number Diff line number Diff line change
@@ -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<ResourceAmount<FluidVariant>> implements SingleSlotStorage<FluidVariant> {

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<FluidVariant> createSnapshot() {
return new ResourceAmount(getResource(), getAmount());
}

@Override
protected void readSnapshot(ResourceAmount<FluidVariant> snapshot) {
fluidTank.setFluid(snapshot.resource().getFluid(), Ints.saturatedCast(snapshot.amount()));
}

@Override
public void onFinalCommit() {
fluidTank.setChanged();
}

@Override
public String toString() {
return "BalmFluidStorage[" + fluidTank + "]";
}
}

0 comments on commit a264571

Please sign in to comment.