Upgrade older world presets
This commit is contained in:
parent
427d6dbdfd
commit
a064f2d6bb
6 changed files with 72 additions and 142 deletions
|
@ -17,10 +17,6 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
public abstract class BCLBiomeSource extends BiomeSource implements BiomeSourceWithSeed, MergeableBiomeSource<BCLBiomeSource> {
|
||||
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;
|
||||
protected final Registry<Biome> biomeRegistry;
|
||||
protected long currentSeed;
|
||||
protected int maxHeight;
|
||||
|
|
|
@ -98,6 +98,8 @@ public class LevelGenEvents {
|
|||
setupWorld();
|
||||
if (isNewWorld) {
|
||||
DataFixerAPI.initializePatchData();
|
||||
} else {
|
||||
LevelGenUtil.migrateGeneratorSettings();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,6 +113,7 @@ public class LevelGenEvents {
|
|||
if (isNewWorld) {
|
||||
DataFixerAPI.initializePatchData();
|
||||
} else {
|
||||
LevelGenUtil.migrateGeneratorSettings();
|
||||
DataFixerAPI.fixData(storageAccess, false, (didFix) -> {/* not called when showUI==false */});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.betterx.bclib.api.v2.levelgen;
|
||||
|
||||
import org.betterx.bclib.BCLib;
|
||||
import org.betterx.bclib.api.v2.generator.BCLBiomeSource;
|
||||
import org.betterx.bclib.api.v2.generator.BCLChunkGenerator;
|
||||
import org.betterx.bclib.api.v2.generator.BCLibEndBiomeSource;
|
||||
import org.betterx.bclib.api.v2.generator.BCLibNetherBiomeSource;
|
||||
|
@ -25,9 +24,11 @@ import net.minecraft.world.level.dimension.DimensionType;
|
|||
import net.minecraft.world.level.dimension.LevelStem;
|
||||
import net.minecraft.world.level.levelgen.WorldGenSettings;
|
||||
import net.minecraft.world.level.levelgen.presets.WorldPreset;
|
||||
import net.minecraft.world.level.levelgen.presets.WorldPresets;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LevelGenUtil {
|
||||
|
@ -63,30 +64,6 @@ public class LevelGenUtil {
|
|||
}
|
||||
|
||||
|
||||
public static WorldGenSettings replaceGenerator(
|
||||
ResourceKey<LevelStem> dimensionKey,
|
||||
ResourceKey<DimensionType> dimensionTypeKey,
|
||||
int biomeSourceVersion,
|
||||
RegistryAccess registryAccess,
|
||||
WorldGenSettings worldGenSettings
|
||||
) {
|
||||
Optional<Holder<LevelStem>> oLevelStem = referenceStemForVersion(
|
||||
dimensionKey,
|
||||
biomeSourceVersion,
|
||||
registryAccess,
|
||||
worldGenSettings.seed(),
|
||||
worldGenSettings.generateStructures(),
|
||||
worldGenSettings.generateStructures()
|
||||
);
|
||||
return replaceGenerator(
|
||||
dimensionKey,
|
||||
dimensionTypeKey,
|
||||
registryAccess,
|
||||
worldGenSettings,
|
||||
oLevelStem.map(l -> l.value().generator()).orElseThrow()
|
||||
);
|
||||
}
|
||||
|
||||
public static WorldGenSettings replaceGenerator(
|
||||
ResourceKey<LevelStem> dimensionKey,
|
||||
ResourceKey<DimensionType> dimensionTypeKey,
|
||||
|
@ -110,24 +87,6 @@ public class LevelGenUtil {
|
|||
);
|
||||
}
|
||||
|
||||
// public static WorldGenSettings replaceStem(
|
||||
// ResourceKey<LevelStem> dimensionKey,
|
||||
// WorldGenSettings worldGenSettings,
|
||||
// LevelStem levelStem
|
||||
// ) {
|
||||
// Registry<LevelStem> newDimensions = withDimension(
|
||||
// dimensionKey,
|
||||
// worldGenSettings.dimensions(),
|
||||
// levelStem
|
||||
// );
|
||||
// return new WorldGenSettings(
|
||||
// worldGenSettings.seed(),
|
||||
// worldGenSettings.generateStructures(),
|
||||
// worldGenSettings.generateBonusChest(),
|
||||
// newDimensions
|
||||
// );
|
||||
// }
|
||||
|
||||
public static Registry<LevelStem> withDimension(
|
||||
ResourceKey<LevelStem> dimensionKey,
|
||||
ResourceKey<DimensionType> dimensionTypeKey,
|
||||
|
@ -170,76 +129,47 @@ public class LevelGenUtil {
|
|||
return writableRegistry;
|
||||
}
|
||||
|
||||
// public static int getBiomeVersionForGenerator(ChunkGenerator generator) {
|
||||
// if (generator == null) return BCLBiomeSource.getVersionBiomeSource(null);
|
||||
// return BCLBiomeSource.getVersionBiomeSource(generator.getBiomeSource());
|
||||
// }
|
||||
|
||||
public static Optional<Holder<LevelStem>> referenceStemForVersion(
|
||||
ResourceKey<LevelStem> dimensionKey,
|
||||
int biomeSourceVersion,
|
||||
RegistryAccess registryAccess,
|
||||
long seed,
|
||||
boolean generateStructures,
|
||||
boolean generateBonusChest
|
||||
) {
|
||||
final WorldGenSettings referenceSettings;
|
||||
if (biomeSourceVersion == BCLBiomeSource.BIOME_SOURCE_VERSION_VANILLA) {
|
||||
referenceSettings = net.minecraft.world.level.levelgen.presets.WorldPresets.createNormalWorldFromPreset(
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest
|
||||
);
|
||||
} else if (biomeSourceVersion == BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE) {
|
||||
referenceSettings = WorldGenUtil.createWorldFromPreset(
|
||||
PresetsRegistry.BCL_WORLD_17,
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest
|
||||
);
|
||||
} else {
|
||||
referenceSettings = WorldGenUtil.createDefaultWorldFromPreset(
|
||||
registryAccess,
|
||||
seed,
|
||||
generateStructures,
|
||||
generateBonusChest
|
||||
);
|
||||
}
|
||||
return referenceSettings.dimensions().getHolder(dimensionKey);
|
||||
}
|
||||
|
||||
// public static int getBiomeVersionForCurrentWorld(ResourceKey<LevelStem> key) {
|
||||
// final CompoundTag settingsNbt = WorldGenUtil.getSettingsNbt();
|
||||
// if (!settingsNbt.contains(key.location().toString())) return BCLBiomeSource.DEFAULT_BIOME_SOURCE_VERSION;
|
||||
// return settingsNbt.getInt(key.location().toString());
|
||||
// }
|
||||
|
||||
// private static int getDimensionVersion(
|
||||
// WorldGenSettings settings,
|
||||
// ResourceKey<LevelStem> key
|
||||
// ) {
|
||||
// var dimension = settings.dimensions().getHolder(key);
|
||||
// if (dimension.isPresent()) {
|
||||
// return getBiomeVersionForGenerator(dimension.get().value().generator());
|
||||
// } else {
|
||||
// return getBiomeVersionForGenerator(null);
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static void writeDimensionVersion(
|
||||
// WorldGenSettings settings,
|
||||
// CompoundTag generatorSettings,
|
||||
// ResourceKey<LevelStem> key
|
||||
// ) {
|
||||
// generatorSettings.putInt(key.location().toString(), getDimensionVersion(settings, key));
|
||||
// }
|
||||
|
||||
public static void migrateGeneratorSettings() {
|
||||
final CompoundTag settingsNbt = WorldGenUtil.getSettingsNbt();
|
||||
final CompoundTag settingsNbt = WorldGenUtil.getPresetsNbt();
|
||||
|
||||
if (settingsNbt.size() == 0) {
|
||||
CompoundTag oldGen = WorldGenUtil.getGeneratorNbt();
|
||||
if (oldGen != null) {
|
||||
if (oldGen.contains("type")) {
|
||||
BCLib.LOGGER.info("Found World with beta generator Settings.");
|
||||
if ("bclib:bcl_world_preset_settings".equals(oldGen.getString("type"))) {
|
||||
int netherVersion = 18;
|
||||
int endVersion = 18;
|
||||
if (oldGen.contains("minecraft:the_nether"))
|
||||
netherVersion = oldGen.getInt("minecraft:the_nether");
|
||||
if (oldGen.contains("minecraft:the_end"))
|
||||
endVersion = oldGen.getInt("minecraft:the_end");
|
||||
|
||||
if (netherVersion == 18) netherVersion = 0;
|
||||
else if (netherVersion == 17) netherVersion = 1;
|
||||
else netherVersion = 2;
|
||||
|
||||
if (endVersion == 18) endVersion = 0;
|
||||
else if (endVersion == 17) endVersion = 1;
|
||||
else endVersion = 2;
|
||||
|
||||
var presets = List.of(
|
||||
TogetherWorldPreset.getDimensionsMap(PresetsRegistry.BCL_WORLD),
|
||||
TogetherWorldPreset.getDimensionsMap(PresetsRegistry.BCL_WORLD_17),
|
||||
TogetherWorldPreset.getDimensionsMap(WorldPresets.NORMAL)
|
||||
);
|
||||
Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions = new HashMap<>();
|
||||
dimensions.put(LevelStem.OVERWORLD, presets.get(0).get(LevelStem.OVERWORLD));
|
||||
dimensions.put(LevelStem.NETHER, presets.get(netherVersion).get(LevelStem.NETHER));
|
||||
dimensions.put(LevelStem.END, presets.get(endVersion).get(LevelStem.END));
|
||||
|
||||
TogetherWorldPreset.writeWorldPresetSettingsDirect(dimensions);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
BCLib.LOGGER.info("Found World without generator Settings. Setting up data...");
|
||||
ResourceKey<WorldPreset> biomeSourceVersion = PresetsRegistry.BCL_WORLD;
|
||||
|
||||
|
@ -273,12 +203,4 @@ public class LevelGenUtil {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @deprecated Replace by {@link WorldGenUtil#getWorldSettings()}
|
||||
// */
|
||||
// @Deprecated(forRemoval = true)
|
||||
// public static WorldPresetSettings getWorldSettings() {
|
||||
// return null;
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import net.minecraft.world.level.levelgen.structure.StructureSet;
|
|||
import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
||||
|
||||
public class WorldGenUtil {
|
||||
public static final String TAG_PRESET = "preset";
|
||||
public static final String TAG_GENERATOR = "generator";
|
||||
|
||||
public static WorldGenSettings createWorldFromPreset(
|
||||
|
@ -78,8 +79,15 @@ public class WorldGenUtil {
|
|||
return createDefaultWorldFromPreset(registryAccess, RandomSource.create().nextLong());
|
||||
}
|
||||
|
||||
public static CompoundTag getSettingsNbt() {
|
||||
public static CompoundTag getPresetsNbt() {
|
||||
return WorldConfig.getCompoundTag(WorldsTogether.MOD_ID, TAG_PRESET);
|
||||
}
|
||||
|
||||
public static CompoundTag getGeneratorNbt() {
|
||||
CompoundTag root = WorldConfig.getRootTag(WorldsTogether.MOD_ID);
|
||||
if (root.contains(TAG_GENERATOR))
|
||||
return WorldConfig.getCompoundTag(WorldsTogether.MOD_ID, TAG_GENERATOR);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Context extends StemContext {
|
||||
|
@ -122,7 +130,7 @@ public class WorldGenUtil {
|
|||
RegistryAccess registryAccess,
|
||||
WorldGenSettings settings
|
||||
) {
|
||||
var dimensions = TogetherWorldPreset.getWorldDimensions();
|
||||
var dimensions = TogetherWorldPreset.loadWorldDimensions();
|
||||
for (var entry : settings.dimensions().entrySet()) {
|
||||
ResourceKey<LevelStem> key = entry.getKey();
|
||||
LevelStem loadedStem = entry.getValue();
|
||||
|
|
|
@ -125,7 +125,7 @@ public class WorldBootstrap {
|
|||
Helpers.initializeWorldConfig(levelStorageAccess, false);
|
||||
WorldEventsImpl.BEFORE_SERVER_WORLD_LOAD.emit(e -> e.prepareWorld(
|
||||
levelStorageAccess,
|
||||
TogetherWorldPreset.getWorldDimensions(),
|
||||
TogetherWorldPreset.loadWorldDimensions(),
|
||||
false
|
||||
));
|
||||
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
|
||||
|
@ -235,7 +235,7 @@ public class WorldBootstrap {
|
|||
//Helpers.setupWorld();
|
||||
WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld(
|
||||
levelStorageAccess,
|
||||
TogetherWorldPreset.getWorldDimensions(),
|
||||
TogetherWorldPreset.loadWorldDimensions(),
|
||||
false
|
||||
));
|
||||
levelStorageAccess.close();
|
||||
|
|
|
@ -29,8 +29,6 @@ import org.jetbrains.annotations.NotNull;
|
|||
public class TogetherWorldPreset extends WorldPreset {
|
||||
public final int sortOrder;
|
||||
|
||||
private static final String TAG_GENERATOR = WorldGenUtil.TAG_GENERATOR;
|
||||
|
||||
private static int NEXT_IN_SORT_ORDER = 1000;
|
||||
|
||||
public TogetherWorldPreset(
|
||||
|
@ -80,6 +78,11 @@ public class TogetherWorldPreset extends WorldPreset {
|
|||
writeWorldPresetSettings(wrapper);
|
||||
}
|
||||
|
||||
public static void writeWorldPresetSettingsDirect(Map<ResourceKey<LevelStem>, ChunkGenerator> settings) {
|
||||
DimensionsWrapper wrapper = new DimensionsWrapper(settings);
|
||||
writeWorldPresetSettings(wrapper);
|
||||
}
|
||||
|
||||
private static void writeWorldPresetSettings(DimensionsWrapper wrapper) {
|
||||
final RegistryOps<Tag> registryOps = RegistryOps.create(
|
||||
NbtOps.INSTANCE,
|
||||
|
@ -89,7 +92,7 @@ public class TogetherWorldPreset extends WorldPreset {
|
|||
|
||||
if (encodeResult.result().isPresent()) {
|
||||
final CompoundTag settingsNbt = WorldConfig.getRootTag(WorldsTogether.MOD_ID);
|
||||
settingsNbt.put(TAG_GENERATOR, encodeResult.result().get());
|
||||
settingsNbt.put(WorldGenUtil.TAG_PRESET, encodeResult.result().get());
|
||||
} else {
|
||||
WorldsTogether.LOGGER.error("Unable to encode world generator settings for level.dat.");
|
||||
}
|
||||
|
@ -99,7 +102,7 @@ public class TogetherWorldPreset extends WorldPreset {
|
|||
|
||||
private static DimensionsWrapper DEFAULT_DIMENSIONS_WRAPPER = null;
|
||||
|
||||
public static @NotNull Map<ResourceKey<LevelStem>, ChunkGenerator> getWorldDimensions() {
|
||||
public static @NotNull Map<ResourceKey<LevelStem>, ChunkGenerator> loadWorldDimensions() {
|
||||
final RegistryAccess registryAccess;
|
||||
if (WorldBootstrap.getLastRegistryAccess() != null) {
|
||||
registryAccess = WorldBootstrap.getLastRegistryAccess();
|
||||
|
@ -107,21 +110,19 @@ public class TogetherWorldPreset extends WorldPreset {
|
|||
registryAccess = BuiltinRegistries.ACCESS;
|
||||
}
|
||||
final RegistryOps<Tag> registryOps = RegistryOps.create(NbtOps.INSTANCE, registryAccess);
|
||||
if (DEFAULT_DIMENSIONS_WRAPPER == null) {
|
||||
DEFAULT_DIMENSIONS_WRAPPER = new DimensionsWrapper(TogetherWorldPreset.getDimensionsMap(WorldPresets.DEFAULT));
|
||||
}
|
||||
|
||||
CompoundTag presetNBT = WorldGenUtil.getPresetsNbt();
|
||||
if (!presetNBT.contains("dimensions")) {
|
||||
return DEFAULT_DIMENSIONS_WRAPPER.dimensions;
|
||||
}
|
||||
|
||||
Optional<DimensionsWrapper> oLevelStem = DimensionsWrapper.CODEC
|
||||
.parse(new Dynamic<>(registryOps, WorldGenUtil.getSettingsNbt()))
|
||||
.parse(new Dynamic<>(registryOps, presetNBT))
|
||||
.resultOrPartial(WorldsTogether.LOGGER::error);
|
||||
|
||||
if (DEFAULT_DIMENSIONS_WRAPPER == null) {
|
||||
DEFAULT_DIMENSIONS_WRAPPER = new DimensionsWrapper(WorldPresets
|
||||
.get(
|
||||
registryAccess,
|
||||
WorldPresets.DEFAULT
|
||||
)
|
||||
.value()
|
||||
.createWorldGenSettings(0, true, true)
|
||||
.dimensions());
|
||||
}
|
||||
|
||||
return oLevelStem.orElse(DEFAULT_DIMENSIONS_WRAPPER).dimensions;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue