Fixed FluidUtil#tryFillContainer returning invalid result when simulating #9358

This commit is contained in:
brandon3055 authored and LexManos committed 2023-07-15 10:51:17 -07:00
1 parent 1ee53c0464
commit 09bc73d5cd
2 files changed
+33 -1

No files matched your search

@@ -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();
@@ -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 <a href="https://github.com/MinecraftForge/MinecraftForge/issues/6796">issue #6796</a>.
*/
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 <a href="https://github.com/MinecraftForge/MinecraftForge/issues/6796">issue #6796</a>.