Skip to content

Commit 911672c

Browse files
committed
fix: Fix chestLootHoldsBlockheadSeeds not working reliably on modded loot chests
1 parent 2f45c04 commit 911672c

2 files changed

Lines changed: 45 additions & 40 deletions

File tree

common/src/main/java/net/blay09/mods/pantryforblockheads/loot/ModLootModifiers.java

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package net.blay09.mods.pantryforblockheads.loot;
22

33
import net.blay09.mods.balm.Balm;
4-
import net.blay09.mods.balm.world.level.storage.loot.UnpackedLootTableHolder;
4+
import net.blay09.mods.balm.world.level.storage.loot.BalmLootModifier;
55
import net.blay09.mods.pantryforblockheads.PantryForBlockheads;
66
import net.blay09.mods.pantryforblockheads.tag.ModBlockTags;
7-
import net.minecraft.core.BlockPos;
87
import net.minecraft.core.registries.Registries;
98
import net.minecraft.resources.Identifier;
109
import net.minecraft.resources.ResourceKey;
11-
import net.minecraft.util.Util;
10+
import net.minecraft.world.item.ItemStack;
1211
import net.minecraft.world.level.block.state.BlockState;
12+
import net.minecraft.world.level.storage.loot.LootContext;
13+
import net.minecraft.world.level.storage.loot.LootTable;
1314
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
15+
import org.jspecify.annotations.Nullable;
16+
17+
import java.util.List;
1418

1519
public final class ModLootModifiers {
1620
private static final ThreadLocal<Boolean> isApplyingSeedChestLoot = ThreadLocal.withInitial(() -> false);
@@ -20,50 +24,51 @@ private ModLootModifiers() {
2024
}
2125

2226
public static void initialize() {
23-
Balm.lootModifiers().registerLootModifier(PantryForBlockheads.id("seed_chest_loot"), (context, loot) -> {
24-
if (isApplyingSeedChestLoot.get() || !context.hasParameter(LootContextParams.ORIGIN)) {
25-
return;
26-
}
27+
Balm.lootModifiers().registerLootModifier(PantryForBlockheads.id("seed_chest_loot"), new BalmLootModifier() {
28+
@Override
29+
public void apply(LootContext context, List<ItemStack> loot, @Nullable ResourceKey<LootTable> lootTableId) {
30+
if (isApplyingSeedChestLoot.get() || lootTableId == null) {
31+
return;
32+
}
2733

28-
final var blockEntity = context.getLevel().getBlockEntity(BlockPos.containing(context.getParameter(LootContextParams.ORIGIN)));
29-
if (blockEntity instanceof UnpackedLootTableHolder unpackedLootTableHolder) {
30-
unpackedLootTableHolder.balm$getUnpackedLootTable().ifPresent(lootTableId -> {
31-
if (PantryForBlockheads.config().vanillaModifications.chestLootHoldsBlockheadSeeds) {
32-
final var level = context.getLevel();
33-
final var additionsLootTable = Identifier.fromNamespaceAndPath(PantryForBlockheads.MOD_ID, lootTableId.identifier().getPath()).withPrefix("loot_additions/");
34-
final var lootTable = level.getServer().reloadableRegistries().getLootTable(ResourceKey.create(Registries.LOOT_TABLE, additionsLootTable));
35-
isApplyingSeedChestLoot.set(true);
36-
try {
37-
lootTable.getRandomItems(context, loot::add);
38-
} finally {
39-
isApplyingSeedChestLoot.set(false);
40-
}
34+
if (PantryForBlockheads.config().vanillaModifications.chestLootHoldsBlockheadSeeds) {
35+
final var level = context.getLevel();
36+
final var additionsLootTable = Identifier.fromNamespaceAndPath(PantryForBlockheads.MOD_ID, lootTableId.identifier().getPath()).withPrefix("loot_additions/");
37+
final var lootTable = level.getServer().reloadableRegistries().getLootTable(ResourceKey.create(Registries.LOOT_TABLE, additionsLootTable));
38+
isApplyingSeedChestLoot.set(true);
39+
try {
40+
lootTable.getRandomItems(context, loot::add);
41+
} finally {
42+
isApplyingSeedChestLoot.set(false);
4143
}
42-
});
44+
}
4345
}
4446
});
4547

46-
Balm.lootModifiers().registerLootModifier(PantryForBlockheads.id("grass_seeds"), (context, loot) -> {
47-
if (isApplyingGrassSeeds.get()) {
48-
return;
49-
}
48+
Balm.lootModifiers().registerLootModifier(PantryForBlockheads.id("grass_seeds"), new BalmLootModifier() {
49+
@Override
50+
public void apply(LootContext context, List<ItemStack> loot, @Nullable ResourceKey<LootTable> lootTableId) {
51+
if (isApplyingGrassSeeds.get()) {
52+
return;
53+
}
5054

51-
if (!PantryForBlockheads.config().vanillaModifications.grassDropsBlockheadSeeds) {
52-
return;
53-
}
55+
if (!PantryForBlockheads.config().vanillaModifications.grassDropsBlockheadSeeds) {
56+
return;
57+
}
5458

55-
final BlockState blockState = context.getOptionalParameter(LootContextParams.BLOCK_STATE);
56-
if (blockState == null || !blockState.is(ModBlockTags.DROPS_SEEDS)) {
57-
return;
58-
}
59+
final BlockState blockState = context.getOptionalParameter(LootContextParams.BLOCK_STATE);
60+
if (blockState == null || !blockState.is(ModBlockTags.DROPS_SEEDS)) {
61+
return;
62+
}
5963

60-
final var level = context.getLevel();
61-
final var grassSeedLootTable = level.getServer().reloadableRegistries().getLootTable(ModLootTables.GRASS_SEEDS);
62-
isApplyingGrassSeeds.set(true);
63-
try {
64-
grassSeedLootTable.getRandomItems(context, loot::add);
65-
} finally {
66-
isApplyingGrassSeeds.set(false);
64+
final var level = context.getLevel();
65+
final var grassSeedLootTable = level.getServer().reloadableRegistries().getLootTable(ModLootTables.GRASS_SEEDS);
66+
isApplyingGrassSeeds.set(true);
67+
try {
68+
grassSeedLootTable.getRandomItems(context, loot::add);
69+
} finally {
70+
isApplyingGrassSeeds.set(false);
71+
}
6772
}
6873
});
6974
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
balm = "26.1.2.2"
2+
balm = "26.1.2.3-SNAPSHOT"
33
minecraft = "26.1.2"
44
neoForm = "26.1.2-1"
55
neoForge = "26.1.2.12-beta"

0 commit comments

Comments
 (0)