Skip to content

Commit

Permalink
Fixed tanks taking all the fluid of one type even if it can't contain…
Browse files Browse the repository at this point in the history
… all of it
  • Loading branch information
K0LALA committed Jul 11, 2024
1 parent a5fafbf commit 6d5198c
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static FluidStack tryTransfer(Storage<FluidVariant> input, Storage<FluidV

// check how much can be extracted
try (Transaction extractionTestTransaction = Transaction.openOuter()) {
maxExtracted = view.extract(resource, maxFill, extractionTestTransaction);
maxExtracted = input.extract(resource, maxFill, extractionTestTransaction);
extractionTestTransaction.abort();
}

Expand All @@ -100,7 +101,7 @@ public static FluidStack tryTransfer(Storage<FluidVariant> input, Storage<FluidV
long accepted = output.insert(resource, maxExtracted, transferTransaction);

// extract it, or rollback if the amounts don't match
long drained = view.extract(resource, accepted, transferTransaction);
long drained = input.extract(resource, accepted, transferTransaction);
if (drained != accepted) {
Mantle.logger.error("Lost {} fluid during transfer", drained - accepted);
}
Expand Down Expand Up @@ -210,12 +211,12 @@ public static boolean interactWithFluidItem(Level world, BlockPos pos, Player pl
if (!world.isClientSide) {
Storage<FluidVariant> itemHandler = ContainerItemContext.forPlayerInteraction(player, hand).find(FluidStorage.ITEM);
// first, try filling the TE from the item
FluidStack transferred = tryTransfer(itemHandler, teHandler, Long.MAX_VALUE);
FluidStack transferred = tryTransfer(itemHandler, teHandler, (itemHandler instanceof SingleSlotStorage ? ((SingleSlotStorage<FluidVariant>) itemHandler).getCapacity() : Long.MAX_VALUE));
if (!transferred.isEmpty()) {
playEmptySound(world, pos, player, transferred);
} else {
// if that failed, try filling the item handler from the TE
transferred = tryTransfer(teHandler, itemHandler, Integer.MAX_VALUE);
transferred = tryTransfer(teHandler, itemHandler, (itemHandler instanceof SingleSlotStorage ? ((SingleSlotStorage<FluidVariant>) itemHandler).getCapacity() : Integer.MAX_VALUE));
if (!transferred.isEmpty()) {
playFillSound(world, pos, player, transferred);
}
Expand Down

0 comments on commit 6d5198c

Please sign in to comment.