From 9e4931e1afed642eea6a2bb6564eebaffd985f54 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 27 May 2022 01:06:09 +0200 Subject: [PATCH] Make sure all dimensions get checked for new surface rules --- .../betterx/bclib/api/biomes/BiomeAPI.java | 25 +++++------- .../bclib/api/worldgen/BCLChunkGenerator.java | 39 ++++++++++++------- .../mixin/common/PrimaryLevelDataMixin.java | 3 ++ .../worldgen/BCLWorldPresetSettings.java | 6 ++- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/betterx/bclib/api/biomes/BiomeAPI.java b/src/main/java/org/betterx/bclib/api/biomes/BiomeAPI.java index 6c37cc61..5ba5e7bc 100644 --- a/src/main/java/org/betterx/bclib/api/biomes/BiomeAPI.java +++ b/src/main/java/org/betterx/bclib/api/biomes/BiomeAPI.java @@ -26,8 +26,7 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.chunk.ChunkGenerator; import net.minecraft.world.level.chunk.PalettedContainer; -import net.minecraft.world.level.dimension.BuiltinDimensionTypes; -import net.minecraft.world.level.dimension.DimensionType; +import net.minecraft.world.level.dimension.LevelStem; import net.minecraft.world.level.levelgen.GenerationStep.Decoration; import net.minecraft.world.level.levelgen.NoiseGeneratorSettings; import net.minecraft.world.level.levelgen.feature.ConfiguredFeature; @@ -53,7 +52,6 @@ import org.betterx.bclib.interfaces.BiomeSourceAccessor; import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider; import org.betterx.bclib.interfaces.SurfaceMaterialProvider; import org.betterx.bclib.interfaces.SurfaceRuleProvider; -import org.betterx.bclib.mixin.client.MinecraftMixin; import org.betterx.bclib.mixin.common.BiomeGenerationSettingsAccessor; import org.betterx.bclib.mixin.common.MobSpawnSettingsAccessor; import org.betterx.bclib.util.CollectionsUtil; @@ -125,9 +123,8 @@ public class BiomeAPI { private static final Map, Integer> FEATURE_ORDER = Maps.newHashMap(); private static final MutableInt FEATURE_ORDER_ID = new MutableInt(0); - private static final Map, List>>> MODIFICATIONS = Maps.newHashMap(); + private static final Map, List>>> MODIFICATIONS = Maps.newHashMap(); private static final Map>>> TAG_ADDERS = Maps.newHashMap(); - private static final Set MODIFIED_SURFACE_PROVIDERS = new HashSet<>(8); public static final BCLBiome NETHER_WASTES_BIOME = registerNetherBiome(getFromRegistry(Biomes.NETHER_WASTES).value()); public static final BCLBiome CRIMSON_FOREST_BIOME = registerNetherBiome(getFromRegistry(Biomes.CRIMSON_FOREST).value()); @@ -183,12 +180,10 @@ public class BiomeAPI { /** * For internal use only. *

- * This method gets called before a world is loaded/created to flush cashes we build. The Method is - * called from {@link MinecraftMixin} + * This method gets called before a world is loaded/created to flush cashes we build. */ public static void prepareNewLevel() { - MODIFIED_SURFACE_PROVIDERS.forEach(p -> p.bclib_clearBiomeSources()); - MODIFIED_SURFACE_PROVIDERS.clear(); + } /** @@ -559,7 +554,7 @@ public class BiomeAPI { * @param dimensionID {@link ResourceLocation} dimension ID, example: Level.OVERWORLD or "minecraft:overworld". * @param modification {@link BiConsumer} with {@link ResourceKey} biome ID and {@link Biome} parameters. */ - public static void registerBiomeModification(ResourceKey dimensionID, + public static void registerBiomeModification(ResourceKey dimensionID, BiConsumer> modification) { List>> modifications = MODIFICATIONS.computeIfAbsent(dimensionID, k -> Lists.newArrayList()); @@ -572,7 +567,7 @@ public class BiomeAPI { * @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters. */ public static void registerOverworldBiomeModification(BiConsumer> modification) { - registerBiomeModification(BuiltinDimensionTypes.OVERWORLD, modification); + registerBiomeModification(LevelStem.OVERWORLD, modification); } /** @@ -581,7 +576,7 @@ public class BiomeAPI { * @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters. */ public static void registerNetherBiomeModification(BiConsumer> modification) { - registerBiomeModification(BuiltinDimensionTypes.NETHER, modification); + registerBiomeModification(LevelStem.NETHER, modification); } /** @@ -590,7 +585,7 @@ public class BiomeAPI { * @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters. */ public static void registerEndBiomeModification(BiConsumer> modification) { - registerBiomeModification(BuiltinDimensionTypes.END, modification); + registerBiomeModification(LevelStem.END, modification); } /** @@ -695,7 +690,7 @@ public class BiomeAPI { // Provided by all the BiomeSources that use the same generator. // This happens for example when using the MiningDimensions, which reuses the generator for the // Nethering Dimension - MODIFIED_SURFACE_PROVIDERS.add(provider); + //MODIFIED_SURFACE_PROVIDERS.add(provider); provider.bclib_addBiomeSource(source); } else { BCLib.LOGGER.warning("No generator for " + source); @@ -704,7 +699,7 @@ public class BiomeAPI { ((BiomeSourceAccessor) source).bclRebuildFeatures(); } - public static void applyModifications(BiomeSource source, ResourceKey dimension) { + public static void applyModifications(BiomeSource source, ResourceKey dimension) { BCLib.LOGGER.info("Apply Modifications for " + dimension.location() + " BiomeSource " + source); final Set> biomes = source.possibleBiomes(); List>> modifications = MODIFICATIONS.get(dimension); diff --git a/src/main/java/org/betterx/bclib/api/worldgen/BCLChunkGenerator.java b/src/main/java/org/betterx/bclib/api/worldgen/BCLChunkGenerator.java index cadba8c8..9654a4e2 100644 --- a/src/main/java/org/betterx/bclib/api/worldgen/BCLChunkGenerator.java +++ b/src/main/java/org/betterx/bclib/api/worldgen/BCLChunkGenerator.java @@ -6,8 +6,6 @@ import net.minecraft.resources.RegistryOps; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.biome.BiomeSource; import net.minecraft.world.level.chunk.ChunkGenerator; -import net.minecraft.world.level.dimension.BuiltinDimensionTypes; -import net.minecraft.world.level.dimension.DimensionType; import net.minecraft.world.level.dimension.LevelStem; import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator; import net.minecraft.world.level.levelgen.NoiseGeneratorSettings; @@ -22,7 +20,9 @@ import org.betterx.bclib.api.biomes.BiomeAPI; import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider; import org.betterx.bclib.interfaces.SurfaceRuleProvider; +import java.util.List; import java.util.Optional; +import java.util.function.Predicate; public class BCLChunkGenerator extends NoiseBasedChunkGenerator { @@ -59,23 +59,14 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator { } public static void injectNoiseSettings(WorldGenSettings settings) { - injectNoiseSettings(LevelStem.NETHER, BuiltinDimensionTypes.NETHER, settings); - injectNoiseSettings(LevelStem.END, BuiltinDimensionTypes.END, settings); + BCLChunkGenerator.injectNoiseSettings(settings, BCLChunkGenerator.ALL_DIMENSIONS); } public static void injectNoiseSettings(ResourceKey dimensionKey, - ResourceKey dimensionTypeKey, - WorldGenSettings settings) { - Optional> loadedStem = settings.dimensions().getHolder(dimensionKey); - final ChunkGenerator loadedChunkGenerator = loadedStem.map(h -> h.value().generator()).orElse(null); - injectNoiseSettings(dimensionTypeKey, loadedChunkGenerator); - } - - public static void injectNoiseSettings(ResourceKey dimensionTypeKey, ChunkGenerator loadedChunkGenerator) { - BCLib.LOGGER.debug("Correcting Noise Settings for " + dimensionTypeKey.location().toString()); + BCLib.LOGGER.debug("Correcting Noise Settings for " + dimensionKey.location().toString()); final BiomeSource loadedBiomeSource = loadedChunkGenerator.getBiomeSource(); - BiomeAPI.applyModifications(loadedBiomeSource, dimensionTypeKey); + BiomeAPI.applyModifications(loadedBiomeSource, dimensionKey); if (loadedChunkGenerator instanceof NoiseBasedChunkGenerator nbc) { if (((Object) nbc.generatorSettings().value()) instanceof SurfaceRuleProvider srp) { @@ -87,6 +78,26 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator { } } + public static final Predicate> NON_MANAGED_DIMENSIONS = dim -> dim != LevelStem.NETHER && dim != LevelStem.END; + public static final Predicate> ALL_DIMENSIONS = dim -> true; + + public static void injectNoiseSettings(WorldGenSettings settings, Predicate> filter) { + List> otherDimensions = settings + .dimensions() + .entrySet() + .stream() + .map(e -> e.getKey()) + .filter(filter) + .toList(); + + for (ResourceKey key : otherDimensions) { + Optional> stem = settings.dimensions().getHolder(key); + if (stem.isPresent()) { + injectNoiseSettings(key, stem.get().value().generator()); + } + } + } + @Override protected Codec codec() { return CODEC; diff --git a/src/main/java/org/betterx/bclib/mixin/common/PrimaryLevelDataMixin.java b/src/main/java/org/betterx/bclib/mixin/common/PrimaryLevelDataMixin.java index 2efeeb6f..612813a5 100644 --- a/src/main/java/org/betterx/bclib/mixin/common/PrimaryLevelDataMixin.java +++ b/src/main/java/org/betterx/bclib/mixin/common/PrimaryLevelDataMixin.java @@ -32,6 +32,7 @@ public class PrimaryLevelDataMixin { private static final ThreadLocal>> bcl_lastRegistryAccess = ThreadLocal.withInitial( () -> Optional.empty()); + //This is the way a created (new) world is initializing the PrimaryLevelData @ModifyArg(method = "(Lnet/minecraft/world/level/LevelSettings;Lnet/minecraft/world/level/levelgen/WorldGenSettings;Lcom/mojang/serialization/Lifecycle;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/PrimaryLevelData;(Lcom/mojang/datafixers/DataFixer;ILnet/minecraft/nbt/CompoundTag;ZIIIFJJIIIZIZZZLnet/minecraft/world/level/border/WorldBorder$Settings;IILjava/util/UUID;Ljava/util/Set;Lnet/minecraft/world/level/timers/TimerQueue;Lnet/minecraft/nbt/CompoundTag;Lnet/minecraft/nbt/CompoundTag;Lnet/minecraft/world/level/LevelSettings;Lnet/minecraft/world/level/levelgen/WorldGenSettings;Lcom/mojang/serialization/Lifecycle;)V")) private static WorldGenSettings bcl_fixOtherSettings(WorldGenSettings worldGenSettings) { BCLChunkGenerator.injectNoiseSettings(worldGenSettings); @@ -53,6 +54,8 @@ public class PrimaryLevelDataMixin { } } + + //This is the way an loaded (existing) world is initializing the PrimaryLevelData @ModifyArg(method = "parse", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/PrimaryLevelData;(Lcom/mojang/datafixers/DataFixer;ILnet/minecraft/nbt/CompoundTag;ZIIIFJJIIIZIZZZLnet/minecraft/world/level/border/WorldBorder$Settings;IILjava/util/UUID;Ljava/util/Set;Lnet/minecraft/world/level/timers/TimerQueue;Lnet/minecraft/nbt/CompoundTag;Lnet/minecraft/nbt/CompoundTag;Lnet/minecraft/world/level/LevelSettings;Lnet/minecraft/world/level/levelgen/WorldGenSettings;Lcom/mojang/serialization/Lifecycle;)V")) private static WorldGenSettings bcl_fixSettings(WorldGenSettings settings) { Optional> registryOps = bcl_lastRegistryAccess.get(); diff --git a/src/main/java/org/betterx/bclib/presets/worldgen/BCLWorldPresetSettings.java b/src/main/java/org/betterx/bclib/presets/worldgen/BCLWorldPresetSettings.java index a9510889..6a17b01c 100644 --- a/src/main/java/org/betterx/bclib/presets/worldgen/BCLWorldPresetSettings.java +++ b/src/main/java/org/betterx/bclib/presets/worldgen/BCLWorldPresetSettings.java @@ -170,7 +170,7 @@ public class BCLWorldPresetSettings extends WorldPresetSettings { if (loadedChunkGenerator instanceof NoiseGeneratorSettingsProvider noiseProvider) { final Set> biomes = loadedChunkGenerator.getBiomeSource().possibleBiomes(); final BiomeSource bs = fixBiomeSource(referenceGenerator.getBiomeSource(), biomes); - BiomeAPI.applyModifications(bs, dimensionTypeKey); + BiomeAPI.applyModifications(bs, dimensionKey); referenceGenerator = new BCLChunkGenerator(generator.bclib_getStructureSetsRegistry(), noiseProvider.bclib_getNoises(), bs, @@ -185,7 +185,7 @@ public class BCLWorldPresetSettings extends WorldPresetSettings { settings, referenceGenerator); } else { - BCLChunkGenerator.injectNoiseSettings(dimensionTypeKey, loadedChunkGenerator); + BCLChunkGenerator.injectNoiseSettings(dimensionKey, loadedChunkGenerator); } return settings; } @@ -193,6 +193,8 @@ public class BCLWorldPresetSettings extends WorldPresetSettings { public WorldGenSettings repairSettingsOnLoad(RegistryAccess registryAccess, WorldGenSettings settings) { settings = fixSettingsInCurrentWorld(registryAccess, LevelStem.NETHER, BuiltinDimensionTypes.NETHER, settings); settings = fixSettingsInCurrentWorld(registryAccess, LevelStem.END, BuiltinDimensionTypes.END, settings); + BCLChunkGenerator.injectNoiseSettings(settings, BCLChunkGenerator.NON_MANAGED_DIMENSIONS); return settings; } + }