From 1015dd7b3bcaf568d689bdfb7f5cc87c818c183a Mon Sep 17 00:00:00 2001 From: Pixirora Date: Wed, 25 Oct 2023 20:38:51 -0400 Subject: [PATCH] Add support for default generator access in config --- .../janerator/config/ConfigFileManager.java | 8 +-- .../janerator/config/DefaultGenerators.java | 26 +++++--- .../worldgen/generator/MultiGenOrganizer.java | 37 ++++++----- .../generator/PreparedGenerators.java | 64 +++++++++++++++++++ 4 files changed, 100 insertions(+), 35 deletions(-) create mode 100644 src/main/java/net/pixaurora/janerator/worldgen/generator/PreparedGenerators.java diff --git a/src/main/java/net/pixaurora/janerator/config/ConfigFileManager.java b/src/main/java/net/pixaurora/janerator/config/ConfigFileManager.java index bb474f3..0d0c5cd 100644 --- a/src/main/java/net/pixaurora/janerator/config/ConfigFileManager.java +++ b/src/main/java/net/pixaurora/janerator/config/ConfigFileManager.java @@ -4,7 +4,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.Map; import java.util.Optional; import org.quiltmc.loader.api.QuiltLoader; @@ -97,11 +96,8 @@ private JaneratorConfig createDefault() { new GraphProperties( Level.OVERWORLD, new MultiGenOrganizer( - DefaultGenerators.createUnshadedOverworldGenerator(), - Map.of( - "grassy_mushroom", DefaultGenerators.createShadedOverworldGenerator(), - "rainbow_outline", DefaultGenerators.createOutlineOverworldGenerator() - ), + DefaultGenerators.getOverworldGenerators(), + "default_overworld", List.of( new JaneratorLayer( new FunctionGrapher( diff --git a/src/main/java/net/pixaurora/janerator/config/DefaultGenerators.java b/src/main/java/net/pixaurora/janerator/config/DefaultGenerators.java index 2a01fc0..9a50a96 100644 --- a/src/main/java/net/pixaurora/janerator/config/DefaultGenerators.java +++ b/src/main/java/net/pixaurora/janerator/config/DefaultGenerators.java @@ -3,12 +3,14 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; import net.minecraft.core.Holder; import net.minecraft.core.HolderSet; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.biome.Biomes; import net.minecraft.world.level.biome.FixedBiomeSource; @@ -22,6 +24,7 @@ import net.minecraft.world.level.levelgen.structure.StructureSet; import net.pixaurora.janerator.RegistryCache; import net.pixaurora.janerator.graphing.GraphFunctionDefinition; +import net.pixaurora.janerator.worldgen.generator.PreparedGenerators; import net.pixaurora.janerator.worldgen.generator.SlantedFlatGenerator; import net.pixaurora.janerator.worldgen.settings.SlantedFlatGeneratorSettings; @@ -38,16 +41,6 @@ public static ChunkGenerator createShadedOverworldGenerator() { ); } - public static ChunkGenerator createUnshadedOverworldGenerator() { - return createGenerator( - Biomes.MUSHROOM_FIELDS, - new FlatLayerInfo(1, Blocks.WHITE_CONCRETE), - new FlatLayerInfo(125, Blocks.GRAY_CONCRETE), - - new FlatLayerInfo(1, Blocks.BEDROCK) - ); - } - public static ChunkGenerator createOutlineOverworldGenerator() { return new SlantedFlatGenerator( new FixedBiomeSource(getBiome(Biomes.MUSHROOM_FIELDS)), @@ -106,4 +99,17 @@ private static ChunkGenerator createGenerator(ResourceKey biomeKey, FlatL .withBiomeAndLayers(layers, optional, biome) ); } + + public static PreparedGenerators getOverworldGenerators() { + return new PreparedGenerators( + Map.of( + "grassy_mushroom", createShadedOverworldGenerator(), + "rainbow_outline", createOutlineOverworldGenerator() + ), + Map.of( + "default_overworld", Level.OVERWORLD + ), + RegistryCache.INSTANCE.getProvider(Registries.LEVEL_STEM) + ); + } } diff --git a/src/main/java/net/pixaurora/janerator/worldgen/generator/MultiGenOrganizer.java b/src/main/java/net/pixaurora/janerator/worldgen/generator/MultiGenOrganizer.java index 4507232..92ec35b 100644 --- a/src/main/java/net/pixaurora/janerator/worldgen/generator/MultiGenOrganizer.java +++ b/src/main/java/net/pixaurora/janerator/worldgen/generator/MultiGenOrganizer.java @@ -29,23 +29,23 @@ public class MultiGenOrganizer { public static final Codec CODEC = RecordCodecBuilder.create( instance -> instance.group( - ChunkGenerator.CODEC.fieldOf("default_generator").forGetter(MultiGenOrganizer::getDefaultGenerator), - Codec.unboundedMap(Codec.STRING, ChunkGenerator.CODEC).fieldOf("other_generators").forGetter(MultiGenOrganizer::getOtherGenerators), + PreparedGenerators.CODEC.fieldOf("generators").forGetter(MultiGenOrganizer::getGenerators), + Codec.STRING.fieldOf("default_generator_key").forGetter(MultiGenOrganizer::getDefaultGeneratorKey), JaneratorLayer.CODEC.listOf().fieldOf("layers").forGetter(MultiGenOrganizer::getLayers) ).apply(instance, MultiGenOrganizer::new) ); - private final ChunkGenerator defaultGenerator; - private final Map otherGenerators; + private final PreparedGenerators generators; + private final String defaultGeneratorKey; private final List layers; private int generatorCount; private LoadingCache selectionCache; - public MultiGenOrganizer(ChunkGenerator defaultGenerator, Map keyedGenerators, List layers) { - this.defaultGenerator = defaultGenerator; - this.otherGenerators = keyedGenerators; + public MultiGenOrganizer(PreparedGenerators generators, String defaultGeneratorKey, List layers) { + this.generators = generators; + this.defaultGeneratorKey = defaultGeneratorKey; this.layers = layers; @@ -60,12 +60,16 @@ public MultiGenOrganizer(ChunkGenerator defaultGenerator, Map getOtherGenerators() { - return this.otherGenerators; + public PreparedGenerators getGenerators() { + return this.generators; } public List getLayers() { @@ -80,7 +84,7 @@ private void validateGeneratorKeys() { List missingKeys = layer.involvedGeneratorKeys() .stream() - .filter(key -> this.generatorByKey(key) == null) + .filter(key -> this.generators.get(key) == null) .distinct() .toList(); @@ -106,16 +110,11 @@ private void validateGeneratorKeys() { public List involvedGenerators() { List involvedGenerators = new ArrayList<>(); - involvedGenerators.add(this.defaultGenerator); - involvedGenerators.addAll(this.otherGenerators.values()); + involvedGenerators.addAll(this.generators.getAll()); return involvedGenerators; } - public ChunkGenerator generatorByKey(String generatorKey) { - return generatorKey == "default" ? defaultGenerator : otherGenerators.get(generatorKey); - } - private ChunkGenerator sampleOne(List regularShading, int sectionX, int sectionZ) { Object2IntMap areaSample = new Object2IntOpenHashMap<>(this.generatorCount); @@ -154,12 +153,12 @@ public List sampleForBiomes(List regularShading) } private FullGeneratorLookup createLookup(ChunkPos chunk) { - List generatorShading = new ArrayList<>(Collections.nCopies(256, this.defaultGenerator)); + List generatorShading = new ArrayList<>(Collections.nCopies(256, this.getDefaultGenerator())); List layerShading = new ArrayList<>(Collections.nCopies(256, JaneratorLayerData.DEFAULT)); for (JaneratorLayer layer : this.layers) { for (ShadeData shade : layer.shadesIn(chunk)) { - generatorShading.set(shade.index(), this.generatorByKey(shade.generatorKey())); + generatorShading.set(shade.index(), this.generators.get(shade.generatorKey())); layerShading.set(shade.index(), layer); } } diff --git a/src/main/java/net/pixaurora/janerator/worldgen/generator/PreparedGenerators.java b/src/main/java/net/pixaurora/janerator/worldgen/generator/PreparedGenerators.java new file mode 100644 index 0000000..d3a58d7 --- /dev/null +++ b/src/main/java/net/pixaurora/janerator/worldgen/generator/PreparedGenerators.java @@ -0,0 +1,64 @@ +package net.pixaurora.janerator.worldgen.generator; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.minecraft.core.HolderGetter; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.RegistryOps; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.chunk.ChunkGenerator; +import net.minecraft.world.level.dimension.LevelStem; + +public class PreparedGenerators { + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + Codec.unboundedMap(Codec.STRING, ChunkGenerator.CODEC).fieldOf("defined").forGetter(PreparedGenerators::userGenerators), + Codec.unboundedMap(Codec.STRING, ResourceKey.codec(Registries.DIMENSION)).fieldOf("default_generator_dimensions").forGetter(PreparedGenerators::defaultGeneratorDimensions), + RegistryOps.retrieveGetter(Registries.LEVEL_STEM) + ).apply(instance, PreparedGenerators::new) + ); + + public final Map userGenerators; + public final Map> defaultGenerators; + + public final Map allGenerators; + + public PreparedGenerators(Map userGenerators, + Map> defaultGeneratorDimensions, + HolderGetter dimensionGeneratorRegistry) { + this.userGenerators = userGenerators; + this.defaultGenerators = defaultGeneratorDimensions; + + this.allGenerators = new HashMap<>(); + + this.allGenerators.putAll(userGenerators); + for (Map.Entry> specifiedDefault : defaultGeneratorDimensions.entrySet()) { + ResourceKey generatorLocation = Registries.levelToLevelStem(specifiedDefault.getValue()); + LevelStem defaultGenerator = dimensionGeneratorRegistry.get(generatorLocation).get().value(); + + this.allGenerators.put(specifiedDefault.getKey(), defaultGenerator.generator()); + } + } + + public Map userGenerators() { + return this.userGenerators; + } + + public Map> defaultGeneratorDimensions() { + return this.defaultGenerators; + } + + public ChunkGenerator get(String name) { + return this.allGenerators.get(name); + } + + public Collection getAll() { + return this.allGenerators.values(); + } +}