From 048a5265773f1b3616585eb5bd9ef4a860ca5722 Mon Sep 17 00:00:00 2001 From: Draylar <17728338+Draylar@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:09:46 -0500 Subject: [PATCH] Update ore registry to rely on registry events for load order --- .../java/draylar/horizon/MinersHorizon.java | 4 +- .../horizon/registry/HorizonWorld.java | 53 +++++++++++++------ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/main/java/draylar/horizon/MinersHorizon.java b/src/main/java/draylar/horizon/MinersHorizon.java index d0fc469..c5364f9 100644 --- a/src/main/java/draylar/horizon/MinersHorizon.java +++ b/src/main/java/draylar/horizon/MinersHorizon.java @@ -28,8 +28,8 @@ public void onInitialize() { HorizonBlocks.init(); HorizonWorld.init(); - // Register ores from config & KubeJS - HorizonWorld.loadOres(); + // Register ores from config + HorizonWorld.registerOreHandlers(); // Load biomes after ores are loaded HorizonBiomes.init(); diff --git a/src/main/java/draylar/horizon/registry/HorizonWorld.java b/src/main/java/draylar/horizon/registry/HorizonWorld.java index 05fe1c5..e724bda 100644 --- a/src/main/java/draylar/horizon/registry/HorizonWorld.java +++ b/src/main/java/draylar/horizon/registry/HorizonWorld.java @@ -5,6 +5,8 @@ import draylar.horizon.world.MinersHorizonChunkGenerator; import draylar.horizon.world.MiningCaveCarver; import draylar.horizon.world.RockySurfaceBuilder; +import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; +import net.minecraft.block.Block; import net.minecraft.block.Blocks; import net.minecraft.util.Identifier; import net.minecraft.util.math.floatprovider.ConstantFloatProvider; @@ -77,23 +79,42 @@ public static void init() { Registry.register(Registry.CHUNK_GENERATOR, MinersHorizon.id("horizon"), MinersHorizonChunkGenerator.CODEC); } - public static void loadOres() { + public static void registerOreHandlers() { List ores = new ArrayList<>(Arrays.asList(MinersHorizon.CONFIG.oreConfigList)); - // Register each ore - for (OreConfig ore : ores) { - ConfiguredFeature feature = register( - String.format("%s_%d_%d_%d", new Identifier(ore.block).getPath(), ore.size, ore.count, ore.maxY), - Feature.ORE.configure(new OreFeatureConfig( - OreFeatureConfig.Rules.BASE_STONE_OVERWORLD, - Registry.BLOCK.get(new Identifier(ore.block)).getDefaultState(), - ore.size) - ) - .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(UniformHeightProvider.create(YOffset.fixed(ore.minY), YOffset.fixed(ore.maxY))))) - .spreadHorizontally() - .repeat(ore.count)); - - ORES.add(feature); - } + // Handle ores that have already been registered. + Registry.BLOCK.getEntries().forEach(entry -> { + RegistryKey key = entry.getKey(); + String id = key.getValue().toString(); + + // Load every Ore Config with a matching ID. + ores.stream().filter(config -> config.block.equals(id)).forEach(config -> { + ORES.add(load(config, entry.getValue())); + }); + }); + + // TODO: there is a chance this slows down game loading. The question is how much? + // 100 entries * 1,000 registry events = 100,000 filter checks. + // Register a callback to add in future ores. + RegistryEntryAddedCallback.event(Registry.BLOCK).register((rawId, id, object) -> { + + // Load every Ore Config with a matching ID. + ores.stream().filter(config -> config.block.equals(id.toString())).forEach(config -> { + ORES.add(load(config, object)); + }); + }); + } + + private static ConfiguredFeature load(OreConfig config, Block block) { + return register( + String.format("%s_%d_%d_%d", new Identifier(config.block).getPath(), config.size, config.count, config.maxY), + Feature.ORE.configure(new OreFeatureConfig( + OreFeatureConfig.Rules.BASE_STONE_OVERWORLD, + block.getDefaultState(), + config.size) + ) + .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(UniformHeightProvider.create(YOffset.fixed(config.minY), YOffset.fixed(config.maxY))))) + .spreadHorizontally() + .repeat(config.count)); } }