WorldPreset serialization and better structure for BiomeGenerator builder;

This commit is contained in:
Frank 2022-06-19 19:59:29 +02:00
parent d63d773bb8
commit c74b7b2d28
20 changed files with 365 additions and 244 deletions

View file

@ -1,16 +1,8 @@
package org.betterx.bclib.api.v2;
import org.betterx.bclib.api.v2.dataexchange.DataExchangeAPI;
import org.betterx.bclib.api.v2.datafixer.DataFixerAPI;
import org.betterx.bclib.api.v2.levelgen.LevelGenUtil;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.mixin.common.RegistryOpsAccessor;
import net.minecraft.client.gui.screens.worldselection.WorldGenSettingsComponent;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
@ -18,13 +10,11 @@ import net.minecraft.server.level.progress.ChunkProgressListener;
import net.minecraft.world.level.CustomSpawner;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.ServerLevelData;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;
/**
@ -36,66 +26,6 @@ public class LifeCycleAPI {
private final static List<BeforeLevelLoadCall> beforeLoadLevel = new ArrayList<>(2);
private static void worldCreationStarted(RegistryAccess access) {
InternalBiomeAPI.initRegistry(access);
}
public static void newWorldSetup(
LevelStorageSource.LevelStorageAccess levelStorageAccess,
WorldGenSettings settings
) {
DataExchangeAPI.prepareServerside();
InternalBiomeAPI.prepareNewLevel();
DataFixerAPI.createWorldData(levelStorageAccess, settings);
_runBeforeLevelLoad();
}
public static void newWorldSetup(
String levelID,
WorldGenSettings worldGenSettings,
LevelStorageSource levelSource
) {
DataExchangeAPI.prepareServerside();
InternalBiomeAPI.prepareNewLevel();
DataFixerAPI.createWorldData(levelSource, levelID, worldGenSettings);
_runBeforeLevelLoad();
}
public static void newWorldSetup(LevelStorageSource.LevelStorageAccess levelStorageAccess) {
InternalBiomeAPI.prepareNewLevel();
DataFixerAPI.fixData(levelStorageAccess, false, (didFix) -> {/* not called when showUI==false */});
_runBeforeLevelLoad();
}
public static WorldGenSettings worldLoadStarted(WorldGenSettings settings, Optional<RegistryOps<Tag>> registryOps) {
if (registryOps.orElse(null) instanceof RegistryOpsAccessor acc) {
InternalBiomeAPI.initRegistry(acc.bcl_getRegistryAccess());
}
settings = LevelGenUtil.fixSettingsInCurrentWorld(registryOps, settings);
return settings;
}
public static void worldCreationStarted(RegistryOps<Tag> regOps) {
if (regOps instanceof RegistryOpsAccessor acc) {
worldCreationStarted(acc.bcl_getRegistryAccess());
}
}
public static void worldCreationStarted(
Optional<LevelStorageSource.LevelStorageAccess> levelStorageAccess,
WorldGenSettingsComponent worldGenSettingsComponent
) {
worldCreationStarted(worldGenSettingsComponent.registryHolder());
if (levelStorageAccess.isPresent()) {
newWorldSetup(levelStorageAccess.get(), worldGenSettingsComponent.settings().worldGenSettings());
}
}
/**
* Register a callback that is called before a level is loaded or created,
* but after the {@link WorldDataAPI} was initialized and patches from

View file

@ -1,13 +1,11 @@
package org.betterx.bclib.api.v2;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.datafixer.DataFixerAPI;
import org.betterx.bclib.util.ModUtil;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
@ -20,14 +18,13 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
/**
* Mod-specifix data-storage for a world.
* <p>
* This class provides the ability for mod to store persistent data inside a world. The Storage for the world is
* currently initialized as part of the {@link DataFixerAPI} in {@link DataFixerAPI#fixData(LevelStorageAccess, boolean, Consumer)}
* or {@link DataFixerAPI#initializeWorldData(File, boolean)}
* currently initialized as part of the {@link org.betterx.bclib.presets.worldgen.WorldBootstrap} in
* {@link org.betterx.bclib.presets.worldgen.WorldBootstrap#initializeWorldDataAPI(File, boolean)}
*/
public class WorldDataAPI {
private static final Map<String, CompoundTag> TAGS = Maps.newHashMap();

View file

@ -2,7 +2,6 @@ package org.betterx.bclib.api.v2.datafixer;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.WorldDataAPI;
import org.betterx.bclib.api.v2.levelgen.LevelGenUtil;
import org.betterx.bclib.client.gui.screens.AtomicProgressListener;
import org.betterx.bclib.client.gui.screens.ConfirmFixScreen;
import org.betterx.bclib.client.gui.screens.LevelFixErrorScreen;
@ -19,7 +18,6 @@ import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.storage.LevelResource;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
@ -135,77 +133,17 @@ public class DataFixerAPI {
Consumer<Boolean> onResume
) {
File levelPath = levelStorageAccess.getLevelPath(LevelResource.ROOT).toFile();
File levelDat = levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile();
boolean newWorld = false;
if (!levelDat.exists()) {
BCLib.LOGGER.info("Creating a new World, no fixes needed");
newWorld = true;
}
initializeWorldData(levelPath, newWorld);
if (newWorld) return false;
return fixData(levelPath, levelStorageAccess.getLevelId(), showUI, onResume);
}
/**
* Initializes the DataStorage for this world. If the world is new, the patch registry is initialized to the
* current versions of the plugins.
* <p>
* This implementation will create a new {@link LevelStorageAccess} and call {@link #initializeWorldData(File, boolean)}
* using the provided root path.
*
* @param levelSource The SourceStorage for this Minecraft instance, You can get this using
* {@code Minecraft.getInstance().getLevelSource()}
* @param levelID The ID of the Level you want to patch
* @param newWorld {@code true} if this is a fresh world
* Creates the patch level file for new worlds
*/
public static void initializeWorldData(LevelStorageSource levelSource, String levelID, boolean newWorld) {
wrapCall(levelSource, levelID, (levelStorageAccess) -> {
initializeWorldData(levelStorageAccess, newWorld);
return true;
});
}
public static void createWorldData(LevelStorageSource levelSource, String levelID, WorldGenSettings settings) {
wrapCall(levelSource, levelID, (levelStorageAccess) -> {
createWorldData(levelStorageAccess, settings);
return true;
});
}
/**
* Initializes the DataStorage for this world. If the world is new, the patch registry is initialized to the
* current versions of the plugins.
*
* @param access levelAccess for the worldd
* @param newWorld {@code true} if this is a fresh world
*/
public static void initializeWorldData(LevelStorageAccess access, boolean newWorld) {
initializeWorldData(access.getLevelPath(LevelResource.ROOT).toFile(), newWorld);
}
public static void createWorldData(LevelStorageAccess access, WorldGenSettings settings) {
initializeWorldData(access, true);
LevelGenUtil.initializeWorldData(settings);
public static void initializePatchData() {
getMigrationProfile().markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
}
/**
* Initializes the DataStorage for this world. If the world is new, the patch registry is initialized to the
* current versions of the plugins.
*
* @param levelBaseDir Folder of the world
* @param newWorld {@code true} if this is a fresh world
*/
public static void initializeWorldData(File levelBaseDir, boolean newWorld) {
WorldDataAPI.load(new File(levelBaseDir, "data"));
if (newWorld) {
getMigrationProfile().markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
}
}
@Environment(EnvType.CLIENT)
private static AtomicProgressListener showProgressScreen() {

View file

@ -104,9 +104,6 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
}
}
public static void injectNoiseSettings(WorldGenSettings settings) {
BCLChunkGenerator.injectNoiseSettings(settings, BCLChunkGenerator.ALL_DIMENSIONS);
}
public static void injectNoiseSettings(
ResourceKey<LevelStem> dimensionKey,
@ -127,7 +124,6 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
}
public static final Predicate<ResourceKey<LevelStem>> NON_MANAGED_DIMENSIONS = dim -> dim != LevelStem.NETHER && dim != LevelStem.END;
public static final Predicate<ResourceKey<LevelStem>> ALL_DIMENSIONS = dim -> true;
public static void injectNoiseSettings(WorldGenSettings settings, Predicate<ResourceKey<LevelStem>> filter) {
List<ResourceKey<LevelStem>> otherDimensions = settings

View file

@ -16,6 +16,7 @@ import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BiomeTags;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.biome.Climate;
@ -147,13 +148,13 @@ public class BCLibNetherBiomeSource extends BCLBiomeSource {
private static boolean isValidNetherBiome(Holder<Biome> biome, ResourceLocation location) {
return biome.unwrapKey().get().location().toString().equals("betternether:crimson_glowing_woods")
|| biome.unwrapKey().get().location().toString().equals("minecraft:crimson_forest")
|| biome.unwrapKey().get().location().toString().equals("betternether:crimson_pinewood");
// return biome.unwrapKey().get().location().toString().equals("betternether:crimson_glowing_woods")
// || biome.unwrapKey().get().location().toString().equals("minecraft:crimson_forest")
// || biome.unwrapKey().get().location().toString().equals("betternether:crimson_pinewood");
// return NetherBiomes.canGenerateInNether(biome.unwrapKey().get()) ||
// biome.is(BiomeTags.IS_NETHER) ||
// BiomeAPI.wasRegisteredAsNetherBiome(location);
return NetherBiomes.canGenerateInNether(biome.unwrapKey().get()) ||
biome.is(BiomeTags.IS_NETHER) ||
BiomeAPI.wasRegisteredAsNetherBiome(location);
}
private static boolean isValidNonVanillaNetherBiome(Holder<Biome> biome, ResourceLocation location) {

View file

@ -6,7 +6,7 @@ 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;
import org.betterx.bclib.mixin.common.RegistryOpsAccessor;
import org.betterx.bclib.presets.worldgen.BCLWorldPreset;
import org.betterx.bclib.presets.worldgen.BCLWorldPresetSettings;
import org.betterx.bclib.presets.worldgen.BCLWorldPresets;
import org.betterx.bclib.presets.worldgen.WorldPresetSettings;
@ -43,7 +43,7 @@ import org.jetbrains.annotations.NotNull;
public class LevelGenUtil {
private static final String TAG_VERSION = "version";
private static final String TAG_BN_GEN_VERSION = "generator_version";
private static final String TAG_GENERATOR = "generator";
public static final String TAG_GENERATOR = "generator";
@NotNull
public static LevelStem getBCLNetherLevelStem(Context context, Optional<Integer> version) {
@ -81,27 +81,6 @@ public class LevelGenUtil {
return getBCLEndLevelStem(context, endSource);
}
/**
* Datapacks can change the world's generator. This Method will ensure, that the Generators contain
* the correct BiomeSources for this world
*
* @param settings
* @return
* @see BCLChunkGenerator#injectNoiseSettings(WorldGenSettings) for the correcponding behaviour
* for new worlds
*/
public static WorldGenSettings fixSettingsInCurrentWorld(
Optional<RegistryOps<Tag>> registryOps,
WorldGenSettings settings
) {
if (registryOps.orElse(null) instanceof RegistryOpsAccessor acc) {
return getWorldSettings().repairSettingsOnLoad(acc.bcl_getRegistryAccess(), settings);
} else {
BCLib.LOGGER.error("Unable to obtain registryAccess when enforcing generators.");
}
return settings;
}
public static WorldGenSettings createWorldFromPreset(
ResourceKey<WorldPreset> preset,
RegistryAccess registryAccess,
@ -325,27 +304,6 @@ public class LevelGenUtil {
generatorSettings.putInt(key.location().toString(), getDimensionVersion(settings, key));
}
public static void initializeWorldData(WorldGenSettings settings) {
updateWorldData(getDimensionVersion(settings, LevelStem.NETHER), getDimensionVersion(settings, LevelStem.END));
}
public static void updateWorldData(int netherVersion, int endVersion) {
BCLWorldPresetSettings worldSettings = new BCLWorldPresetSettings(netherVersion, endVersion);
final RegistryAccess registryAccess = RegistryAccess.builtinCopy();
final RegistryOps<Tag> registryOps = RegistryOps.create(NbtOps.INSTANCE, registryAccess);
final var codec = WorldPresetSettings.CODEC.orElse(worldSettings);
final var encodeResult = codec.encodeStart(registryOps, worldSettings);
if (encodeResult.result().isPresent()) {
final CompoundTag settingsNbt = WorldDataAPI.getRootTag(BCLib.TOGETHER_WORLDS);
settingsNbt.put(TAG_GENERATOR, encodeResult.result().get());
} else {
BCLib.LOGGER.error("Unable to encode world generator settings generator for level.dat.");
}
WorldDataAPI.saveFile(BCLib.TOGETHER_WORLDS);
}
static CompoundTag getSettingsNbt() {
return WorldDataAPI.getCompoundTag(BCLib.TOGETHER_WORLDS, TAG_GENERATOR);
}
@ -390,7 +348,7 @@ public class LevelGenUtil {
}
BCLib.LOGGER.info("Set world to BiomeSource Version " + biomeSourceVersion);
updateWorldData(biomeSourceVersion, biomeSourceVersion);
BCLWorldPreset.writeWorldPresetSettings(new BCLWorldPresetSettings(biomeSourceVersion, biomeSourceVersion));
}
}

View file

@ -5,11 +5,14 @@ import org.betterx.bclib.interfaces.BiomeSourceAccessor;
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
import org.betterx.bclib.interfaces.SurfaceRuleProvider;
import org.betterx.bclib.mixin.common.BiomeGenerationSettingsAccessor;
import org.betterx.bclib.mixin.common.RegistryOpsAccessor;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
@ -65,6 +68,21 @@ public class InternalBiomeAPI {
})));
}
public static RegistryAccess worldRegistryAccess() {
return registryAccess;
}
/**
* Initialize registry for current server.
*
* @param regOps - registryOps for the current Session.
*/
public static void initRegistry(RegistryOps<Tag> regOps) {
if (regOps instanceof RegistryOpsAccessor acc) {
initRegistry(acc.bcl_getRegistryAccess());
}
}
/**
* Initialize registry for current server.
*
@ -159,12 +177,7 @@ public class InternalBiomeAPI {
}
}
}
/**
* Will apply biome modifications to world, internal usage only.
*
* @param level
*/
@Deprecated(forRemoval = true)
public static void applyModificationsDeprecated(ServerLevel level) {
//TODO: Now Disabled, because we fix the settings when everything gets loaded