diff --git a/src/main/java/net/minecraftforge/fluids/FluidUtil.java b/src/main/java/net/minecraftforge/fluids/FluidUtil.java
index bb683e43a4..9f91541dee 100644
--- a/src/main/java/net/minecraftforge/fluids/FluidUtil.java
+++ b/src/main/java/net/minecraftforge/fluids/FluidUtil.java
@@ -146,7 +146,9 @@ public class FluidUtil
}
else
{
- containerFluidHandler.fill(simulatedTransfer, IFluidHandler.FluidAction.SIMULATE);
+ // We are acting on a COPY of the stack, so performing changes on the source is acceptable even if we are simulating.
+ // We need to perform the change otherwise the call to getContainer() will be incorrect.
+ containerFluidHandler.fill(simulatedTransfer, IFluidHandler.FluidAction.EXECUTE);
}
ItemStack resultContainer = containerFluidHandler.getContainer();
diff --git a/src/test/java/net/minecraftforge/debug/FluidUtilTest.java b/src/test/java/net/minecraftforge/debug/FluidUtilTest.java
index c8fd82870d..a46f2bcc62 100644
--- a/src/test/java/net/minecraftforge/debug/FluidUtilTest.java
+++ b/src/test/java/net/minecraftforge/debug/FluidUtilTest.java
@@ -38,6 +38,7 @@ public class FluidUtilTest
private static void runTests(FMLCommonSetupEvent commonSetupEvent)
{
test_tryEmptyContainer();
+ test_tryFillContainer();
test_tryEmptyContainerAndStow_stackable();
LogManager.getLogger().info("FluidUtilTest ok!");
@@ -70,6 +71,35 @@ public class FluidUtilTest
checkItemStack(sourceStack, Items.WATER_BUCKET, 2); // Apparently the stack is not supposed to be modified
}
+ /**
+ * Ensures that tryFillContainer doesn't change the target fluid handler when simulating.
+ * Ant that the result of the simulated transfver is valid.
+ * Regression test for the root cause of issue #6796.
+ */
+ private static void test_tryFillContainer()
+ {
+ var targetStack = new ItemStack(Items.BUCKET, 2);
+ var sourceTank = new FluidTank(10000);
+ sourceTank.setFluid(new FluidStack(Fluids.WATER, 5000));
+
+ // Simulate is not supposed to modify anything
+ var simulateResult = FluidUtil.tryFillContainer(targetStack, sourceTank, 1000, null, false);
+ if (!simulateResult.isSuccess())
+ throw new AssertionError("Failed to transfer.");
+ checkItemStack(simulateResult.getResult(), Items.WATER_BUCKET, 1);
+ // Tank and stack shouldn't be modified
+ checkItemStack(targetStack, Items.BUCKET, 2);
+ checkFluidStack(sourceTank.getFluid(), Fluids.WATER, 5000);
+
+ // Execute should modify
+ var executeResult = FluidUtil.tryFillContainer(targetStack, sourceTank, 1000, null, true);
+ if (!executeResult.isSuccess())
+ throw new AssertionError("Failed to transfer.");
+ checkItemStack(executeResult.getResult(), Items.WATER_BUCKET, 1);
+ checkFluidStack(sourceTank.getFluid(), Fluids.WATER, 4000);
+ checkItemStack(targetStack, Items.BUCKET, 2);
+ }
+
/**
* Ensures that tryEmptyContainerAndStow doesn't duplicate fluids in the target when the container is stackable.
* Regression test for issue #6796.