From 915b6f9d819f37e08f0809ba04e17895628914b4 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 7 Jul 2023 10:18:16 +0100 Subject: [PATCH] Switch away from Forge's loot modifiers We switched to Forge's loot modifier system in the 1.20 update, as LootTable.addPool had been removed. Turns out this was by accident, and so we switch back to the previous implementation, as it's much simpler and efficient. --- gradle/libs.versions.toml | 2 +- .../computercraft/shared/CommonHooks.java | 16 ++++++- .../computercraft/shared/ComputerCraft.java | 9 +--- .../loot_modifiers/treasure_disk.json | 21 --------- .../loot_modifiers/global_loot_modifiers.json | 1 - .../dan200/computercraft/ComputerCraft.java | 2 - .../dan200/computercraft/data/Generators.java | 2 - .../data/LootModifierProvider.java | 32 ------------- .../shared/ForgeCommonHooks.java | 6 +++ .../shared/ForgeModRegistry.java | 31 ------------- .../shared/loot/InjectLootTableModifier.java | 45 ------------------- 11 files changed, 24 insertions(+), 143 deletions(-) delete mode 100644 projects/forge/src/generated/resources/data/computercraft/loot_modifiers/treasure_disk.json delete mode 100644 projects/forge/src/generated/resources/data/forge/loot_modifiers/global_loot_modifiers.json delete mode 100644 projects/forge/src/main/java/dan200/computercraft/data/LootModifierProvider.java delete mode 100644 projects/forge/src/main/java/dan200/computercraft/shared/ForgeModRegistry.java delete mode 100644 projects/forge/src/main/java/dan200/computercraft/shared/loot/InjectLootTableModifier.java diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f65c3cf62..1813fe7be 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,7 +9,7 @@ # Remember to update corresponding versions in fabric.mod.json/mods.toml fabric-api = "0.83.1+1.20.1" fabric-loader = "0.14.21" -forge = "47.0.1" +forge = "47.1.0" forgeSpi = "6.0.0" mixin = "0.8.5" parchment = "2023.03.12" diff --git a/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java b/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java index 8db24018e..392081fa4 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java @@ -23,7 +23,11 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.storage.loot.BuiltInLootTables; +import net.minecraft.world.level.storage.loot.LootPool; +import net.minecraft.world.level.storage.loot.entries.LootTableReference; +import net.minecraft.world.level.storage.loot.providers.number.ConstantValue; +import javax.annotation.Nullable; import java.util.Set; import java.util.function.BiConsumer; @@ -71,7 +75,7 @@ public final class CommonHooks { public static final ResourceLocation TREASURE_DISK_LOOT = new ResourceLocation(ComputerCraftAPI.MOD_ID, "treasure_disk"); - public static final Set TREASURE_DISK_LOOT_TABLES = Set.of( + private static final Set TREASURE_DISK_LOOT_TABLES = Set.of( BuiltInLootTables.SIMPLE_DUNGEON, BuiltInLootTables.ABANDONED_MINESHAFT, BuiltInLootTables.STRONGHOLD_CORRIDOR, @@ -84,6 +88,16 @@ public final class CommonHooks { BuiltInLootTables.VILLAGE_CARTOGRAPHER ); + public static @Nullable LootPool.Builder getExtraLootPool(ResourceLocation lootTable) { + if (!lootTable.getNamespace().equals("minecraft") || !TREASURE_DISK_LOOT_TABLES.contains(lootTable)) { + return null; + } + + return LootPool.lootPool() + .add(LootTableReference.lootTableReference(TREASURE_DISK_LOOT)) + .setRolls(ConstantValue.exactly(1)); + } + public static void onDatapackReload(BiConsumer addReload) { addReload.accept("mounts", ResourceMount.RELOAD_LISTENER); addReload.accept("turtle_upgrades", TurtleUpgrades.instance()); diff --git a/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java b/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java index 5a35a33ea..d4fb64027 100644 --- a/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java +++ b/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java @@ -37,9 +37,6 @@ import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.storage.LevelResource; -import net.minecraft.world.level.storage.loot.LootPool; -import net.minecraft.world.level.storage.loot.entries.LootTableReference; -import net.minecraft.world.level.storage.loot.providers.number.ConstantValue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; @@ -95,10 +92,8 @@ public class ComputerCraft { UseBlockCallback.EVENT.register(FabricCommonHooks::useOnBlock); LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> { - if (!id.getNamespace().equals("minecraft") || !CommonHooks.TREASURE_DISK_LOOT_TABLES.contains(id)) return; - tableBuilder.withPool(LootPool.lootPool() - .add(LootTableReference.lootTableReference(CommonHooks.TREASURE_DISK_LOOT)) - .setRolls(ConstantValue.exactly(1))); + var pool = CommonHooks.getExtraLootPool(id); + if (pool != null) tableBuilder.withPool(pool); }); CommonHooks.onDatapackReload((name, listener) -> ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(new ReloadListener(name, listener))); diff --git a/projects/forge/src/generated/resources/data/computercraft/loot_modifiers/treasure_disk.json b/projects/forge/src/generated/resources/data/computercraft/loot_modifiers/treasure_disk.json deleted file mode 100644 index 47d15389d..000000000 --- a/projects/forge/src/generated/resources/data/computercraft/loot_modifiers/treasure_disk.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "type": "computercraft:inject_loot_table", - "conditions": [ - { - "condition": "minecraft:any_of", - "terms": [ - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/stronghold_library"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/woodland_mansion"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/stronghold_corridor"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/abandoned_mineshaft"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/simple_dungeon"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/village/village_cartographer"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/igloo_chest"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/jungle_temple"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/desert_pyramid"}, - {"condition": "forge:loot_table_id", "loot_table_id": "minecraft:chests/stronghold_crossing"} - ] - } - ], - "loot_table": "computercraft:treasure_disk" -} diff --git a/projects/forge/src/generated/resources/data/forge/loot_modifiers/global_loot_modifiers.json b/projects/forge/src/generated/resources/data/forge/loot_modifiers/global_loot_modifiers.json deleted file mode 100644 index e94aa6173..000000000 --- a/projects/forge/src/generated/resources/data/forge/loot_modifiers/global_loot_modifiers.json +++ /dev/null @@ -1 +0,0 @@ -{"entries": ["computercraft:treasure_disk"], "replace": false} diff --git a/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java b/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java index 735b68c35..3533d29e0 100644 --- a/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java @@ -12,7 +12,6 @@ import dan200.computercraft.api.network.wired.WiredElement; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.pocket.PocketUpgradeSerialiser; import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; -import dan200.computercraft.shared.ForgeModRegistry; import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.config.ConfigSpec; import dan200.computercraft.shared.details.FluidData; @@ -37,7 +36,6 @@ import net.minecraftforge.registries.RegistryBuilder; public final class ComputerCraft { public ComputerCraft() { ModRegistry.register(); - ForgeModRegistry.register(); ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ((ForgeConfigFile) ConfigSpec.serverSpec).spec()); ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ((ForgeConfigFile) ConfigSpec.clientSpec).spec()); diff --git a/projects/forge/src/main/java/dan200/computercraft/data/Generators.java b/projects/forge/src/main/java/dan200/computercraft/data/Generators.java index 9e0f96149..2ddaf757d 100644 --- a/projects/forge/src/main/java/dan200/computercraft/data/Generators.java +++ b/projects/forge/src/main/java/dan200/computercraft/data/Generators.java @@ -42,8 +42,6 @@ public class Generators { public static void gather(GatherDataEvent event) { var generator = event.getGenerator(); DataProviders.add(new GeneratorFactoryImpl(generator.getVanillaPack(true), event.getExistingFileHelper(), event.getLookupProvider())); - - generator.addProvider(event.includeServer(), LootModifierProvider::new); } private record GeneratorFactoryImpl( diff --git a/projects/forge/src/main/java/dan200/computercraft/data/LootModifierProvider.java b/projects/forge/src/main/java/dan200/computercraft/data/LootModifierProvider.java deleted file mode 100644 index 25d6f6f68..000000000 --- a/projects/forge/src/main/java/dan200/computercraft/data/LootModifierProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers -// -// SPDX-License-Identifier: MPL-2.0 - -package dan200.computercraft.data; - -import dan200.computercraft.api.ComputerCraftAPI; -import dan200.computercraft.shared.CommonHooks; -import dan200.computercraft.shared.loot.InjectLootTableModifier; -import net.minecraft.data.PackOutput; -import net.minecraft.world.level.storage.loot.predicates.AnyOfCondition; -import net.minecraft.world.level.storage.loot.predicates.LootItemCondition; -import net.minecraftforge.common.data.GlobalLootModifierProvider; -import net.minecraftforge.common.loot.LootTableIdCondition; - -final class LootModifierProvider extends GlobalLootModifierProvider { - LootModifierProvider(PackOutput output) { - super(output, ComputerCraftAPI.MOD_ID); - } - - @Override - protected void start() { - add("treasure_disk", new InjectLootTableModifier( - new LootItemCondition[]{ - AnyOfCondition.anyOf( - CommonHooks.TREASURE_DISK_LOOT_TABLES.stream().map(LootTableIdCondition::builder).toArray(LootItemCondition.Builder[]::new) - ).build(), - }, - CommonHooks.TREASURE_DISK_LOOT - )); - } -} diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java b/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java index dc8c01740..d5f1a4ed5 100644 --- a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java +++ b/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java @@ -138,6 +138,12 @@ public class ForgeCommonHooks { } } + @SubscribeEvent + public static void lootLoad(LootTableLoadEvent event) { + var pool = CommonHooks.getExtraLootPool(event.getName()); + if (pool != null) event.getTable().addPool(pool.build()); + } + @SubscribeEvent(priority = EventPriority.HIGHEST) public static void onEntitySpawn(EntityJoinLevelEvent event) { if (CommonHooks.onEntitySpawn(event.getEntity())) event.setCanceled(true); diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeModRegistry.java b/projects/forge/src/main/java/dan200/computercraft/shared/ForgeModRegistry.java deleted file mode 100644 index fcbff73b2..000000000 --- a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeModRegistry.java +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers -// -// SPDX-License-Identifier: MPL-2.0 - -package dan200.computercraft.shared; - -import com.mojang.serialization.Codec; -import dan200.computercraft.shared.loot.InjectLootTableModifier; -import dan200.computercraft.shared.platform.PlatformHelper; -import dan200.computercraft.shared.platform.RegistrationHelper; -import dan200.computercraft.shared.platform.RegistryEntry; -import net.minecraftforge.common.loot.IGlobalLootModifier; -import net.minecraftforge.registries.ForgeRegistries; - -/** - * {@link ModRegistry} equivalent for Forge-specific content. - */ -public final class ForgeModRegistry { - private ForgeModRegistry() { - } - - public static final class Codecs { - static final RegistrationHelper> REGISTRY = PlatformHelper.get().createRegistrationHelper(ForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS); - - public static final RegistryEntry> INJECT_LOOT_TABLE = REGISTRY.register("inject_loot_table", InjectLootTableModifier::createCodec); - } - - public static void register() { - Codecs.REGISTRY.register(); - } -} diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/loot/InjectLootTableModifier.java b/projects/forge/src/main/java/dan200/computercraft/shared/loot/InjectLootTableModifier.java deleted file mode 100644 index 0bf46f8c6..000000000 --- a/projects/forge/src/main/java/dan200/computercraft/shared/loot/InjectLootTableModifier.java +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers -// -// SPDX-License-Identifier: MPL-2.0 - -package dan200.computercraft.shared.loot; - -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; -import dan200.computercraft.shared.ForgeModRegistry; -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.storage.loot.LootContext; -import net.minecraft.world.level.storage.loot.predicates.LootItemCondition; -import net.minecraftforge.common.loot.LootModifier; - -/** - * A {@link LootModifier} which adds a new loot pool to a piece of loot. - */ -public final class InjectLootTableModifier extends LootModifier { - private final ResourceLocation location; - - public InjectLootTableModifier(LootItemCondition[] conditionsIn, ResourceLocation location) { - super(conditionsIn); - this.location = location; - } - - public static Codec createCodec() { - return RecordCodecBuilder.create(inst -> LootModifier.codecStart(inst).and( - ResourceLocation.CODEC.fieldOf("loot_table").forGetter(m -> m.location) - ).apply(inst, InjectLootTableModifier::new) - ); - } - - @Override - protected ObjectArrayList doApply(ObjectArrayList generatedLoot, LootContext context) { - context.getResolver().getLootTable(location).getRandomItemsRaw(context, generatedLoot::add); - return generatedLoot; - } - - @Override - public Codec codec() { - return ForgeModRegistry.Codecs.INJECT_LOOT_TABLE.get(); - } -}