Streamlined WorldBootstrap

This commit is contained in:
Frank 2022-10-28 15:58:46 +02:00
parent d9a586741e
commit b4f9fdf95f
13 changed files with 172 additions and 141 deletions

View file

@ -38,22 +38,21 @@ public class LevelGenEvents {
} }
public static void register() { public static void register() {
WorldEvents.BEFORE_WORLD_LOAD.on(LevelGenEvents::prepareWorld); WorldEvents.BEFORE_WORLD_LOAD.on(LevelGenEvents::beforeWorldLoad);
WorldEvents.BEFORE_SERVER_WORLD_LOAD.on(LevelGenEvents::prepareServerWorld);
WorldEvents.ON_WORLD_LOAD.on(LevelGenEvents::onWorldLoad); WorldEvents.ON_WORLD_LOAD.on(LevelGenEvents::onWorldLoad);
WorldEvents.WORLD_REGISTRY_READY.on(LevelGenEvents::onRegistryReady); WorldEvents.WORLD_REGISTRY_READY.on(LevelGenEvents::worldRegistryReady);
WorldEvents.ON_FINALIZE_LEVEL_STEM.on(LevelGenEvents::finalizeStem); WorldEvents.ON_FINALIZE_LEVEL_STEM.on(LevelGenEvents::finalizeStem);
WorldEvents.ON_FINALIZED_WORLD_LOAD.on(LevelGenEvents::finalizedWorldLoad); WorldEvents.ON_FINALIZED_WORLD_LOAD.on(LevelGenEvents::finalizedWorldLoad);
WorldEvents.PATCH_WORLD.on(LevelGenEvents::patchExistingWorld); WorldEvents.PATCH_WORLD.on(LevelGenEvents::patchExistingWorld);
WorldEvents.ADAPT_WORLD_PRESET.on(LevelGenEvents::adaptWorldPresetSettings); WorldEvents.ADAPT_WORLD_PRESET.on(LevelGenEvents::adaptWorldPreset);
WorldEvents.BEFORE_ADDING_TAGS.on(LevelGenEvents::appplyTags); WorldEvents.BEFORE_ADDING_TAGS.on(LevelGenEvents::applyBiomeTags);
} }
private static void appplyTags( private static void applyBiomeTags(
String directory, String directory,
Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap
) { ) {
@ -67,10 +66,10 @@ public class LevelGenEvents {
LevelStorageSource.LevelStorageAccess storageAccess, LevelStorageSource.LevelStorageAccess storageAccess,
Consumer<Boolean> allDone Consumer<Boolean> allDone
) { ) {
return DataFixerAPI.fixData(storageAccess, true, allDone); return DataFixerAPI.fixData(storageAccess, allDone != null, allDone);
} }
private static Optional<Holder<WorldPreset>> adaptWorldPresetSettings( private static Optional<Holder<WorldPreset>> adaptWorldPreset(
Optional<Holder<WorldPreset>> currentPreset, Optional<Holder<WorldPreset>> currentPreset,
WorldDimensions worldDims WorldDimensions worldDims
) { ) {
@ -110,14 +109,15 @@ public class LevelGenEvents {
return currentPreset; return currentPreset;
} }
private static void onRegistryReady(RegistryAccess a) { private static void worldRegistryReady(RegistryAccess a) {
InternalBiomeAPI.initRegistry(a); InternalBiomeAPI.initRegistry(a);
} }
private static void prepareWorld( private static void beforeWorldLoad(
LevelStorageSource.LevelStorageAccess storageAccess, LevelStorageSource.LevelStorageAccess storageAccess,
Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions, Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions,
boolean isNewWorld boolean isNewWorld,
boolean isServer
) { ) {
setupWorld(); setupWorld();
if (isNewWorld) { if (isNewWorld) {
@ -128,22 +128,6 @@ public class LevelGenEvents {
} }
} }
private static void prepareServerWorld(
LevelStorageSource.LevelStorageAccess storageAccess,
Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions,
boolean isNewWorld
) {
setupWorld();
if (isNewWorld) {
WorldConfig.saveFile(BCLib.MOD_ID);
DataFixerAPI.initializePatchData();
} else {
LevelGenUtil.migrateGeneratorSettings();
DataFixerAPI.fixData(storageAccess, false, (didFix) -> {/* not called when showUI==false */});
}
}
private static void onWorldLoad() { private static void onWorldLoad() {
LifeCycleAPI._runBeforeLevelLoad(); LifeCycleAPI._runBeforeLevelLoad();
} }

View file

@ -1,18 +1,32 @@
package org.betterx.bclib.mixin.client; package org.betterx.bclib.mixin.client;
import org.betterx.bclib.client.BCLibClient;
import net.minecraft.client.resources.model.ModelManager; import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@Mixin(ModelManager.class) @Mixin(ModelManager.class)
public class ModelManagerMixin { public class ModelManagerMixin {
//TODO: 1.19.3 Disabled for now @Inject(method = "reload", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/util/profiling/ProfilerFiller;startTick()V"))
// @Inject(method = "prepare", at = @At("HEAD")) private void bclib_loadCustomModels(
// private void bclib_loadCustomModels( PreparableReloadListener.PreparationBarrier preparationBarrier,
// ResourceManager resourceManager, ResourceManager resourceManager,
// ProfilerFiller profilerFiller, ProfilerFiller profilerFiller,
// CallbackInfoReturnable<ModelBakery> info ProfilerFiller profilerFiller2,
// ) { Executor executor,
// BCLibClient.modelBakery.loadCustomModels(resourceManager); Executor executor2,
// } CallbackInfoReturnable<CompletableFuture<Void>> cir
) {
BCLibClient.modelBakery.loadCustomModels(resourceManager);
}
} }

View file

@ -29,6 +29,7 @@ import net.minecraft.world.level.levelgen.presets.WorldPreset;
import net.minecraft.world.level.levelgen.structure.StructureSet; import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.minecraft.world.level.levelgen.synth.NormalNoise; import net.minecraft.world.level.levelgen.synth.NormalNoise;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
public class WorldGenUtil { public class WorldGenUtil {
@ -228,7 +229,7 @@ public class WorldGenUtil {
RegistryAccess registryAccess, RegistryAccess registryAccess,
Registry<LevelStem> dimensionRegistry Registry<LevelStem> dimensionRegistry
) { ) {
var dimensions = TogetherWorldPreset.loadWorldDimensions(); Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions = TogetherWorldPreset.loadWorldDimensions();
for (var entry : dimensionRegistry.entrySet()) { for (var entry : dimensionRegistry.entrySet()) {
boolean didRepair = false; boolean didRepair = false;
ResourceKey<LevelStem> key = entry.getKey(); ResourceKey<LevelStem> key = entry.getKey();

View file

@ -36,7 +36,7 @@ public class CreateWorldScreen_Mixin {
WorldGenSettingsComponent worldGenSettingsComponent, WorldGenSettingsComponent worldGenSettingsComponent,
CallbackInfo ci CallbackInfo ci
) { ) {
WorldBootstrap.InGUI.registryReadyOnNewWorld(worldGenSettingsComponent); //WorldBootstrap.InGUI.registryReadyOnNewWorld(worldGenSettingsComponent);
} }
//Change the WorldPreset that is selected by default on the Create World Screen //Change the WorldPreset that is selected by default on the Create World Screen
@ -51,9 +51,4 @@ public class CreateWorldScreen_Mixin {
WorldBootstrap.InGUI.registryReadyOnNewWorld(this.worldGenSettingsComponent); WorldBootstrap.InGUI.registryReadyOnNewWorld(this.worldGenSettingsComponent);
WorldBootstrap.InGUI.setupNewWorld(cir.getReturnValue(), this.worldGenSettingsComponent); WorldBootstrap.InGUI.setupNewWorld(cir.getReturnValue(), this.worldGenSettingsComponent);
} }
@Inject(method = "onCreate", at = @At("HEAD"))
void wt_onCreate(CallbackInfo ci) {
System.out.println("there");
}
} }

View file

@ -35,13 +35,13 @@ public abstract class WorldOpenFlowsMixin {
WorldBootstrap.InGUI.setupLoadedWorld(levelID, this.levelSource); WorldBootstrap.InGUI.setupLoadedWorld(levelID, this.levelSource);
if (WorldBootstrap.InGUI.applyWorldPatches(levelSource, levelID, (appliedFixes) -> { if (WorldBootstrap.InGUI.applyWorldPatches(levelSource, levelID, (appliedFixes) -> {
WorldBootstrap.InGUI.finishedWorldLoad(levelID, this.levelSource); WorldBootstrap.finishedWorldLoad();
this.doLoadLevel(screen, levelID, false, false); this.doLoadLevel(screen, levelID, false, false);
})) { })) {
//cancel call when fix-screen is presented //cancel call when fix-screen is presented
ci.cancel(); ci.cancel();
} else { } else {
WorldBootstrap.InGUI.finishedWorldLoad(levelID, this.levelSource); WorldBootstrap.finishedWorldLoad();
if (WorldsTogether.SURPRESS_EXPERIMENTAL_DIALOG) { if (WorldsTogether.SURPRESS_EXPERIMENTAL_DIALOG) {
this.doLoadLevel(screen, levelID, false, false); this.doLoadLevel(screen, levelID, false, false);
//cancel call as we manually start the level load here //cancel call as we manually start the level load here

View file

@ -14,7 +14,7 @@ import java.util.Properties;
@Mixin(DedicatedServerProperties.class) @Mixin(DedicatedServerProperties.class)
public class DedicatedServerPropertiesMixin { public class DedicatedServerPropertiesMixin {
//Make sure the default server properties use our Default World Preset by default (read from "level-type") //Make sure the default server properties use our Default World Preset by default (read from "level-type")
@ModifyArg(method = "<init>", index = 3, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/dedicated/DedicatedServerProperties$WorldGenProperties;<init>(Ljava/lang/String;Lcom/google/gson/JsonObject;ZLjava/lang/String;)V")) @ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/dedicated/DedicatedServerProperties$WorldDimensionData;<init>(Lcom/google/gson/JsonObject;Ljava/lang/String;)V"))
protected String wt_defaultPreset(String string) { protected String wt_defaultPreset(String string) {
if (WorldsTogether.FORCE_SERVER_TO_BETTERX_PRESET) { if (WorldsTogether.FORCE_SERVER_TO_BETTERX_PRESET) {
return WorldPresets.getDEFAULT().location().toString(); return WorldPresets.getDEFAULT().location().toString();

View file

@ -15,9 +15,12 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable;
@Mixin(value = Main.class, priority = 200) @Mixin(value = Main.class, priority = 200)
abstract public class MainMixin { abstract public class MainMixin {
private static LevelStorageSource.LevelStorageAccess bcl_levelStorageAccess = null;
@ModifyVariable(method = "main", ordinal = 0, at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;getSummary()Lnet/minecraft/world/level/storage/LevelSummary;")) @ModifyVariable(method = "main", ordinal = 0, at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;getSummary()Lnet/minecraft/world/level/storage/LevelSummary;"))
private static LevelStorageSource.LevelStorageAccess bc_createAccess(LevelStorageSource.LevelStorageAccess levelStorageAccess) { private static LevelStorageSource.LevelStorageAccess bc_createAccess(LevelStorageSource.LevelStorageAccess levelStorageAccess) {
WorldBootstrap.DedicatedServer.setupWorld(levelStorageAccess); bcl_levelStorageAccess = levelStorageAccess;
WorldBootstrap.DedicatedServer.applyWorldPatches(levelStorageAccess);
return levelStorageAccess; return levelStorageAccess;
} }
@ -27,13 +30,7 @@ abstract public class MainMixin {
if (dynamicOps instanceof RegistryOps<Tag> regOps) { if (dynamicOps instanceof RegistryOps<Tag> regOps) {
WorldBootstrap.DedicatedServer.registryReady(regOps); WorldBootstrap.DedicatedServer.registryReady(regOps);
} }
WorldBootstrap.DedicatedServer.setupWorld(bcl_levelStorageAccess);
return dynamicOps; return dynamicOps;
} }
//TODO: 1.19.3 this may be obsolete, as datapacks are handled differently now
// @ModifyArg(method = "method_43613", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/PrimaryLevelData;<init>(Lnet/minecraft/world/level/LevelSettings;Lnet/minecraft/world/level/levelgen/WorldOptions;Lnet/minecraft/world/level/storage/PrimaryLevelData$SpecialWorldProperty;Lcom/mojang/serialization/Lifecycle;)V"))
// private static WorldGenSettings bcl_onCreateLevelData(WorldGenSettings worldGenSettings) {
// WorldBootstrap.DedicatedServer.applyDatapackChangesOnNewWorld(worldGenSettings);
// return worldGenSettings;
// }
} }

View file

@ -12,11 +12,10 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable;
@Mixin(WorldStem.class) @Mixin(WorldStem.class)
public class WorldStem_Mixin { public class WorldStem_Mixin {
//TODO:1.19.3 no general registry access available yet as the layerd access is generated after this
@ModifyVariable(method = "<init>", argsOnly = true, at = @At(value = "INVOKE", target = "Ljava/lang/Record;<init>()V", shift = At.Shift.AFTER)) @ModifyVariable(method = "<init>", argsOnly = true, at = @At(value = "INVOKE", target = "Ljava/lang/Record;<init>()V", shift = At.Shift.AFTER))
LayeredRegistryAccess<RegistryLayer> wt_bake(LayeredRegistryAccess<RegistryLayer> registries) { LayeredRegistryAccess<RegistryLayer> wt_bake(LayeredRegistryAccess<RegistryLayer> registries) {
return WorldBootstrap.enforceInLayeredRegistry(registries); LayeredRegistryAccess<RegistryLayer> rNew = WorldBootstrap.enforceInLayeredRegistry(registries);
return rNew;
} }
} }

View file

@ -11,6 +11,7 @@ public interface BeforeWorldLoad {
void prepareWorld( void prepareWorld(
LevelStorageSource.LevelStorageAccess storageAccess, LevelStorageSource.LevelStorageAccess storageAccess,
Map<ResourceKey<LevelStem>, ChunkGenerator> settings, Map<ResourceKey<LevelStem>, ChunkGenerator> settings,
boolean isNewWorld boolean isNewWorld,
boolean isServer
); );
} }

View file

@ -78,17 +78,17 @@ public class WorldBootstrap {
); );
} }
private static Map<ResourceKey<LevelStem>, ChunkGenerator> defaultServerDimensions() { private static WorldDimensions defaultServerDimensions() {
final Holder<WorldPreset> defaultPreset = defaultServerPreset(); final Holder<WorldPreset> defaultPreset = defaultServerPreset();
return defaultServerDimensions(defaultPreset); return defaultServerDimensions(defaultPreset);
} }
private static Map<ResourceKey<LevelStem>, ChunkGenerator> defaultServerDimensions(Holder<WorldPreset> defaultPreset) { private static WorldDimensions defaultServerDimensions(Holder<WorldPreset> defaultPreset) {
final Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions; final WorldDimensions dimensions;
if (defaultPreset.value() instanceof TogetherWorldPreset t) { if (defaultPreset.value() instanceof TogetherWorldPreset t) {
dimensions = t.getDimensionsMap(); dimensions = t.getWorldDimensions();
} else { } else {
dimensions = TogetherWorldPreset.getDimensionsMap(net.minecraft.world.level.levelgen.presets.WorldPresets.NORMAL); dimensions = TogetherWorldPreset.getWorldDimensions(net.minecraft.world.level.levelgen.presets.WorldPresets.NORMAL);
} }
return dimensions; return dimensions;
} }
@ -118,35 +118,38 @@ public class WorldBootstrap {
File levelDat = levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile(); File levelDat = levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile();
if (!levelDat.exists()) { if (!levelDat.exists()) {
WorldsTogether.LOGGER.info("Creating a new World, no fixes needed"); WorldsTogether.LOGGER.info("Creating a new World, no fixes needed");
final Map<ResourceKey<LevelStem>, ChunkGenerator> settings = Helpers.defaultServerDimensions(); final WorldDimensions dimensions = Helpers.defaultServerDimensions();
Helpers.initializeWorldConfig(levelStorageAccess, true); WorldBootstrap.setupWorld(
WorldEventsImpl.BEFORE_SERVER_WORLD_LOAD.emit(e -> e.prepareWorld( levelStorageAccess, TogetherWorldPreset.getDimensionMap(dimensions),
levelStorageAccess, settings, true true, true
)); );
Optional<Holder<WorldPreset>> currentPreset = Optional.of(Helpers.defaultServerPreset());
writeWorldPresets(dimensions, currentPreset);
finishedWorldLoad();
} else { } else {
Helpers.initializeWorldConfig(levelStorageAccess, false); WorldBootstrap.setupWorld(
WorldEventsImpl.BEFORE_SERVER_WORLD_LOAD.emit(e -> e.prepareWorld( levelStorageAccess, TogetherWorldPreset.loadWorldDimensions(),
levelStorageAccess, false, true
TogetherWorldPreset.loadWorldDimensions(), );
false finishedWorldLoad();
));
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
} }
} }
//Needs to get called after setupWorld public static boolean applyWorldPatches(
public static void applyDatapackChangesOnNewWorld(WorldDimensions worldDims) { LevelStorageSource.LevelStorageAccess levelStorageAccess
Optional<Holder<WorldPreset>> currentPreset = Optional.of(Helpers.defaultServerPreset()); ) {
currentPreset = WorldEventsImpl.ADAPT_WORLD_PRESET.emit(currentPreset, worldDims); boolean result = false;
if (levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile().exists()) {
if (currentPreset.map(Holder::value).orElse(null) instanceof WorldPresetAccessor acc) { try {
TogetherWorldPreset.writeWorldPresetSettings(acc.bcl_getDimensions()); result = WorldEventsImpl.PATCH_WORLD.applyPatches(levelStorageAccess, null);
} else { } catch (Exception e) {
WorldsTogether.LOGGER.error("Failed writing together File"); WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
TogetherWorldPreset.writeWorldPresetSettings(worldDims.dimensions()); }
} }
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
return result;
} }
} }
@ -156,13 +159,13 @@ public class WorldBootstrap {
} }
public static void registryReadyOnLoadedWorld(Optional<RegistryOps<Tag>> registryOps) { public static void registryReadyOnLoadedWorld(Optional<RegistryOps<Tag>> registryOps) {
if (registryOps.orElse(null) instanceof RegistryOpsAccessor acc) { // if (registryOps.orElse(null) instanceof RegistryOpsAccessor acc) {
Helpers.onRegistryReady(acc.bcl_getRegistryAccess()); // Helpers.onRegistryReady(acc.bcl_getRegistryAccess());
} // }
} }
public static void registryReady(RegistryAccess access) { public static void registryReady(RegistryAccess access) {
Helpers.onRegistryReady(access); //Helpers.onRegistryReady(access);
} }
public static void setupNewWorld( public static void setupNewWorld(
@ -195,35 +198,16 @@ public class WorldBootstrap {
Optional<Holder<WorldPreset>> currentPreset, Optional<Holder<WorldPreset>> currentPreset,
WorldDimensions worldDims WorldDimensions worldDims
) { ) {
Helpers.initializeWorldConfig(levelStorageAccess, true); final WorldDimensions dimensions;
final Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions;
if (currentPreset.map(Holder::value).orElse(null) instanceof TogetherWorldPreset t) { if (currentPreset.map(Holder::value).orElse(null) instanceof TogetherWorldPreset t) {
dimensions = t.getDimensionsMap(); dimensions = t.getWorldDimensions();
} else { } else {
dimensions = TogetherWorldPreset.getDimensionsMap(net.minecraft.world.level.levelgen.presets.WorldPresets.NORMAL); dimensions = TogetherWorldPreset.getWorldDimensions(net.minecraft.world.level.levelgen.presets.WorldPresets.NORMAL);
} }
// Helpers.setupWorld(); setupWorld(levelStorageAccess, TogetherWorldPreset.getDimensionMap(dimensions), true, false);
// DataFixerAPI.initializePatchData(); writeWorldPresets(worldDims, currentPreset);
WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld( finishedWorldLoad();
levelStorageAccess,
dimensions,
true
));
currentPreset = WorldEventsImpl.ADAPT_WORLD_PRESET.emit(currentPreset, worldDims);
if (currentPreset.map(Holder::value).orElse(null) instanceof WorldPresetAccessor acc) {
TogetherWorldPreset.writeWorldPresetSettings(acc.bcl_getDimensions());
} else {
WorldsTogether.LOGGER.error("Failed writing together File");
TogetherWorldPreset.writeWorldPresetSettings(worldDims.dimensions());
}
//LifeCycleAPI._runBeforeLevelLoad();
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
return currentPreset; return currentPreset;
} }
@ -238,18 +222,11 @@ public class WorldBootstrap {
WorldGenUtil.clearPreloadedWorldPresets(); WorldGenUtil.clearPreloadedWorldPresets();
try { try {
var levelStorageAccess = levelSource.createAccess(levelID); var levelStorageAccess = levelSource.createAccess(levelID);
try { WorldBootstrap.setupWorld(
Helpers.initializeWorldConfig(levelStorageAccess, false); levelStorageAccess,
TogetherWorldPreset.loadWorldDimensions(),
//Helpers.setupWorld(); false, false
WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld( );
levelStorageAccess,
TogetherWorldPreset.loadWorldDimensions(),
false
));
} catch (Exception e) {
WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
}
levelStorageAccess.close(); levelStorageAccess.close();
} catch (Exception e) { } catch (Exception e) {
WorldsTogether.LOGGER.error("Failed to acquire storage access", e); WorldsTogether.LOGGER.error("Failed to acquire storage access", e);
@ -273,14 +250,6 @@ public class WorldBootstrap {
return result; return result;
} }
public static void finishedWorldLoad(
String levelID,
LevelStorageSource levelSource
) {
//LifeCycleAPI._runBeforeLevelLoad();
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
WorldGenUtil.clearPreloadedWorldPresets();
}
} }
public static class InFreshLevel { public static class InFreshLevel {
@ -300,6 +269,39 @@ public class WorldBootstrap {
} }
} }
private static void setupWorld(
LevelStorageSource.LevelStorageAccess levelStorageAccess,
Map<ResourceKey<LevelStem>, ChunkGenerator> dimensions,
boolean newWorld, boolean isServer
) {
try {
Helpers.initializeWorldConfig(levelStorageAccess, newWorld);
WorldEventsImpl.BEFORE_WORLD_LOAD.emit(e -> e.prepareWorld(
levelStorageAccess,
dimensions,
newWorld, isServer
));
} catch (Exception e) {
WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
}
}
private static void writeWorldPresets(WorldDimensions dimensions, Optional<Holder<WorldPreset>> currentPreset) {
currentPreset = WorldEventsImpl.ADAPT_WORLD_PRESET.emit(currentPreset, dimensions);
if (currentPreset.map(Holder::value).orElse(null) instanceof WorldPresetAccessor acc) {
TogetherWorldPreset.writeWorldPresetSettings(acc.bcl_getDimensions());
} else {
WorldsTogether.LOGGER.error("Failed writing together File");
TogetherWorldPreset.writeWorldPresetSettings(dimensions);
}
}
public static void finishedWorldLoad() {
WorldEventsImpl.ON_WORLD_LOAD.emit(OnWorldLoad::onLoad);
WorldGenUtil.clearPreloadedWorldPresets();
}
public static void finalizeWorldGenSettings(Registry<LevelStem> dimensionRegistry) { public static void finalizeWorldGenSettings(Registry<LevelStem> dimensionRegistry) {
String output = "World Dimensions: "; String output = "World Dimensions: ";
for (var entry : dimensionRegistry.entrySet()) { for (var entry : dimensionRegistry.entrySet()) {
@ -327,18 +329,22 @@ public class WorldBootstrap {
public static LayeredRegistryAccess<RegistryLayer> enforceInLayeredRegistry(LayeredRegistryAccess<RegistryLayer> registries) { public static LayeredRegistryAccess<RegistryLayer> enforceInLayeredRegistry(LayeredRegistryAccess<RegistryLayer> registries) {
RegistryAccess access = registries.compositeAccess(); RegistryAccess access = registries.compositeAccess();
InGUI.registryReady(access); Helpers.onRegistryReady(access);
final Registry<LevelStem> dimensions = access.registryOrThrow(Registry.LEVEL_STEM_REGISTRY); final Registry<LevelStem> dimensions = access.registryOrThrow(Registry.LEVEL_STEM_REGISTRY);
final Registry<LevelStem> changedDimensions = WorldGenUtil.repairBiomeSourceInAllDimensions(access, dimensions); final Registry<LevelStem> changedDimensions = WorldGenUtil.repairBiomeSourceInAllDimensions(access, dimensions);
if (dimensions != changedDimensions) { if (dimensions != changedDimensions) {
if (Configs.MAIN_CONFIG.verboseLogging()) { if (Configs.MAIN_CONFIG.verboseLogging()) {
WorldsTogether.LOGGER.info("Loading originally configured Dimensions in World."); WorldsTogether.LOGGER.info("Loading originally configured Dimensions in World.");
} }
return registries.replaceFrom( registries = registries.replaceFrom(
RegistryLayer.DIMENSIONS, RegistryLayer.DIMENSIONS,
new RegistryAccess.ImmutableRegistryAccess(List.of(changedDimensions)).freeze() new RegistryAccess.ImmutableRegistryAccess(List.of(changedDimensions)).freeze()
); );
//this will generate a new access object we have to use from now on...
Helpers.onRegistryReady(registries.compositeAccess());
} }
return registries; return registries;
} }
} }

View file

@ -3,7 +3,6 @@ package org.betterx.worlds.together.world.event;
public class WorldEvents { public class WorldEvents {
public static final Event<OnWorldRegistryReady> WORLD_REGISTRY_READY = WorldEventsImpl.WORLD_REGISTRY_READY; public static final Event<OnWorldRegistryReady> WORLD_REGISTRY_READY = WorldEventsImpl.WORLD_REGISTRY_READY;
public static final Event<BeforeWorldLoad> BEFORE_WORLD_LOAD = WorldEventsImpl.BEFORE_WORLD_LOAD; public static final Event<BeforeWorldLoad> BEFORE_WORLD_LOAD = WorldEventsImpl.BEFORE_WORLD_LOAD;
public static final Event<BeforeServerWorldLoad> BEFORE_SERVER_WORLD_LOAD = WorldEventsImpl.BEFORE_SERVER_WORLD_LOAD;
public static final Event<OnWorldLoad> ON_WORLD_LOAD = WorldEventsImpl.ON_WORLD_LOAD; public static final Event<OnWorldLoad> ON_WORLD_LOAD = WorldEventsImpl.ON_WORLD_LOAD;
public static final Event<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = WorldEventsImpl.ON_FINALIZE_LEVEL_STEM; public static final Event<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = WorldEventsImpl.ON_FINALIZE_LEVEL_STEM;
public static final Event<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = WorldEventsImpl.ON_FINALIZED_WORLD_LOAD; public static final Event<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = WorldEventsImpl.ON_FINALIZED_WORLD_LOAD;

View file

@ -6,8 +6,7 @@ import org.jetbrains.annotations.ApiStatus;
public class WorldEventsImpl { public class WorldEventsImpl {
public static final EventImpl<OnWorldRegistryReady> WORLD_REGISTRY_READY = new EventImpl<>(); public static final EventImpl<OnWorldRegistryReady> WORLD_REGISTRY_READY = new EventImpl<>();
public static final EventImpl<BeforeWorldLoad> BEFORE_WORLD_LOAD = new EventImpl<>(); public static final EventImpl<BeforeWorldLoad> BEFORE_WORLD_LOAD = new EventImpl<>();
public static final EventImpl<BeforeServerWorldLoad> BEFORE_SERVER_WORLD_LOAD = new EventImpl<>();
public static final EventImpl<OnWorldLoad> ON_WORLD_LOAD = new EventImpl<>(); public static final EventImpl<OnWorldLoad> ON_WORLD_LOAD = new EventImpl<>();
public static final EventImpl<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = new EventImpl<>(); public static final EventImpl<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = new EventImpl<>();
public static final EventImpl<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = new EventImpl<>(); public static final EventImpl<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = new EventImpl<>();

View file

@ -9,9 +9,12 @@ import org.betterx.worlds.together.world.event.WorldBootstrap;
import com.mojang.serialization.Codec; import com.mojang.serialization.Codec;
import com.mojang.serialization.Dynamic; import com.mojang.serialization.Dynamic;
import com.mojang.serialization.Lifecycle;
import com.mojang.serialization.codecs.RecordCodecBuilder; import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
@ -19,6 +22,7 @@ import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.chunk.ChunkGenerator; import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.dimension.LevelStem; import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.WorldDimensions;
import net.minecraft.world.level.levelgen.presets.WorldPreset; import net.minecraft.world.level.levelgen.presets.WorldPreset;
import java.util.HashMap; import java.util.HashMap;
@ -30,6 +34,8 @@ public class TogetherWorldPreset extends WorldPreset {
public final int sortOrder; public final int sortOrder;
private static int NEXT_IN_SORT_ORDER = 1000; private static int NEXT_IN_SORT_ORDER = 1000;
private final WorldDimensions worldDimensions;
public TogetherWorldPreset( public TogetherWorldPreset(
Map<ResourceKey<LevelStem>, LevelStem> map, Map<ResourceKey<LevelStem>, LevelStem> map,
@ -44,6 +50,20 @@ public class TogetherWorldPreset extends WorldPreset {
) { ) {
super(map); super(map);
this.sortOrder = sortOrder; this.sortOrder = sortOrder;
this.worldDimensions = buildWorldDimensions(map);
}
public static WorldDimensions buildWorldDimensions(Map<ResourceKey<LevelStem>, LevelStem> map) {
Registry<LevelStem> registry = new MappedRegistry<>(Registry.LEVEL_STEM_REGISTRY, Lifecycle.experimental());
for (var entry : map.entrySet()) {
Registry.register(registry, entry.getKey(), entry.getValue());
}
return new WorldDimensions(registry);
}
public WorldDimensions getWorldDimensions() {
return this.worldDimensions;
} }
public TogetherWorldPreset withDimensions(Registry<LevelStem> dimensions) { public TogetherWorldPreset withDimensions(Registry<LevelStem> dimensions) {
@ -68,6 +88,10 @@ public class TogetherWorldPreset extends WorldPreset {
return getDimensions().get(key); return getDimensions().get(key);
} }
public static void writeWorldPresetSettings(WorldDimensions dimensions) {
writeWorldPresetSettings(dimensions.dimensions());
}
public static void writeWorldPresetSettings(Registry<LevelStem> dimensions) { public static void writeWorldPresetSettings(Registry<LevelStem> dimensions) {
DimensionsWrapper wrapper = new DimensionsWrapper(dimensions); DimensionsWrapper wrapper = new DimensionsWrapper(dimensions);
writeWorldPresetSettings(wrapper); writeWorldPresetSettings(wrapper);
@ -129,7 +153,10 @@ public class TogetherWorldPreset extends WorldPreset {
public static Registry<LevelStem> getDimensions(ResourceKey<WorldPreset> key) { public static Registry<LevelStem> getDimensions(ResourceKey<WorldPreset> key) {
RegistryAccess access = WorldBootstrap.getLastRegistryAccessOrElseBuiltin(); RegistryAccess access = WorldBootstrap.getLastRegistryAccessOrElseBuiltin();
var preset = access.registryOrThrow(Registry.WORLD_PRESET_REGISTRY).getHolder(key); var preset = (access == null
? BuiltinRegistries.WORLD_PRESET
: access.registryOrThrow(Registry.WORLD_PRESET_REGISTRY))
.getHolder(key);
if (preset.isEmpty()) return null; if (preset.isEmpty()) return null;
return preset return preset
.get() .get()
@ -144,6 +171,15 @@ public class TogetherWorldPreset extends WorldPreset {
return DimensionsWrapper.build(reg); return DimensionsWrapper.build(reg);
} }
public static @NotNull Map<ResourceKey<LevelStem>, ChunkGenerator> getDimensionMap(WorldDimensions worldDims) {
return DimensionsWrapper.build(worldDims.dimensions());
}
public static @NotNull WorldDimensions getWorldDimensions(ResourceKey<WorldPreset> key) {
Registry<LevelStem> reg = getDimensions(key);
return new WorldDimensions(reg);
}
private static class DimensionsWrapper { private static class DimensionsWrapper {
public static final Codec<DimensionsWrapper> CODEC = RecordCodecBuilder.create(instance -> instance public static final Codec<DimensionsWrapper> CODEC = RecordCodecBuilder.create(instance -> instance
.group(Codec.unboundedMap( .group(Codec.unboundedMap(