diff --git a/src/main/java/ru/bclib/api/LifeCycleAPI.java b/src/main/java/ru/bclib/api/LifeCycleAPI.java index c50b83b9..05ade7a5 100644 --- a/src/main/java/ru/bclib/api/LifeCycleAPI.java +++ b/src/main/java/ru/bclib/api/LifeCycleAPI.java @@ -23,6 +23,13 @@ import java.util.concurrent.Executor; public class LifeCycleAPI { private final static List onLoadLevelBiomes = new ArrayList<>(2); private final static List onLoadLevel = new ArrayList<>(2); + private final static List beforeLoadLevel = new ArrayList<>(2); + /** + * A callback function that is used for each new ServerLevel instance + */ + public interface BeforeLevelLoadCall { + void beforeLoad(); + } /** * A callback function that is used for each new ServerLevel instance @@ -51,6 +58,17 @@ public class LifeCycleAPI { boolean bl2); } + /** + * Register a callback that is called before a level is loaded or created, + * but after the {@link WorldDataAPI} was initialized and patches from + * the {@link ru.bclib.api.datafixer.DataFixerAPI} were applied. + * + * @param call The callback Method + */ + public static void beforeLevelLoad(BeforeLevelLoadCall call){ + beforeLoadLevel.add(call); + } + /** * Register a callback that is called when a new {@code ServerLevel is instantiated}. * This callback will receive the world seed as well as it's biome registry. @@ -69,6 +87,13 @@ public class LifeCycleAPI { onLoadLevel.add(call); } + /** + * For internal use, You should not call this method! + * @param src + */ + public static void _runBeforeLevelLoad(){ + beforeLoadLevel.forEach(c -> c.beforeLoad()); + } /** * For internal use, You should not call this method! * @param minecraftServer diff --git a/src/main/java/ru/bclib/mixin/client/MinecraftMixin.java b/src/main/java/ru/bclib/mixin/client/MinecraftMixin.java index f1cb2523..6c884bcf 100644 --- a/src/main/java/ru/bclib/mixin/client/MinecraftMixin.java +++ b/src/main/java/ru/bclib/mixin/client/MinecraftMixin.java @@ -22,6 +22,7 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import ru.bclib.api.LifeCycleAPI; import ru.bclib.api.dataexchange.DataExchangeAPI; import ru.bclib.api.datafixer.DataFixerAPI; import ru.bclib.interfaces.CustomColorProvider; @@ -61,14 +62,18 @@ public abstract class MinecraftMixin { DataExchangeAPI.prepareServerside(); if (DataFixerAPI.fixData(this.levelSource, levelID, true, (appliedFixes) -> { + LifeCycleAPI._runBeforeLevelLoad(); this.doLoadLevel(levelID, RegistryAccess.builtin(), Minecraft::loadDataPacks, Minecraft::loadWorldData, false, appliedFixes?ExperimentalDialogType.NONE:ExperimentalDialogType.BACKUP); })) { ci.cancel(); + } else { + LifeCycleAPI._runBeforeLevelLoad(); } } @Inject(method = "createLevel", at = @At("HEAD")) private void bclib_initPatchData(String levelID, LevelSettings levelSettings, RegistryHolder registryHolder, WorldGenSettings worldGenSettings, CallbackInfo ci) { DataFixerAPI.initializeWorldData(this.levelSource, levelID, true); + LifeCycleAPI._runBeforeLevelLoad(); } } diff --git a/src/main/java/ru/bclib/mixin/common/MainMixin.java b/src/main/java/ru/bclib/mixin/common/MainMixin.java index 72f17ecf..e672681d 100644 --- a/src/main/java/ru/bclib/mixin/common/MainMixin.java +++ b/src/main/java/ru/bclib/mixin/common/MainMixin.java @@ -1,10 +1,12 @@ package ru.bclib.mixin.common; import net.minecraft.server.Main; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.storage.LevelStorageSource; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyArg; +import ru.bclib.api.LifeCycleAPI; import ru.bclib.api.datafixer.DataFixerAPI; @Mixin(Main.class) @@ -12,6 +14,7 @@ abstract public class MainMixin { @ModifyArg(method="main", at=@At(value="INVOKE_ASSIGN", target="Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;getSummary()Lnet/minecraft/world/level/storage/LevelSummary;")) private static LevelStorageSource.LevelStorageAccess bclib_callServerFix(LevelStorageSource.LevelStorageAccess session){ DataFixerAPI.fixData(session, false, (didFix)->{/* not called when showUI==false */}); + LifeCycleAPI._runBeforeLevelLoad(); return session; } }