From a1aacf25fef1fb9d0020790b07db85ad287a9338 Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 10 Oct 2023 23:41:21 -0700 Subject: [PATCH] Fix loot tables not having their ids set. --- .../storage/loot/LootDataType.java.patch | 8 +- .../loot_modifiers/global_loot_modifiers.json | 2 +- .../loot_modifiers/dungeon_loot.json | 9 --- .../loot_modifiers/multiply_loot.json | 9 +++ .../loot_tables/blocks/test.json | 21 ++++++ .../loot/GlobalLootModifiersTest.java | 73 ++++++++++++++++--- 6 files changed, 100 insertions(+), 22 deletions(-) delete mode 100644 src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/dungeon_loot.json create mode 100644 src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/multiply_loot.json create mode 100644 src/generated_test/resources/global_loot_test/data/global_loot_test/loot_tables/blocks/test.json diff --git a/patches/minecraft/net/minecraft/world/level/storage/loot/LootDataType.java.patch b/patches/minecraft/net/minecraft/world/level/storage/loot/LootDataType.java.patch index 0ee807b4c6..c37a47e904 100644 --- a/patches/minecraft/net/minecraft/world/level/storage/loot/LootDataType.java.patch +++ b/patches/minecraft/net/minecraft/world/level/storage/loot/LootDataType.java.patch @@ -1,13 +1,15 @@ --- a/net/minecraft/world/level/storage/loot/LootDataType.java +++ b/net/minecraft/world/level/storage/loot/LootDataType.java -@@ -42,7 +_,10 @@ +@@ -42,7 +_,12 @@ dataresult.error().ifPresent((p_297003_) -> { LOGGER.error("Couldn't parse element {}:{} - {}", this.directory, p_279253_, p_297003_.message()); }); - return dataresult.result(); + var ret = dataresult.result(); -+ if (this == TABLE && ret.isPresent()) -+ ret = Optional.ofNullable((T)net.minecraftforge.event.ForgeEventFactory.onLoadLootTable(p_279253_, (LootTable)ret.get())); ++ if (ret.orElse(null) instanceof LootTable table) { ++ table.setLootTableId(p_279253_); ++ ret = Optional.ofNullable((T)net.minecraftforge.event.ForgeEventFactory.onLoadLootTable(p_279253_, table)); ++ } + return ret; } diff --git a/src/generated_test/resources/global_loot_test/data/forge/loot_modifiers/global_loot_modifiers.json b/src/generated_test/resources/global_loot_test/data/forge/loot_modifiers/global_loot_modifiers.json index 86a059a62d..2ee3765d0c 100644 --- a/src/generated_test/resources/global_loot_test/data/forge/loot_modifiers/global_loot_modifiers.json +++ b/src/generated_test/resources/global_loot_test/data/forge/loot_modifiers/global_loot_modifiers.json @@ -1,7 +1,7 @@ { "entries": [ - "global_loot_test:dungeon_loot", "global_loot_test:bamboo_silk", + "global_loot_test:multiply_loot", "global_loot_test:wheat_harvest", "global_loot_test:smelting" ], diff --git a/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/dungeon_loot.json b/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/dungeon_loot.json deleted file mode 100644 index 5dba5b78d3..0000000000 --- a/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/dungeon_loot.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "type": "global_loot_test:dungeon_loot", - "conditions": [ - { - "condition": "forge:loot_table_id", - "loot_table_id": "minecraft:chests/simple_dungeon" - } - ] -} \ No newline at end of file diff --git a/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/multiply_loot.json b/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/multiply_loot.json new file mode 100644 index 0000000000..1b638b2e37 --- /dev/null +++ b/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_modifiers/multiply_loot.json @@ -0,0 +1,9 @@ +{ + "type": "global_loot_test:multiply_loot", + "conditions": [ + { + "condition": "forge:loot_table_id", + "loot_table_id": "global_loot_test:blocks/test" + } + ] +} \ No newline at end of file diff --git a/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_tables/blocks/test.json b/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_tables/blocks/test.json new file mode 100644 index 0000000000..9a0c146055 --- /dev/null +++ b/src/generated_test/resources/global_loot_test/data/global_loot_test/loot_tables/blocks/test.json @@ -0,0 +1,21 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "bonus_rolls": 0.0, + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ], + "entries": [ + { + "type": "minecraft:item", + "name": "global_loot_test:test" + } + ], + "rolls": 1.0 + } + ], + "random_sequence": "global_loot_test:blocks/test" +} \ No newline at end of file diff --git a/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java b/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java index 16fe622ceb..1b4c1d23a8 100644 --- a/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java +++ b/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java @@ -14,8 +14,12 @@ import net.minecraft.advancements.critereon.ItemPredicate; import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.core.BlockPos; import net.minecraft.util.ExtraCodecs; +import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.data.PackOutput; +import net.minecraft.data.loot.BlockLootSubProvider; +import net.minecraft.data.loot.LootTableProvider; import net.minecraft.gametest.framework.GameTest; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.world.item.enchantment.Enchantment; @@ -24,7 +28,9 @@ import net.minecraft.world.item.enchantment.EnchantmentCategory; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.flag.FeatureFlags; import net.minecraft.world.SimpleContainer; +import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; @@ -36,7 +42,7 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition; import net.minecraft.world.level.storage.loot.predicates.LootItemCondition; import net.minecraft.world.level.storage.loot.predicates.MatchTool; -import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.common.crafting.conditions.IConditionBuilder; import net.minecraftforge.common.data.GlobalLootModifierProvider; import net.minecraftforge.common.loot.IGlobalLootModifier; import net.minecraftforge.common.loot.LootModifier; @@ -52,7 +58,9 @@ import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.NotNull; +import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -63,7 +71,7 @@ public class GlobalLootModifiersTest extends BaseTestMod { private static final DeferredRegister> GLM = DeferredRegister.create(ForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, MODID); static { - GLM.register("dungeon_loot", DungeonLootEnhancerModifier.CODEC); + GLM.register("multiply_loot", MultiplyDropsModifier.CODEC); GLM.register("smelting", SmeltingEnchantmentModifier.CODEC); GLM.register("wheat_harvest", WheatSeedsConverterModifier.CODEC); GLM.register("silk_touch_bamboo", SilkTouchTestModifier.CODEC); @@ -72,6 +80,11 @@ public class GlobalLootModifiersTest extends BaseTestMod { private static final DeferredRegister ENCHANTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, MODID); private static final RegistryObject SMELT = ENCHANTS.register("smelt", () -> new SmelterEnchantment(Rarity.UNCOMMON, EnchantmentCategory.DIGGER, EquipmentSlot.MAINHAND)); + private static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + private static final RegistryObject TEST_BLOCK = BLOCKS.register("test", () -> new Block(BlockBehaviour.Properties.of())); + private static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + private static final RegistryObject TEST_ITEM = ITEMS.register("test", () -> new BlockItem(TEST_BLOCK.get(), new Item.Properties())); + public GlobalLootModifiersTest() { testItem(() -> { var smelt = new ItemStack(Items.IRON_AXE); @@ -83,13 +96,13 @@ public class GlobalLootModifiersTest extends BaseTestMod { @SubscribeEvent public void runData(GatherDataEvent event) { event.getGenerator().addProvider(event.includeServer(), new DataProvider(event.getGenerator().getPackOutput(), MODID)); + event.getGenerator().addProvider(event.includeServer(), new LootProvider(event.getGenerator().getPackOutput())); } @GameTest(template = "forge:empty3x3x3") public static void test_smelting_modifier(GameTestHelper helper) { var center = new BlockPos(1, 1, 1); helper.setBlock(center, Blocks.OAK_LOG); - helper.assertBlock(center, block -> block == Blocks.OAK_LOG, "Failed to set log"); var smelt = new ItemStack(Items.IRON_AXE); EnchantmentHelper.setEnchantments(Map.of(SMELT.get(), 1), smelt); @@ -103,6 +116,24 @@ public class GlobalLootModifiersTest extends BaseTestMod { helper.succeed(); } + @GameTest(template = "forge:empty3x3x3") + public static void table_name_condition(GameTestHelper helper) { + var center = new BlockPos(1, 1, 1); + var player = helper.makeMockServerPlayer(); + + // Should be doubled + helper.setBlock(center, TEST_BLOCK.get()); + player.gameMode.destroyBlock(helper.absolutePos(center)); + helper.assertItemEntityCountIs(TEST_ITEM.get(), center, 1.0, 2); + + // Should not be modified + helper.setBlock(center, Blocks.OAK_LOG); + player.gameMode.destroyBlock(helper.absolutePos(center)); + helper.assertItemEntityCountIs(Items.OAK_LOG, center, 1.0, 1); + + helper.succeed(); + } + private static class DataProvider extends GlobalLootModifierProvider { public DataProvider(PackOutput output, String modid) { super(output, modid); @@ -128,9 +159,9 @@ public class GlobalLootModifiersTest extends BaseTestMod { 3, Items.WHEAT_SEEDS, Items.WHEAT) ); - add("dungeon_loot", new DungeonLootEnhancerModifier( + add("multiply_loot", new MultiplyDropsModifier( new LootItemCondition[] { - LootTableIdCondition.builder(new ResourceLocation("chests/simple_dungeon")).build() + LootTableIdCondition.builder(TEST_BLOCK.get().getLootTable()).build() }, 2) ); @@ -143,6 +174,30 @@ public class GlobalLootModifiersTest extends BaseTestMod { } } + private static class LootProvider extends LootTableProvider { + public LootProvider(PackOutput out) { + super(out, Set.of(), List.of( + new LootTableProvider.SubProviderEntry(BlockLoot::new, LootContextParamSets.BLOCK) + )); + } + + private static class BlockLoot extends BlockLootSubProvider implements IConditionBuilder { + public BlockLoot() { + super(Set.of(), FeatureFlags.REGISTRY.allFlags()); + } + + @Override + protected Iterable getKnownBlocks() { + return List.of(TEST_BLOCK.get()); + } + + @Override + protected void generate() { + this.dropSelf(TEST_BLOCK.get()); + } + } + } + private static class SmelterEnchantment extends Enchantment { protected SmelterEnchantment(Rarity rarityIn, EnchantmentCategory typeIn, EquipmentSlot... slots) { super(rarityIn, typeIn, slots); @@ -281,15 +336,15 @@ public class GlobalLootModifiersTest extends BaseTestMod { } } - private static class DungeonLootEnhancerModifier extends LootModifier { - public static final Supplier> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst) + private static class MultiplyDropsModifier extends LootModifier { + public static final Supplier> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst) .and(ExtraCodecs.POSITIVE_INT.optionalFieldOf("multiplication_factor", 2).forGetter(m -> m.multiplicationFactor)) - .apply(inst, DungeonLootEnhancerModifier::new) + .apply(inst, MultiplyDropsModifier::new) )); private final int multiplicationFactor; - public DungeonLootEnhancerModifier(final LootItemCondition[] conditionsIn, final int multiplicationFactor) { + public MultiplyDropsModifier(final LootItemCondition[] conditionsIn, final int multiplicationFactor) { super(conditionsIn); this.multiplicationFactor = multiplicationFactor; }