Skip to content

Commit deb878f

Browse files
authored
Fix #1152: EU is left in a hidden buffer when Diesel Generator runs out of fuel (#1185)
1 parent cfcc323 commit deb878f

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/main/java/aztech/modern_industrialization/machines/components/FluidItemConsumerComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public long getEuProduction(List<ConfigurableFluidStack> fluidInputs,
117117

118118
long euProduced = 0;
119119

120+
// Consume from the buffer first
121+
euProduced += euBuffer;
122+
euBuffer = 0;
123+
120124
for (ConfigurableFluidStack stack : fluidInputs) {
121125
Fluid fluid = stack.getResource().getFluid();
122126
if (fluidEUProductionMap.accept(fluid) && stack.getAmount() > 0) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Azercoco & Technici4n
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package aztech.modern_industrialization.test;
25+
26+
import aztech.modern_industrialization.MI;
27+
import aztech.modern_industrialization.MIFluids;
28+
import aztech.modern_industrialization.machines.blockentities.GeneratorMachineBlockEntity;
29+
import aztech.modern_industrialization.test.framework.MIGameTest;
30+
import aztech.modern_industrialization.test.framework.MIGameTestHelper;
31+
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.transaction.Transaction;
32+
import net.minecraft.core.BlockPos;
33+
import net.minecraft.core.Direction;
34+
import net.minecraft.core.registries.BuiltInRegistries;
35+
36+
public class GeneratorTests {
37+
/**
38+
* Regression test for <a href="https://github.com/AztechMC/Modern-Industrialization/issues/1152">issue 1152</a>:
39+
* "EU is left in a hidden buffer when Diesel Generator runs out of fuel".
40+
*/
41+
@MIGameTest
42+
public void testGeneratorEmptiesEuBuffer(MIGameTestHelper helper) {
43+
var generatorPos = new BlockPos(0, 1, 0);
44+
var lvDieselGeneratorBlock = BuiltInRegistries.BLOCK.getOptional(MI.id("lv_diesel_generator")).orElseThrow();
45+
helper.setBlock(generatorPos, lvDieselGeneratorBlock);
46+
47+
var dieselGenerator = (GeneratorMachineBlockEntity) helper.getBlockEntity(generatorPos);
48+
49+
try (var tx = Transaction.openOuter()) {
50+
long inserted = dieselGenerator.getInventory().fluidStorage.insert(MIFluids.BIODIESEL.variant(), 1, tx);
51+
helper.assertValueEqual(inserted, 1L, "inserted biodiesel");
52+
tx.commit();
53+
}
54+
55+
// Should produce 64 per tick: 192 after 3 ticks and only 250 (the biodiesel value) after 4 ticks
56+
helper.startSequence()
57+
.thenIdle(3)
58+
.thenExecute(() -> helper.assertEnergy(generatorPos, 192, Direction.NORTH))
59+
.thenIdle(1)
60+
.thenExecute(() -> helper.assertEnergy(generatorPos, 250, Direction.NORTH))
61+
.thenSucceed();
62+
}
63+
}

src/main/java/aztech/modern_industrialization/test/framework/MIGameTestHelper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package aztech.modern_industrialization.test.framework;
2525

2626
import aztech.modern_industrialization.MIBlock;
27+
import aztech.modern_industrialization.api.energy.EnergyApi;
2728
import aztech.modern_industrialization.blocks.storage.tank.creativetank.CreativeTankBlockEntity;
2829
import aztech.modern_industrialization.materials.MIMaterials;
2930
import aztech.modern_industrialization.materials.Material;
@@ -35,12 +36,14 @@
3536
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant;
3637
import java.util.function.Consumer;
3738
import net.minecraft.core.BlockPos;
39+
import net.minecraft.core.Direction;
3840
import net.minecraft.core.registries.BuiltInRegistries;
3941
import net.minecraft.gametest.framework.GameTestHelper;
4042
import net.minecraft.gametest.framework.GameTestInfo;
4143
import net.minecraft.world.level.block.Blocks;
4244
import net.minecraft.world.level.material.Fluid;
4345
import net.neoforged.neoforge.capabilities.Capabilities;
46+
import org.jetbrains.annotations.Nullable;
4447

4548
public class MIGameTestHelper extends GameTestHelper {
4649
public MIGameTestHelper(GameTestInfo testInfo) {
@@ -122,4 +125,15 @@ public void assertNoFluid(BlockPos pos) {
122125
}
123126
}
124127
}
128+
129+
public void assertEnergy(BlockPos pos, long energy, @Nullable Direction side) {
130+
var miEnergyHandler = getLevel().getCapability(EnergyApi.SIDED, absolutePos(pos), side);
131+
if (miEnergyHandler == null) {
132+
fail("Could not find energy handler", pos);
133+
}
134+
long storedEnergy = miEnergyHandler.getAmount();
135+
if (storedEnergy != energy) {
136+
fail("Expected energy to be " + energy + ", was " + storedEnergy, pos);
137+
}
138+
}
125139
}

src/main/java/aztech/modern_industrialization/test/framework/MIGameTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import aztech.modern_industrialization.MI;
2727
import aztech.modern_industrialization.test.FluidPipeTests;
28+
import aztech.modern_industrialization.test.GeneratorTests;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.Locale;
@@ -37,7 +38,8 @@ private MIGameTests() {
3738
}
3839

3940
private static final List<Class<?>> TEST_CLASSES = List.of(
40-
FluidPipeTests.class);
41+
FluidPipeTests.class,
42+
GeneratorTests.class);
4143

4244
@GameTestGenerator
4345
public static List<TestFunction> generateTests() {

0 commit comments

Comments
 (0)