From 00fe453cb9632de7b35fac70d682200bc63fb2f6 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 8 Oct 2025 22:34:02 +0200 Subject: [PATCH] Fix #1152: EU is left in a hidden buffer when Diesel Generator runs out of fuel --- .../FluidItemConsumerComponent.java | 4 ++ .../test/GeneratorTests.java | 63 +++++++++++++++++++ .../test/framework/MIGameTestHelper.java | 14 +++++ .../test/framework/MIGameTests.java | 4 +- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/main/java/aztech/modern_industrialization/test/GeneratorTests.java diff --git a/src/main/java/aztech/modern_industrialization/machines/components/FluidItemConsumerComponent.java b/src/main/java/aztech/modern_industrialization/machines/components/FluidItemConsumerComponent.java index ed6c7ad16..5df6ade2c 100644 --- a/src/main/java/aztech/modern_industrialization/machines/components/FluidItemConsumerComponent.java +++ b/src/main/java/aztech/modern_industrialization/machines/components/FluidItemConsumerComponent.java @@ -117,6 +117,10 @@ public long getEuProduction(List fluidInputs, long euProduced = 0; + // Consume from the buffer first + euProduced += euBuffer; + euBuffer = 0; + for (ConfigurableFluidStack stack : fluidInputs) { Fluid fluid = stack.getResource().getFluid(); if (fluidEUProductionMap.accept(fluid) && stack.getAmount() > 0) { diff --git a/src/main/java/aztech/modern_industrialization/test/GeneratorTests.java b/src/main/java/aztech/modern_industrialization/test/GeneratorTests.java new file mode 100644 index 000000000..676fa0910 --- /dev/null +++ b/src/main/java/aztech/modern_industrialization/test/GeneratorTests.java @@ -0,0 +1,63 @@ +/* + * MIT License + * + * Copyright (c) 2020 Azercoco & Technici4n + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package aztech.modern_industrialization.test; + +import aztech.modern_industrialization.MI; +import aztech.modern_industrialization.MIFluids; +import aztech.modern_industrialization.machines.blockentities.GeneratorMachineBlockEntity; +import aztech.modern_industrialization.test.framework.MIGameTest; +import aztech.modern_industrialization.test.framework.MIGameTestHelper; +import aztech.modern_industrialization.thirdparty.fabrictransfer.api.transaction.Transaction; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; + +public class GeneratorTests { + /** + * Regression test for issue 1152: + * "EU is left in a hidden buffer when Diesel Generator runs out of fuel". + */ + @MIGameTest + public void testGeneratorEmptiesEuBuffer(MIGameTestHelper helper) { + var generatorPos = new BlockPos(0, 1, 0); + var lvDieselGeneratorBlock = BuiltInRegistries.BLOCK.getOptional(MI.id("lv_diesel_generator")).orElseThrow(); + helper.setBlock(generatorPos, lvDieselGeneratorBlock); + + var dieselGenerator = (GeneratorMachineBlockEntity) helper.getBlockEntity(generatorPos); + + try (var tx = Transaction.openOuter()) { + long inserted = dieselGenerator.getInventory().fluidStorage.insert(MIFluids.BIODIESEL.variant(), 1, tx); + helper.assertValueEqual(inserted, 1L, "inserted biodiesel"); + tx.commit(); + } + + // Should produce 64 per tick: 192 after 3 ticks and only 250 (the biodiesel value) after 4 ticks + helper.startSequence() + .thenIdle(3) + .thenExecute(() -> helper.assertEnergy(generatorPos, 192, Direction.NORTH)) + .thenIdle(1) + .thenExecute(() -> helper.assertEnergy(generatorPos, 250, Direction.NORTH)) + .thenSucceed(); + } +} diff --git a/src/main/java/aztech/modern_industrialization/test/framework/MIGameTestHelper.java b/src/main/java/aztech/modern_industrialization/test/framework/MIGameTestHelper.java index 4c9f27c36..7151305be 100644 --- a/src/main/java/aztech/modern_industrialization/test/framework/MIGameTestHelper.java +++ b/src/main/java/aztech/modern_industrialization/test/framework/MIGameTestHelper.java @@ -24,6 +24,7 @@ package aztech.modern_industrialization.test.framework; import aztech.modern_industrialization.MIBlock; +import aztech.modern_industrialization.api.energy.EnergyApi; import aztech.modern_industrialization.blocks.storage.tank.creativetank.CreativeTankBlockEntity; import aztech.modern_industrialization.materials.MIMaterials; import aztech.modern_industrialization.materials.Material; @@ -35,12 +36,14 @@ import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant; import java.util.function.Consumer; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.gametest.framework.GameTestInfo; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.capabilities.Capabilities; +import org.jetbrains.annotations.Nullable; public class MIGameTestHelper extends GameTestHelper { public MIGameTestHelper(GameTestInfo testInfo) { @@ -122,4 +125,15 @@ public void assertNoFluid(BlockPos pos) { } } } + + public void assertEnergy(BlockPos pos, long energy, @Nullable Direction side) { + var miEnergyHandler = getLevel().getCapability(EnergyApi.SIDED, absolutePos(pos), side); + if (miEnergyHandler == null) { + fail("Could not find energy handler", pos); + } + long storedEnergy = miEnergyHandler.getAmount(); + if (storedEnergy != energy) { + fail("Expected energy to be " + energy + ", was " + storedEnergy, pos); + } + } } diff --git a/src/main/java/aztech/modern_industrialization/test/framework/MIGameTests.java b/src/main/java/aztech/modern_industrialization/test/framework/MIGameTests.java index f4a7288fd..25ea4f0f5 100644 --- a/src/main/java/aztech/modern_industrialization/test/framework/MIGameTests.java +++ b/src/main/java/aztech/modern_industrialization/test/framework/MIGameTests.java @@ -25,6 +25,7 @@ import aztech.modern_industrialization.MI; import aztech.modern_industrialization.test.FluidPipeTests; +import aztech.modern_industrialization.test.GeneratorTests; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -37,7 +38,8 @@ private MIGameTests() { } private static final List> TEST_CLASSES = List.of( - FluidPipeTests.class); + FluidPipeTests.class, + GeneratorTests.class); @GameTestGenerator public static List generateTests() {