Correct World Preset handling
This commit is contained in:
parent
7466048a22
commit
7359c64fff
13 changed files with 451 additions and 129 deletions
|
@ -2,19 +2,40 @@ package org.betterx.bclib.presets.worldgen;
|
|||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
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.LevelStem;
|
||||
import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator;
|
||||
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
|
||||
import net.minecraft.world.level.levelgen.WorldGenSettings;
|
||||
import net.minecraft.world.level.levelgen.structure.StructureSet;
|
||||
import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import org.betterx.bclib.BCLib;
|
||||
import org.betterx.bclib.api.WorldDataAPI;
|
||||
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
|
||||
import org.betterx.bclib.util.ModUtil;
|
||||
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";
|
||||
private static final String TAG_BN_GEN_VERSION = "generator_version";
|
||||
|
||||
public static final Codec<BCLChunkGenerator> CODEC = RecordCodecBuilder
|
||||
.create((RecordCodecBuilder.Instance<BCLChunkGenerator> builderInstance) -> {
|
||||
final RecordCodecBuilder<BCLChunkGenerator, Registry<NormalNoise.NoiseParameters>> noiseGetter = RegistryOps
|
||||
|
@ -38,12 +59,12 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
|
|||
.apply(builderInstance, builderInstance.stable(BCLChunkGenerator::new));
|
||||
});
|
||||
|
||||
|
||||
public BCLChunkGenerator(Registry<StructureSet> registry,
|
||||
Registry<NormalNoise.NoiseParameters> registry2,
|
||||
BiomeSource biomeSource,
|
||||
Holder<NoiseGeneratorSettings> holder) {
|
||||
super(registry, registry2, biomeSource, holder);
|
||||
System.out.println("Chunk Generator: " + this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -58,4 +79,115 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<Holder<LevelStem>> referenceStemForVersion(
|
||||
ResourceKey<LevelStem> dimensionKey,
|
||||
int biomeSourceVersion,
|
||||
RegistryAccess registryAccess,
|
||||
long seed,
|
||||
boolean generateStructures,
|
||||
boolean generateBonusChest
|
||||
) {
|
||||
final WorldGenSettings referenceSettings;
|
||||
if (biomeSourceVersion == BIOME_SOURCE_VERSION_VANILLA) {
|
||||
referenceSettings = net.minecraft.world.level.levelgen.presets.WorldPresets.createNormalWorldFromPreset(
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest);
|
||||
} else if (biomeSourceVersion == BIOME_SOURCE_VERSION_SQUARE) {
|
||||
referenceSettings = WorldPresets.createWorldFromPreset(
|
||||
WorldPresets.BCL_WORLD_17,
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest);
|
||||
} else {
|
||||
referenceSettings = WorldPresets.createDefaultWorldFromPreset(
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest);
|
||||
}
|
||||
return referenceSettings.dimensions().getHolder(dimensionKey);
|
||||
}
|
||||
|
||||
public static int getBiomeVersionForCurrentWorld(ResourceKey<LevelStem> key) {
|
||||
final CompoundTag settingsNbt = getSettingsNbt();
|
||||
if (!settingsNbt.contains(key.location().toString())) return DEFAULT_BIOME_SOURCE_VERSION;
|
||||
return settingsNbt.getInt(key.location().toString());
|
||||
}
|
||||
|
||||
private static void writeDimensionVersion(WorldGenSettings settings,
|
||||
CompoundTag generatorSettings,
|
||||
ResourceKey<LevelStem> key) {
|
||||
var dimension = settings.dimensions().getHolder(key);
|
||||
if (dimension.isPresent()) {
|
||||
generatorSettings.putInt(key.location().toString(),
|
||||
getBiomeVersionForGenerator(dimension.get().value().generator()));
|
||||
} else {
|
||||
generatorSettings.putInt(key.location().toString(), getBiomeVersionForGenerator(null));
|
||||
}
|
||||
}
|
||||
|
||||
public static void initializeWorldData(WorldGenSettings settings) {
|
||||
final CompoundTag settingsNbt = getSettingsNbt();
|
||||
writeDimensionVersion(settings, settingsNbt, LevelStem.NETHER);
|
||||
writeDimensionVersion(settings, settingsNbt, LevelStem.END);
|
||||
}
|
||||
|
||||
private static CompoundTag getSettingsNbt() {
|
||||
return WorldDataAPI.getCompoundTag(BCLib.MOD_ID, TAG_GENERATOR);
|
||||
}
|
||||
|
||||
public static void migrateGeneratorSettings() {
|
||||
final CompoundTag settingsNbt = getSettingsNbt();
|
||||
|
||||
if (settingsNbt.size() == 0) {
|
||||
BCLib.LOGGER.info("Found World without generator Settings. Setting up data...");
|
||||
int biomeSourceVersion = DEFAULT_BIOME_SOURCE_VERSION;
|
||||
|
||||
final CompoundTag bclRoot = WorldDataAPI.getRootTag(BCLib.MOD_ID);
|
||||
|
||||
String bclVersion = "0.0.0";
|
||||
if (bclRoot.contains(TAG_VERSION)) {
|
||||
bclVersion = bclRoot.getString(TAG_VERSION);
|
||||
}
|
||||
boolean isPre18 = !ModUtil.isLargerOrEqualVersion(bclVersion, "1.0.0");
|
||||
|
||||
if (isPre18) {
|
||||
BCLib.LOGGER.info("World was create pre 1.18!");
|
||||
biomeSourceVersion = 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;
|
||||
}
|
||||
|
||||
BCLib.LOGGER.info("Set world to BiomeSource Version " + biomeSourceVersion);
|
||||
settingsNbt.putInt(LevelStem.NETHER.location().toString(), biomeSourceVersion);
|
||||
settingsNbt.putInt(LevelStem.END.location().toString(), biomeSourceVersion);
|
||||
|
||||
WorldDataAPI.saveFile(BCLib.MOD_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BCLib - Chunk Generator (" + Integer.toHexString(hashCode()) + ")";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.mojang.datafixers.util.Pair;
|
|||
import org.betterx.bclib.BCLib;
|
||||
import org.betterx.bclib.api.tag.TagAPI;
|
||||
import org.betterx.bclib.api.tag.TagType;
|
||||
import org.betterx.bclib.world.generator.BCLBiomeSource;
|
||||
import org.betterx.bclib.world.generator.BCLibEndBiomeSource;
|
||||
import org.betterx.bclib.world.generator.BCLibNetherBiomeSource;
|
||||
|
||||
|
@ -34,8 +35,9 @@ public class WorldPresets {
|
|||
Holder<DimensionType> dimension,
|
||||
Registry<StructureSet> structureSets,
|
||||
Registry<NormalNoise.NoiseParameters> noiseParameters,
|
||||
Holder<NoiseGeneratorSettings> generatorSettings) {
|
||||
BCLibNetherBiomeSource netherSource = new BCLibNetherBiomeSource(biomes);
|
||||
Holder<NoiseGeneratorSettings> generatorSettings,
|
||||
Optional<Integer> version) {
|
||||
BCLibNetherBiomeSource netherSource = new BCLibNetherBiomeSource(biomes, version);
|
||||
LevelStem bclNether = new LevelStem(
|
||||
dimension,
|
||||
new BCLChunkGenerator(
|
||||
|
@ -52,8 +54,9 @@ public class WorldPresets {
|
|||
Holder<DimensionType> dimension,
|
||||
Registry<StructureSet> structureSets,
|
||||
Registry<NormalNoise.NoiseParameters> noiseParameters,
|
||||
Holder<NoiseGeneratorSettings> generatorSettings) {
|
||||
BCLibEndBiomeSource netherSource = new BCLibEndBiomeSource(biomes);
|
||||
Holder<NoiseGeneratorSettings> generatorSettings,
|
||||
Optional<Integer> version) {
|
||||
BCLibEndBiomeSource netherSource = new BCLibEndBiomeSource(biomes, version);
|
||||
LevelStem bclEnd = new LevelStem(
|
||||
dimension,
|
||||
new BCLChunkGenerator(
|
||||
|
@ -78,18 +81,39 @@ public class WorldPresets {
|
|||
TagAPI.registerType(BuiltinRegistries.WORLD_PRESET, "tags/worldgen/world_preset");
|
||||
|
||||
public static final ResourceKey<WorldPreset> BCL_WORLD = register(BCLib.makeID("normal"));
|
||||
public static final ResourceKey<WorldPreset> BCL_WORLD_17 = register(BCLib.makeID("legacy_17"), false);
|
||||
|
||||
public static Optional<ResourceKey<WorldPreset>> DEFAULT = Optional.of(BCL_WORLD);
|
||||
|
||||
public static WorldGenSettings createWorldFromPreset(ResourceKey<WorldPreset> preset,
|
||||
RegistryAccess registryAccess,
|
||||
long seed,
|
||||
boolean generateStructures,
|
||||
boolean generateBonusChest) {
|
||||
WorldGenSettings settings = registryAccess
|
||||
.registryOrThrow(Registry.WORLD_PRESET_REGISTRY)
|
||||
.getHolderOrThrow(preset)
|
||||
.value()
|
||||
.createWorldGenSettings(seed, generateStructures, generateBonusChest);
|
||||
|
||||
for (LevelStem stem : settings.dimensions()) {
|
||||
if (stem.generator().getBiomeSource() instanceof BCLBiomeSource bcl) {
|
||||
bcl.setSeed(seed);
|
||||
}
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static WorldGenSettings createDefaultWorldFromPreset(RegistryAccess registryAccess,
|
||||
long seed,
|
||||
boolean generateStructures,
|
||||
boolean generateBonusChest) {
|
||||
return registryAccess
|
||||
.registryOrThrow(Registry.WORLD_PRESET_REGISTRY)
|
||||
.getHolderOrThrow(DEFAULT.orElseThrow())
|
||||
.value()
|
||||
.createWorldGenSettings(seed, generateStructures, generateBonusChest);
|
||||
return createWorldFromPreset(DEFAULT.orElseThrow(),
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest);
|
||||
}
|
||||
|
||||
public static Pair<WorldGenSettings, RegistryAccess.Frozen> defaultWorldDataSupplier(RegistryAccess.Frozen frozen) {
|
||||
|
@ -115,8 +139,14 @@ public class WorldPresets {
|
|||
* @return The key you may use to reference your new Preset
|
||||
*/
|
||||
public static ResourceKey<WorldPreset> register(ResourceLocation loc) {
|
||||
return register(loc, true);
|
||||
}
|
||||
|
||||
private static ResourceKey<WorldPreset> register(ResourceLocation loc, boolean addToNormal) {
|
||||
ResourceKey<WorldPreset> key = ResourceKey.create(Registry.WORLD_PRESET_REGISTRY, loc);
|
||||
WORLD_PRESETS.addUntyped(WorldPresetTags.NORMAL, key.location());
|
||||
if (addToNormal) {
|
||||
WORLD_PRESETS.addUntyped(WorldPresetTags.NORMAL, key.location());
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue