[Change] Separated all World-Load related code to a new namespace

This commit is contained in:
Frank 2022-06-21 16:32:45 +02:00
parent 67d09676c4
commit 25fa53541f
79 changed files with 1924 additions and 814 deletions

View file

@ -28,7 +28,7 @@ public class LifeCycleAPI {
/**
* Register a callback that is called before a level is loaded or created,
* but after the {@link WorldDataAPI} was initialized and patches from
* but after the {@link org.betterx.worlds.together.world.WorldConfig} was initialized and patches from
* the {@link DataFixerAPI} were applied.
*
* @param call The callback Method

View file

@ -1,164 +1,77 @@
package org.betterx.bclib.api.v2;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.world.WorldConfig;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* 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 org.betterx.bclib.presets.worldgen.WorldBootstrap} in
* org.betterx.bclib.presets.worldgen.WorldBootstrap.Helpers#initializeWorldDataAPI(File, boolean)
* @deprecated Implementation moved to {@link WorldConfig}
*/
@Deprecated(forRemoval = true)
public class WorldDataAPI {
private static final Map<String, CompoundTag> TAGS = Maps.newHashMap();
private static final List<String> MODS = Lists.newArrayList();
private static final String TAG_CREATED = "create_version";
private static final String TAG_MODIFIED = "modify_version";
private static File dataDir;
/**
* @deprecated use {@link WorldConfig#load(File)} instead
*/
@Deprecated(forRemoval = true)
public static void load(File dataDir) {
WorldDataAPI.dataDir = dataDir;
MODS.stream()
.parallel()
.forEach(modID -> {
File file = new File(dataDir, modID + ".nbt");
CompoundTag root = new CompoundTag();
if (file.exists()) {
try {
root = NbtIo.readCompressed(file);
} catch (IOException e) {
BCLib.LOGGER.error("World data loading failed", e);
}
TAGS.put(modID, root);
} else {
Optional<ModContainer> optional = FabricLoader.getInstance()
.getModContainer(modID);
if (optional.isPresent()) {
ModContainer modContainer = optional.get();
if (BCLib.isDevEnvironment()) {
root.putString("version", "255.255.9999");
} else {
root.putString("version", modContainer.getMetadata()
.getVersion()
.toString());
}
TAGS.put(modID, root);
saveFile(modID);
}
}
});
WorldConfig.load(dataDir);
}
/**
* Register mod cache, world cache is located in world data folder.
*
* @param modID - {@link String} modID.
* @deprecated use {@link WorldConfig#registerModCache(String)} instead
*/
@Deprecated(forRemoval = true)
public static void registerModCache(String modID) {
if (!MODS.contains(modID))
MODS.add(modID);
WorldConfig.registerModCache(modID);
}
/**
* Get root {@link CompoundTag} for mod cache in world data folder.
*
* @param modID - {@link String} modID.
* @return {@link CompoundTag}
* @deprecated use {@link WorldConfig#getRootTag(String)} instead
*/
@Deprecated(forRemoval = true)
public static CompoundTag getRootTag(String modID) {
CompoundTag root = TAGS.get(modID);
if (root == null) {
root = new CompoundTag();
root.putString(TAG_CREATED, ModUtil.getModVersion(modID));
TAGS.put(modID, root);
}
return root;
return WorldConfig.getRootTag(modID);
}
/**
* @deprecated use {@link WorldConfig#hasMod(String)} instead
*/
@Deprecated(forRemoval = true)
public static boolean hasMod(String modID) {
return MODS.contains(modID);
return WorldConfig.hasMod(modID);
}
/**
* Get {@link CompoundTag} with specified path from mod cache in world data folder.
*
* @param modID - {@link String} path to tag, dot-separated.
* @return {@link CompoundTag}
* @deprecated use {@link WorldConfig#getCompoundTag(String, String)} instead
*/
@Deprecated(forRemoval = true)
public static CompoundTag getCompoundTag(String modID, String path) {
String[] parts = path.split("\\.");
CompoundTag tag = getRootTag(modID);
for (String part : parts) {
if (tag.contains(part)) {
tag = tag.getCompound(part);
} else {
CompoundTag t = new CompoundTag();
tag.put(part, t);
tag = t;
}
}
return tag;
return WorldConfig.getCompoundTag(modID, path);
}
/**
* Forces mod cache file to be saved.
*
* @param modID {@link String} mod ID.
* @deprecated use {@link WorldConfig#saveFile(String)} instead
*/
@Deprecated(forRemoval = true)
public static void saveFile(String modID) {
try {
if (!dataDir.exists()) {
dataDir.mkdirs();
}
CompoundTag tag = getRootTag(modID);
tag.putString(TAG_MODIFIED, ModUtil.getModVersion(modID));
final File tempFile = new File(dataDir, modID + "_temp.nbt");
NbtIo.writeCompressed(tag, tempFile);
final File oldFile = new File(dataDir, modID + "_old.nbt");
final File dataFile = new File(dataDir, modID + ".nbt");
Util.safeReplaceFile(dataFile, tempFile, oldFile);
} catch (IOException e) {
BCLib.LOGGER.error("World data saving failed", e);
}
WorldConfig.saveFile(modID);
}
/**
* Get stored mod version (only for mods with registered cache).
*
* @return {@link String} mod version.
* @deprecated use {@link WorldConfig#getModVersion(String)} instead
*/
@Deprecated(forRemoval = true)
public static String getModVersion(String modID) {
return getRootTag(modID).getString("version");
return WorldConfig.getModVersion(modID);
}
/**
* Get stored mod version as integer (only for mods with registered cache).
*
* @return {@code int} mod version.
* @deprecated use {@link WorldConfig#getIntModVersion(String)} instead
*/
@Deprecated(forRemoval = true)
public static int getIntModVersion(String modID) {
return ModUtil.convertModVersion(getModVersion(modID));
return WorldConfig.getIntModVersion(modID);
}
}

View file

@ -5,7 +5,7 @@ import org.betterx.bclib.api.v2.dataexchange.handler.DataExchange;
import org.betterx.bclib.api.v2.dataexchange.handler.autosync.AutoSync;
import org.betterx.bclib.api.v2.dataexchange.handler.autosync.AutoSyncID;
import org.betterx.bclib.config.Config;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil;
import net.minecraft.network.FriendlyByteBuf;

View file

@ -3,11 +3,11 @@ package org.betterx.bclib.api.v2.dataexchange.handler.autosync;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.dataexchange.DataHandler;
import org.betterx.bclib.api.v2.dataexchange.SyncFileHash;
import org.betterx.bclib.util.ModUtil;
import org.betterx.bclib.util.ModUtil.ModInfo;
import org.betterx.bclib.util.Pair;
import org.betterx.bclib.util.PathUtil;
import org.betterx.bclib.util.Triple;
import org.betterx.worlds.together.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil.ModInfo;
import org.betterx.worlds.together.util.PathUtil;
import net.minecraft.network.FriendlyByteBuf;
@ -200,7 +200,10 @@ class AutoFileSyncEntry extends AutoSyncID {
}
private int serializeFileContent(FriendlyByteBuf buf) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())) {
if (!org.betterx.worlds.together.util.PathUtil.isChildOf(
org.betterx.worlds.together.util.PathUtil.GAME_FOLDER,
fileName.toPath()
)) {
BCLib.LOGGER.error(fileName + " is not within game folder " + PathUtil.GAME_FOLDER + ". Pretending it does not exist.");
buf.writeInt(0);
return 0;

View file

@ -5,7 +5,7 @@ import org.betterx.bclib.api.v2.dataexchange.DataExchangeAPI;
import org.betterx.bclib.api.v2.dataexchange.SyncFileHash;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.config.ServerConfig;
import org.betterx.bclib.util.PathUtil;
import org.betterx.worlds.together.util.PathUtil;
import net.fabricmc.loader.api.FabricLoader;

View file

@ -2,7 +2,7 @@ package org.betterx.bclib.api.v2.dataexchange.handler.autosync;
import org.betterx.bclib.api.v2.dataexchange.DataHandler;
import org.betterx.bclib.config.Config;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil;
import net.minecraft.network.FriendlyByteBuf;

View file

@ -10,9 +10,9 @@ import org.betterx.bclib.client.gui.screens.SyncFilesScreen;
import org.betterx.bclib.client.gui.screens.WarnBCLibVersionMismatch;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.config.ServerConfig;
import org.betterx.bclib.util.ModUtil;
import org.betterx.bclib.util.ModUtil.ModInfo;
import org.betterx.bclib.util.PathUtil;
import org.betterx.worlds.together.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil.ModInfo;
import org.betterx.worlds.together.util.PathUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;

View file

@ -5,7 +5,7 @@ import org.betterx.bclib.api.v2.dataexchange.DataExchangeAPI;
import org.betterx.bclib.api.v2.dataexchange.DataHandler;
import org.betterx.bclib.api.v2.dataexchange.DataHandlerDescriptor;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;

View file

@ -6,8 +6,8 @@ import org.betterx.bclib.api.v2.dataexchange.DataHandlerDescriptor;
import org.betterx.bclib.client.gui.screens.ConfirmRestartScreen;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.util.Pair;
import org.betterx.bclib.util.PathUtil;
import org.betterx.bclib.util.Triple;
import org.betterx.worlds.together.util.PathUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;

View file

@ -5,7 +5,7 @@ import org.betterx.bclib.api.v2.dataexchange.DataHandler;
import org.betterx.bclib.api.v2.dataexchange.FileHash;
import org.betterx.bclib.api.v2.dataexchange.handler.autosync.AutoSyncID.ForDirectFileRequest;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.util.PathUtil;
import org.betterx.worlds.together.util.PathUtil;
import net.minecraft.network.FriendlyByteBuf;

View file

@ -1,7 +1,6 @@
package org.betterx.bclib.api.v2.datafixer;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.WorldDataAPI;
import org.betterx.bclib.client.gui.screens.AtomicProgressListener;
import org.betterx.bclib.client.gui.screens.ConfirmFixScreen;
import org.betterx.bclib.client.gui.screens.LevelFixErrorScreen;
@ -9,6 +8,7 @@ import org.betterx.bclib.client.gui.screens.LevelFixErrorScreen.Listener;
import org.betterx.bclib.client.gui.screens.ProgressScreen;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.util.Logger;
import org.betterx.worlds.together.world.WorldConfig;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
@ -141,7 +141,7 @@ public class DataFixerAPI {
*/
public static void initializePatchData() {
getMigrationProfile().markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
WorldConfig.saveFile(BCLib.MOD_ID);
}
@ -283,7 +283,7 @@ public class DataFixerAPI {
@NotNull
private static MigrationProfile getMigrationProfile() {
final CompoundTag patchConfig = WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
final CompoundTag patchConfig = WorldConfig.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
MigrationProfile profile = Patch.createMigrationData(patchConfig);
return profile;
}
@ -332,7 +332,7 @@ public class DataFixerAPI {
if (!state.didFail) {
progress.progressStage(Component.translatable("message.bclib.datafixer.progress.saving"));
profile.markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
WorldConfig.saveFile(BCLib.MOD_ID);
}
progress.incAtomic(maxProgress);
@ -511,7 +511,7 @@ public class DataFixerAPI {
static CompoundTag getPatchData() {
if (patchConfTag == null) {
patchConfTag = WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
patchConfTag = WorldConfig.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
}
return patchConfTag;
}

View file

@ -1,10 +1,10 @@
package org.betterx.bclib.api.v2.datafixer;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.WorldDataAPI;
import org.betterx.bclib.interfaces.PatchBiFunction;
import org.betterx.bclib.interfaces.PatchFunction;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil;
import org.betterx.worlds.together.world.WorldConfig;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -344,22 +344,22 @@ public class MigrationProfile {
public void patchWorldData() throws PatchDidiFailException {
for (Patch patch : worldDataPatchers) {
CompoundTag root = WorldDataAPI.getRootTag(patch.modID);
CompoundTag root = WorldConfig.getRootTag(patch.modID);
boolean changed = patch.getWorldDataPatcher().apply(root, this);
if (changed) {
WorldDataAPI.saveFile(patch.modID);
WorldConfig.saveFile(patch.modID);
}
}
for (Map.Entry<String, List<String>> entry : worldDataIDPaths.entrySet()) {
CompoundTag root = WorldDataAPI.getRootTag(entry.getKey());
CompoundTag root = WorldConfig.getRootTag(entry.getKey());
boolean[] changed = {false};
entry.getValue().forEach(path -> {
changed[0] |= replaceIDatPath(root, path);
});
if (changed[0]) {
WorldDataAPI.saveFile(entry.getKey());
WorldConfig.saveFile(entry.getKey());
}
}
}

View file

@ -1,9 +1,8 @@
package org.betterx.bclib.api.v2.datafixer;
import org.betterx.bclib.api.v2.WorldDataAPI;
import org.betterx.bclib.interfaces.PatchBiFunction;
import org.betterx.bclib.interfaces.PatchFunction;
import org.betterx.bclib.util.ModUtil;
import org.betterx.worlds.together.util.ModUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -149,7 +148,7 @@ public abstract class Patch {
/**
* Return a {@link PatchFunction} that is called with the content from the
* {@link WorldDataAPI} for this Mod.
* {@link org.betterx.worlds.together.world.WorldConfig} for this Mod.
* The function needs to return {@code true}, if changes were made to the data.
* If an error occurs, the method should throw a {@link PatchDidiFailException}
* <p>
@ -202,7 +201,7 @@ public abstract class Patch {
}
/**
* Returns a list of paths where your mod stores IDs in your {@link WorldDataAPI}-File.
* Returns a list of paths where your mod stores IDs in your {@link org.betterx.worlds.together.world.WorldConfig}-File.
* <p>
* {@link DataFixerAPI} will use information from the latest patch that returns a non-null-result. This list is used
* to automatically fix changed IDs from all active patches (see {@link Patch#getIDReplacements()}
@ -229,8 +228,8 @@ public abstract class Patch {
* if the leaf-entry is a {@link net.minecraft.nbt.ListTag}, it is handle the same as a child <i>items</i> entry
* of a {@link CompoundTag}.
*
* @return {@code null} if nothing changes or a list of Paths in your {@link WorldDataAPI}-File.
* Paths are dot-seperated (see {@link WorldDataAPI#getCompoundTag(String, String)}).
* @return {@code null} if nothing changes or a list of Paths in your {@link org.betterx.worlds.together.world.WorldConfig}-File.
* Paths are dot-seperated (see {@link org.betterx.worlds.together.world.WorldConfig#getCompoundTag(String, String)}).
*/
public List<String> getWorldDataIDPaths() {
return null;

View file

@ -1,6 +1,7 @@
package org.betterx.bclib.api.v2.generator;
import org.betterx.bclib.api.v2.levelgen.biomes.BiomeAPI;
import org.betterx.worlds.together.world.BiomeSourceWithSeed;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
@ -15,7 +16,7 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
public abstract class BCLBiomeSource extends BiomeSource {
public abstract class BCLBiomeSource extends BiomeSource implements BiomeSourceWithSeed {
public static int BIOME_SOURCE_VERSION_NONE = -1;
public static int BIOME_SOURCE_VERSION_VANILLA = 0;
public static int BIOME_SOURCE_VERSION_SQUARE = 17;

View file

@ -1,12 +1,13 @@
package org.betterx.bclib.api.v2.generator;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.levelgen.LevelGenUtil;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.api.v2.levelgen.surface.SurfaceRuleUtil;
import org.betterx.bclib.interfaces.NoiseGeneratorSettingsProvider;
import org.betterx.bclib.interfaces.SurfaceRuleProvider;
import org.betterx.bclib.mixin.common.ChunkGeneratorAccessor;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.world.WorldGenUtil;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@ -73,7 +74,7 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
bcl.setMaxHeight(holder.value().noiseSettings().height());
}
if (BCLib.RUNS_TERRABLENDER) {
if (WorldsTogether.RUNS_TERRABLENDER) {
BCLib.LOGGER.info("Make sure features are loaded from terrablender for " + biomeSource);
//terrablender is invalidating the feature initialization
@ -99,7 +100,7 @@ public class BCLChunkGenerator extends NoiseBasedChunkGenerator {
public void restoreInitialBiomeSource() {
if (initialBiomeSource != getBiomeSource()) {
if (this instanceof ChunkGeneratorAccessor acc) {
BiomeSource bs = LevelGenUtil.getWorldSettings()
BiomeSource bs = WorldGenUtil.getWorldSettings()
.fixBiomeSource(initialBiomeSource, getBiomeSource().possibleBiomes());
acc.bcl_setBiomeSource(bs);
rebuildFeaturesPerStep(getBiomeSource());

View file

@ -3,7 +3,6 @@ package org.betterx.bclib.api.v2.generator;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.generator.map.hex.HexBiomeMap;
import org.betterx.bclib.api.v2.generator.map.square.SquareBiomeMap;
import org.betterx.bclib.api.v2.levelgen.LevelGenUtil;
import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiome;
import org.betterx.bclib.api.v2.levelgen.biomes.BiomeAPI;
import org.betterx.bclib.config.ConfigKeeper.StringArrayEntry;
@ -12,6 +11,7 @@ import org.betterx.bclib.interfaces.BiomeMap;
import org.betterx.bclib.interfaces.TheEndBiomeDataAccessor;
import org.betterx.bclib.noise.OpenSimplexNoise;
import org.betterx.bclib.presets.worldgen.BCLWorldPresetSettings;
import org.betterx.worlds.together.world.WorldGenUtil;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@ -97,7 +97,7 @@ public class BCLibEndBiomeSource extends BCLBiomeSource {
boolean initMaps
) {
super(biomeRegistry, list, seed, version);
if (LevelGenUtil.getWorldSettings() instanceof BCLWorldPresetSettings settings) {
if (WorldGenUtil.getWorldSettings() instanceof BCLWorldPresetSettings settings) {
generateEndVoids = settings.generateEndVoid;
} else {
generateEndVoids = true;
@ -164,7 +164,7 @@ public class BCLibEndBiomeSource extends BCLBiomeSource {
this.centerBiome = biomeRegistry.getOrCreateHolderOrThrow(Biomes.THE_END);
this.barrens = biomeRegistry.getOrCreateHolderOrThrow(Biomes.END_BARRENS);
if (LevelGenUtil.getWorldSettings() instanceof BCLWorldPresetSettings settings
if (WorldGenUtil.getWorldSettings() instanceof BCLWorldPresetSettings settings
&& !settings.useEndTerrainGenerator) {
this.endLandFunction = null;
} else {

View file

@ -0,0 +1,119 @@
package org.betterx.bclib.api.v2.levelgen;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.LifeCycleAPI;
import org.betterx.bclib.api.v2.dataexchange.DataExchangeAPI;
import org.betterx.bclib.api.v2.datafixer.DataFixerAPI;
import org.betterx.bclib.api.v2.generator.BCLibEndBiomeSource;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.presets.worldgen.BCLWorldPresetSettings;
import org.betterx.bclib.registry.PresetsRegistry;
import org.betterx.worlds.together.world.event.WorldEvents;
import org.betterx.worlds.together.worldPreset.TogetherWorldPreset;
import org.betterx.worlds.together.worldPreset.settings.WorldPresetSettings;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
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.storage.LevelStorageSource;
import java.util.Optional;
import java.util.function.Consumer;
public class LevelGenEvents {
public static void setupWorld() {
InternalBiomeAPI.prepareNewLevel();
DataExchangeAPI.prepareServerside();
}
public static void register() {
WorldEvents.BEFORE_WORLD_LOAD.on(LevelGenEvents::prepareWorld);
WorldEvents.BEFORE_SERVER_WORLD_LOAD.on(LevelGenEvents::prepareServerWorld);
WorldEvents.ON_WORLD_LOAD.on(LevelGenEvents::onWorldLoad);
WorldEvents.WORLD_REGISTRY_READY.on(LevelGenEvents::onRegistryReady);
WorldEvents.PATCH_WORLD.on(LevelGenEvents::patchExistingWorld);
WorldEvents.ADAPT_WORLD_PRESET.on(LevelGenEvents::adaptWorldPresetSettings);
}
public static boolean patchExistingWorld(
LevelStorageSource.LevelStorageAccess storageAccess,
Consumer<Boolean> allDone
) {
return DataFixerAPI.fixData(storageAccess, true, allDone);
}
public static Optional<Holder<WorldPreset>> adaptWorldPresetSettings(
Optional<Holder<WorldPreset>> currentPreset,
WorldGenSettings worldGenSettings
) {
LevelStem endStem = worldGenSettings.dimensions().get(LevelStem.END);
//We probably loaded a Datapack for the End
if (!(endStem.generator().getBiomeSource() instanceof BCLibEndBiomeSource)) {
BCLib.LOGGER.info("Detected Datapack for END.");
if (currentPreset.isPresent()) {
if (currentPreset.get().value() instanceof TogetherWorldPreset worldPreset) {
ResourceKey key = currentPreset.get().unwrapKey().orElse(null);
//user did not configure the Preset!
if (PresetsRegistry.BCL_WORLD.equals(key) || PresetsRegistry.BCL_WORLD_17.equals(key)) {
if (worldPreset.settings instanceof BCLWorldPresetSettings settings) {
BCLib.LOGGER.info("Changing Default WorldPreset Settings for Datapack use.");
worldPreset = worldPreset.withSettings(new BCLWorldPresetSettings(
settings.netherVersion,
settings.endVersion,
false,
false
));
currentPreset = Optional.of(Holder.direct(worldPreset));
}
}
}
}
}
return currentPreset;
}
public static void onRegistryReady(RegistryAccess a) {
InternalBiomeAPI.initRegistry(a);
}
public static WorldPresetSettings prepareWorld(
LevelStorageSource.LevelStorageAccess storageAccess,
WorldPresetSettings settings,
boolean isNewWorld
) {
setupWorld();
if (isNewWorld) {
DataFixerAPI.initializePatchData();
}
return settings;
}
public static WorldPresetSettings prepareServerWorld(
LevelStorageSource.LevelStorageAccess storageAccess,
WorldPresetSettings settings,
boolean isNewWorld
) {
setupWorld();
if (isNewWorld) {
DataFixerAPI.initializePatchData();
} else {
DataFixerAPI.fixData(storageAccess, false, (didFix) -> {/* not called when showUI==false */});
}
return settings;
}
public static void onWorldLoad() {
LifeCycleAPI._runBeforeLevelLoad();
}
}

View file

@ -1,41 +1,29 @@
package org.betterx.bclib.api.v2.levelgen;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.WorldDataAPI;
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.presets.worldgen.BCLWorldPreset;
import org.betterx.bclib.presets.worldgen.BCLWorldPresetSettings;
import org.betterx.bclib.presets.worldgen.BCLWorldPresets;
import org.betterx.bclib.presets.worldgen.WorldPresetSettings;
import org.betterx.bclib.util.ModUtil;
import org.betterx.bclib.registry.PresetsRegistry;
import org.betterx.worlds.together.util.ModUtil;
import org.betterx.worlds.together.world.WorldConfig;
import org.betterx.worlds.together.world.WorldGenUtil;
import org.betterx.worlds.together.worldPreset.TogetherWorldPreset;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.Lifecycle;
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;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
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;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.minecraft.world.level.levelgen.synth.NormalNoise;
import java.util.Map;
import java.util.Optional;
@ -44,15 +32,14 @@ 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";
public static final String TAG_GENERATOR = "generator";
@NotNull
public static LevelStem getBCLNetherLevelStem(Context context, Optional<Integer> version) {
public static LevelStem getBCLNetherLevelStem(WorldGenUtil.Context context, Optional<Integer> version) {
BCLibNetherBiomeSource netherSource = new BCLibNetherBiomeSource(context.biomes, version);
return getBCLNetherLevelStem(context, netherSource);
}
public static LevelStem getBCLNetherLevelStem(StemContext context, BiomeSource biomeSource) {
public static LevelStem getBCLNetherLevelStem(WorldGenUtil.StemContext context, BiomeSource biomeSource) {
return new LevelStem(
context.dimension,
new BCLChunkGenerator(
@ -65,7 +52,7 @@ public class LevelGenUtil {
}
@NotNull
public static LevelStem getBCLEndLevelStem(StemContext context, BiomeSource biomeSource) {
public static LevelStem getBCLEndLevelStem(WorldGenUtil.StemContext context, BiomeSource biomeSource) {
return new LevelStem(
context.dimension,
new BCLChunkGenerator(
@ -77,60 +64,11 @@ public class LevelGenUtil {
);
}
public static LevelStem getBCLEndLevelStem(Context context, Optional<Integer> version) {
public static LevelStem getBCLEndLevelStem(WorldGenUtil.Context context, Optional<Integer> version) {
BCLibEndBiomeSource endSource = new BCLibEndBiomeSource(context.biomes, version);
return getBCLEndLevelStem(context, endSource);
}
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 createWorldFromPreset(
BCLWorldPresets.DEFAULT.orElseThrow(),
registryAccess,
seed,
generateStructures,
generateBonusChest
);
}
public static Pair<WorldGenSettings, RegistryAccess.Frozen> defaultWorldDataSupplier(RegistryAccess.Frozen frozen) {
WorldGenSettings worldGenSettings = createDefaultWorldFromPreset(frozen);
return Pair.of(worldGenSettings, frozen);
}
public static WorldGenSettings createDefaultWorldFromPreset(RegistryAccess registryAccess, long seed) {
return createDefaultWorldFromPreset(registryAccess, seed, true, false);
}
public static WorldGenSettings createDefaultWorldFromPreset(RegistryAccess registryAccess) {
return createDefaultWorldFromPreset(registryAccess, RandomSource.create().nextLong());
}
public static WorldGenSettings replaceGenerator(
ResourceKey<LevelStem> dimensionKey,
@ -261,15 +199,15 @@ public class LevelGenUtil {
generateBonusChest
);
} else if (biomeSourceVersion == BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE) {
referenceSettings = createWorldFromPreset(
BCLWorldPresets.BCL_WORLD_17,
referenceSettings = WorldGenUtil.createWorldFromPreset(
PresetsRegistry.BCL_WORLD_17,
registryAccess,
seed,
generateStructures,
generateBonusChest
);
} else {
referenceSettings = createDefaultWorldFromPreset(
referenceSettings = WorldGenUtil.createDefaultWorldFromPreset(
registryAccess,
seed,
generateStructures,
@ -280,7 +218,7 @@ public class LevelGenUtil {
}
public static int getBiomeVersionForCurrentWorld(ResourceKey<LevelStem> key) {
final CompoundTag settingsNbt = getSettingsNbt();
final CompoundTag settingsNbt = WorldGenUtil.getSettingsNbt();
if (!settingsNbt.contains(key.location().toString())) return BCLBiomeSource.DEFAULT_BIOME_SOURCE_VERSION;
return settingsNbt.getInt(key.location().toString());
}
@ -305,30 +243,14 @@ public class LevelGenUtil {
generatorSettings.putInt(key.location().toString(), getDimensionVersion(settings, key));
}
static CompoundTag getSettingsNbt() {
return WorldDataAPI.getCompoundTag(BCLib.TOGETHER_WORLDS, TAG_GENERATOR);
}
public static WorldPresetSettings getWorldSettings() {
if (BuiltinRegistries.ACCESS == null) return null;
final RegistryAccess registryAccess = BuiltinRegistries.ACCESS;
final RegistryOps<Tag> registryOps = RegistryOps.create(NbtOps.INSTANCE, registryAccess);
Optional<WorldPresetSettings> oLevelStem = WorldPresetSettings.CODEC
.parse(new Dynamic<>(registryOps, getSettingsNbt()))
.resultOrPartial(BCLib.LOGGER::error);
return oLevelStem.orElse(BCLWorldPresetSettings.DEFAULT);
}
public static void migrateGeneratorSettings() {
final CompoundTag settingsNbt = getSettingsNbt();
final CompoundTag settingsNbt = WorldGenUtil.getSettingsNbt();
if (settingsNbt.size() == 0) {
BCLib.LOGGER.info("Found World without generator Settings. Setting up data...");
int biomeSourceVersion = BCLBiomeSource.DEFAULT_BIOME_SOURCE_VERSION;
final CompoundTag bclRoot = WorldDataAPI.getRootTag(BCLib.MOD_ID);
final CompoundTag bclRoot = WorldConfig.getRootTag(BCLib.MOD_ID);
String bclVersion = "0.0.0";
if (bclRoot.contains(TAG_VERSION)) {
@ -341,16 +263,16 @@ public class LevelGenUtil {
biomeSourceVersion = BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE;
}
if (WorldDataAPI.hasMod("betternether")) {
if (WorldConfig.hasMod("betternether")) {
BCLib.LOGGER.info("Found Data from BetterNether, using for migration.");
final CompoundTag bnRoot = WorldDataAPI.getRootTag("betternether");
final CompoundTag bnRoot = WorldConfig.getRootTag("betternether");
biomeSourceVersion = "1.17".equals(bnRoot.getString(TAG_BN_GEN_VERSION))
? BCLBiomeSource.BIOME_SOURCE_VERSION_SQUARE
: BCLBiomeSource.BIOME_SOURCE_VERSION_HEX;
}
BCLib.LOGGER.info("Set world to BiomeSource Version " + biomeSourceVersion);
BCLWorldPreset.writeWorldPresetSettings(new BCLWorldPresetSettings(
TogetherWorldPreset.writeWorldPresetSettings(new BCLWorldPresetSettings(
biomeSourceVersion,
biomeSourceVersion,
true,
@ -358,38 +280,4 @@ public class LevelGenUtil {
));
}
}
public static class StemContext {
public final Holder<DimensionType> dimension;
public final Registry<StructureSet> structureSets;
public final Registry<NormalNoise.NoiseParameters> noiseParameters;
public final Holder<NoiseGeneratorSettings> generatorSettings;
public StemContext(
Holder<DimensionType> dimension,
Registry<StructureSet> structureSets,
Registry<NormalNoise.NoiseParameters> noiseParameters,
Holder<NoiseGeneratorSettings> generatorSettings
) {
this.dimension = dimension;
this.structureSets = structureSets;
this.noiseParameters = noiseParameters;
this.generatorSettings = generatorSettings;
}
}
public static class Context extends StemContext {
public final Registry<Biome> biomes;
public Context(
Registry<Biome> biomes, Holder<DimensionType> dimension,
Registry<StructureSet> structureSets,
Registry<NormalNoise.NoiseParameters> noiseParameters,
Holder<NoiseGeneratorSettings> generatorSettings
) {
super(dimension, structureSets, noiseParameters, generatorSettings);
this.biomes = biomes;
}
}
}

View file

@ -5,14 +5,11 @@ 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;
@ -72,17 +69,6 @@ public class InternalBiomeAPI {
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.
*
@ -177,7 +163,7 @@ public class InternalBiomeAPI {
}
}
}
@Deprecated(forRemoval = true)
public static void applyModificationsDeprecated(ServerLevel level) {
//TODO: Now Disabled, because we fix the settings when everything gets loaded