Skip to content

Commit c402d9c

Browse files
Copilotrubensworks
andauthored
Fix Dark Tank loot function: set capacity before inserting fluid, add game tests
Agent-Logs-Url: https://github.com/CyclopsMC/EvilCraft/sessions/b8e2ebee-6bde-4d7e-9591-545b7042754d Co-authored-by: rubensworks <440384+rubensworks@users.noreply.github.com>
1 parent 607161b commit c402d9c

3 files changed

Lines changed: 88 additions & 4 deletions

File tree

src/main/java/org/cyclops/evilcraft/EvilCraft.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ public Class<?>[] getGameTestClasses() {
470470
GameTestsBlockEternalWater.class,
471471
GameTestsBloodChest.class,
472472
GameTestsBloodInfuser.class,
473+
GameTestsDarkTank.class,
473474
GameTestsEntangledChalice.class,
474475
GameTestsItemEternalWater.class,
475476
GameTestsItemStacking.class,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.cyclops.evilcraft.gametest;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.gametest.framework.GameTestHelper;
5+
import net.minecraft.network.chat.Component;
6+
import net.minecraft.world.entity.EntityType;
7+
import net.minecraft.world.item.ItemStack;
8+
import net.neoforged.neoforge.capabilities.Capabilities;
9+
import net.neoforged.neoforge.fluids.FluidStack;
10+
import net.neoforged.neoforge.transfer.ResourceHandler;
11+
import net.neoforged.neoforge.transfer.access.ItemAccess;
12+
import net.neoforged.neoforge.transfer.fluid.FluidResource;
13+
import org.cyclops.cyclopscore.gametest.GameTest;
14+
import org.cyclops.evilcraft.Reference;
15+
import org.cyclops.evilcraft.RegistryEntries;
16+
import org.cyclops.evilcraft.blockentity.BlockEntityDarkTank;
17+
18+
public class GameTestsDarkTank {
19+
20+
public static final String TEMPLATE_EMPTY = Reference.MOD_ID + ":empty10";
21+
public static final BlockPos POS = BlockPos.ZERO.offset(2, 1, 2);
22+
23+
/**
24+
* Tests that breaking a large Dark Tank (144,000mB capacity) drops an item that preserves
25+
* the full capacity and fluid amount, not just the base 16,000mB capacity.
26+
*/
27+
@GameTest(template = TEMPLATE_EMPTY)
28+
public void testBreakLargeDarkTankFullPreservesCapacityAndFluid(GameTestHelper helper) {
29+
int largeCapacity = BlockEntityDarkTank.BASE_CAPACITY * 9;
30+
31+
// Place a Dark Tank
32+
helper.setBlock(POS, RegistryEntries.BLOCK_DARK_TANK.get());
33+
BlockEntityDarkTank tank = helper.getBlockEntity(POS, BlockEntityDarkTank.class);
34+
35+
// Set up as a large tank filled with blood
36+
tank.getTank().setCapacity(largeCapacity);
37+
tank.getTank().setFluid(new FluidStack(RegistryEntries.FLUID_BLOOD, largeCapacity));
38+
39+
// Break the block to trigger the loot table (which calls LootFunctionCopyTankData)
40+
helper.destroyBlock(POS);
41+
42+
helper.succeedWhen(() -> {
43+
ItemStack result = helper.findOneEntity(EntityType.ITEM).getItem();
44+
helper.assertValueEqual(result.getItem(), RegistryEntries.ITEM_DARK_TANK.get(), Component.literal("Dropped item should be a Dark Tank"));
45+
46+
ItemAccess itemAccess = ItemAccess.forStack(result);
47+
ResourceHandler<FluidResource> fluidHandler = result.getCapability(Capabilities.Fluid.ITEM, itemAccess);
48+
helper.assertTrue(fluidHandler != null, Component.literal("Dropped Dark Tank should have a fluid handler"));
49+
helper.assertValueEqual(fluidHandler.getCapacityAsInt(0, FluidResource.EMPTY), largeCapacity, Component.literal("Dropped Dark Tank should have large capacity"));
50+
helper.assertValueEqual(fluidHandler.getAmountAsInt(0), largeCapacity, Component.literal("Dropped Dark Tank should contain all fluid"));
51+
});
52+
}
53+
54+
/**
55+
* Tests that breaking an empty large Dark Tank preserves the large capacity.
56+
*/
57+
@GameTest(template = TEMPLATE_EMPTY)
58+
public void testBreakEmptyLargeDarkTankPreservesCapacity(GameTestHelper helper) {
59+
int largeCapacity = BlockEntityDarkTank.BASE_CAPACITY * 9;
60+
61+
// Place a Dark Tank
62+
helper.setBlock(POS, RegistryEntries.BLOCK_DARK_TANK.get());
63+
BlockEntityDarkTank tank = helper.getBlockEntity(POS, BlockEntityDarkTank.class);
64+
65+
// Set up as a large tank with no fluid
66+
tank.getTank().setCapacity(largeCapacity);
67+
68+
// Break the block
69+
helper.destroyBlock(POS);
70+
71+
helper.succeedWhen(() -> {
72+
ItemStack result = helper.findOneEntity(EntityType.ITEM).getItem();
73+
helper.assertValueEqual(result.getItem(), RegistryEntries.ITEM_DARK_TANK.get(), Component.literal("Dropped item should be a Dark Tank"));
74+
75+
ItemAccess itemAccess = ItemAccess.forStack(result);
76+
ResourceHandler<FluidResource> fluidHandler = result.getCapability(Capabilities.Fluid.ITEM, itemAccess);
77+
helper.assertTrue(fluidHandler != null, Component.literal("Dropped Dark Tank should have a fluid handler"));
78+
helper.assertValueEqual(fluidHandler.getCapacityAsInt(0, FluidResource.EMPTY), largeCapacity, Component.literal("Empty dropped Dark Tank should preserve large capacity"));
79+
helper.assertValueEqual(fluidHandler.getAmountAsInt(0), 0, Component.literal("Dropped Dark Tank should be empty"));
80+
});
81+
}
82+
83+
}

src/main/java/org/cyclops/evilcraft/loot/functions/LootFunctionCopyTankData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public ItemStack run(ItemStack itemStack, LootContext lootContext) {
3939
return Optional.ofNullable(itemAccess.getCapability(Capabilities.Fluid.ITEM))
4040
.map(fluidHandlerItem -> {
4141
try (var tx = Transaction.openRoot()) {
42+
if (fluidHandlerItem instanceof IFluidHandlerCapacity) {
43+
((IFluidHandlerCapacity) fluidHandlerItem).setTankCapacity(0, fluidHandlerTile.getTankCapacity(0), tx);
44+
}
4245
FluidResource resource = fluidHandlerTile.getResource(0);
4346
if (!resource.isEmpty()) {
4447
fluidHandlerItem.insert(resource, fluidHandlerTile.getFluidAmount(), tx);
45-
if (fluidHandlerItem instanceof IFluidHandlerCapacity) {
46-
((IFluidHandlerCapacity) fluidHandlerItem).setTankCapacity(0, fluidHandlerTile.getTankCapacity(0), tx);
47-
}
48-
tx.commit();
4948
}
49+
tx.commit();
5050
}
5151
return itemAccess.getResource().toStack(itemAccess.getAmount());
5252
}).orElse(itemStack);

0 commit comments

Comments
 (0)