Functional WorldSetupScreen

This commit is contained in:
Frank 2022-05-22 22:44:09 +02:00
parent faa20c608e
commit dd0c259b56
9 changed files with 197 additions and 112 deletions

View file

@ -26,11 +26,6 @@ import org.betterx.bclib.world.generator.BCLBiomeSource;
import java.util.Optional;
public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
public static int BIOME_SOURCE_VERSION_NONE = -1;
public static int BIOME_SOURCE_VERSION_VANILLA = 0;
public static int BIOME_SOURCE_VERSION_SQUARE = 17;
public static int BIOME_SOURCE_VERSION_HEX = 18;
public static int DEFAULT_BIOME_SOURCE_VERSION = BIOME_SOURCE_VERSION_HEX;
private static String TAG_GENERATOR = "generator";
private static final String TAG_VERSION = "version";
@ -81,13 +76,8 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
}
public static int getBiomeVersionForGenerator(ChunkGenerator generator) {
if (generator == null) return BIOME_SOURCE_VERSION_NONE;
if (generator.getBiomeSource() instanceof BCLBiomeSource bcl) {
return bcl.biomeSourceVersion;
} else {
return BIOME_SOURCE_VERSION_VANILLA;
}
if (generator == null) return BCLBiomeSource.getVersionBiomeSource(null);
return BCLBiomeSource.getVersionBiomeSource(generator.getBiomeSource());
}
public static Optional<Holder<LevelStem>> referenceStemForVersion(
@ -99,13 +89,13 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
boolean generateBonusChest
) {
final WorldGenSettings referenceSettings;
if (biomeSourceVersion == BIOME_SOURCE_VERSION_VANILLA) {
if (biomeSourceVersion == BCLBiomeSource.BIOME_SOURCE_VERSION_VANILLA) {
referenceSettings = net.minecraft.world.level.levelgen.presets.WorldPresets.createNormalWorldFromPreset(
registryAccess,
seed,
generateStructures,
generateBonusChest);
} else if (biomeSourceVersion == BIOME_SOURCE_VERSION_SQUARE) {
} else if (biomeSourceVersion == BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE) {
referenceSettings = WorldPresets.createWorldFromPreset(
WorldPresets.BCL_WORLD_17,
registryAccess,
@ -124,7 +114,7 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
public static int getBiomeVersionForCurrentWorld(ResourceKey<LevelStem> key) {
final CompoundTag settingsNbt = getSettingsNbt();
if (!settingsNbt.contains(key.location().toString())) return DEFAULT_BIOME_SOURCE_VERSION;
if (!settingsNbt.contains(key.location().toString())) return BCLBiomeSource.DEFAULT_BIOME_SOURCE_VERSION;
return settingsNbt.getInt(key.location().toString());
}
@ -155,7 +145,7 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
if (settingsNbt.size() == 0) {
BCLib.LOGGER.info("Found World without generator Settings. Setting up data...");
int biomeSourceVersion = DEFAULT_BIOME_SOURCE_VERSION;
int biomeSourceVersion = BCLBiomeSource.DEFAULT_BIOME_SOURCE_VERSION;
final CompoundTag bclRoot = WorldDataAPI.getRootTag(BCLib.MOD_ID);
@ -167,15 +157,15 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
if (isPre18) {
BCLib.LOGGER.info("World was create pre 1.18!");
biomeSourceVersion = BIOME_SOURCE_VERSION_SQUARE;
biomeSourceVersion = BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE;
}
if (WorldDataAPI.hasMod("betternether")) {
BCLib.LOGGER.info("Found Data from BetterNether, using for migration.");
final CompoundTag bnRoot = WorldDataAPI.getRootTag("betternether");
biomeSourceVersion = "1.17".equals(bnRoot.getString(TAG_BN_GEN_VERSION))
? BIOME_SOURCE_VERSION_SQUARE
: BIOME_SOURCE_VERSION_HEX;
? BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE
: BCLBiomeSource.BIOME_SOURCE_VERSION_HEX;
}
BCLib.LOGGER.info("Set world to BiomeSource Version " + biomeSourceVersion);

View file

@ -1,6 +1,7 @@
package org.betterx.bclib.presets.worldgen;
import net.minecraft.core.Holder;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.data.BuiltinRegistries;
@ -9,6 +10,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.WorldPresetTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
@ -18,6 +20,7 @@ import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.minecraft.world.level.levelgen.synth.NormalNoise;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Lifecycle;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.tag.TagAPI;
import org.betterx.bclib.api.tag.TagType;
@ -129,6 +132,63 @@ public class WorldPresets {
return createDefaultWorldFromPreset(registryAccess, RandomSource.create().nextLong());
}
public static WorldGenSettings replaceGenerator(
ResourceKey<LevelStem> dimensionKey,
ResourceKey<DimensionType> dimensionTypeKey,
int biomeSourceVersion,
RegistryAccess registryAccess,
WorldGenSettings worldGenSettings
) {
Optional<Holder<LevelStem>> oLevelStem = BCLChunkGenerator.referenceStemForVersion(
dimensionKey,
biomeSourceVersion,
registryAccess,
worldGenSettings.seed(),
worldGenSettings.generateStructures(),
worldGenSettings.generateStructures()
);
Registry<DimensionType> registry = registryAccess.registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
Registry<LevelStem> registry2 = withDimension(dimensionKey, dimensionTypeKey, registry,
worldGenSettings.dimensions(),
oLevelStem.map(l -> l.value().generator()).orElseThrow());
return new WorldGenSettings(worldGenSettings.seed(),
worldGenSettings.generateStructures(),
worldGenSettings.generateBonusChest(),
registry2);
}
public static Registry<LevelStem> withDimension(ResourceKey<LevelStem> dimensionKey,
ResourceKey<DimensionType> dimensionTypeKey,
Registry<DimensionType> registry,
Registry<LevelStem> registry2,
ChunkGenerator chunkGenerator) {
LevelStem levelStem = registry2.get(dimensionKey);
Holder<DimensionType> holder = levelStem == null
? registry.getOrCreateHolderOrThrow(dimensionTypeKey)
: levelStem.typeHolder();
return withDimension(dimensionKey, registry2, holder, chunkGenerator);
}
public static Registry<LevelStem> withDimension(ResourceKey<LevelStem> dimensionKey, Registry<LevelStem> registry,
Holder<DimensionType> holder,
ChunkGenerator chunkGenerator) {
MappedRegistry<LevelStem> writableRegistry = new MappedRegistry<LevelStem>(Registry.LEVEL_STEM_REGISTRY,
Lifecycle.experimental(),
null);
writableRegistry.register(dimensionKey,
new LevelStem(holder, chunkGenerator),
Lifecycle.stable());
for (Map.Entry<ResourceKey<LevelStem>, LevelStem> entry : registry.entrySet()) {
ResourceKey<LevelStem> resourceKey = entry.getKey();
if (resourceKey == dimensionKey) continue;
writableRegistry.register(resourceKey,
entry.getValue(),
registry.lifecycle(entry.getValue()));
}
return writableRegistry;
}
/**
* Registers a custom WorldPreset (with custom rules and behaviour)
* <p>