From 7af0b24e3e4c1b38d1a813dcc6a764257e3642e1 Mon Sep 17 00:00:00 2001 From: Ruling-0 <47913168+Ruling-0@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:25:42 -0600 Subject: [PATCH] Open the custom data mapping gate once the world's info has loaded --- .../api/CustomDataReplacementManager.java | 4 +-- .../postea/mixins/PosteaLoadingPlugin.java | 1 + .../postea/mixins/early/MixinSaveHandler.java | 26 ++++++++++++++ .../postea/utility/IDRegistry.java | 13 ++++--- src/main/resources/mixins.postea.early.json | 3 +- .../postea/utility/IDRegistryTest.java | 34 +++++++++++++++++++ 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/postea/mixins/early/MixinSaveHandler.java create mode 100644 src/test/java/com/gtnewhorizons/postea/utility/IDRegistryTest.java diff --git a/src/main/java/com/gtnewhorizons/postea/api/CustomDataReplacementManager.java b/src/main/java/com/gtnewhorizons/postea/api/CustomDataReplacementManager.java index 95d4a5e..da8a1dc 100644 --- a/src/main/java/com/gtnewhorizons/postea/api/CustomDataReplacementManager.java +++ b/src/main/java/com/gtnewhorizons/postea/api/CustomDataReplacementManager.java @@ -23,13 +23,13 @@ public abstract class CustomDataReplacementManager { /// Transforms `root` in place through every registered transformer whose stamp differs from its current /// version, then writes the updated stamps onto `root`. Throws [IllegalStateException] when called before the - /// world's saved id mappings were applied. + /// world's info, and with it any saved id map, has loaded. public static void transform(String storageId, NBTTagCompound root) { if (!serverRunning()) return; if (!IDRegistry.isMappingApplied()) { throw new IllegalStateException( "Custom data " + storageId - + " was offered for transformation before the world's id mappings were applied"); + + " was offered for transformation before the world's info and id mappings loaded"); } transform(storageId, root, VersionedReplacementManager.transformers()); } diff --git a/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java b/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java index 19adeb8..cd54f51 100644 --- a/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java +++ b/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java @@ -49,6 +49,7 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading mixins.add("MixinEntityPlayer"); mixins.add("MixinAnvilChunkLoader"); mixins.add("MixinNBTTagList"); + mixins.add("MixinSaveHandler"); return mixins; } } diff --git a/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinSaveHandler.java b/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinSaveHandler.java new file mode 100644 index 0000000..63e1e2f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinSaveHandler.java @@ -0,0 +1,26 @@ +package com.gtnewhorizons.postea.mixins.early; + +import net.minecraft.world.storage.SaveHandler; +import net.minecraft.world.storage.WorldInfo; + +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 com.gtnewhorizons.postea.utility.IDRegistry; + +import cpw.mods.fml.common.FMLCommonHandler; + +@Mixin(SaveHandler.class) +public abstract class MixinSaveHandler { + + /// Returns come after FML's `handleWorldDataLoad` applies any saved id map, or when the world has no readable + /// `level.dat`. FML applies the map only on the server thread. + @Inject(method = "loadWorldInfo", at = @At("RETURN"), require = 1) + private void postea$markWorldInfoLoaded(CallbackInfoReturnable cir) { + if (FMLCommonHandler.instance() + .getEffectiveSide() + .isServer()) IDRegistry.onWorldInfoLoaded(); + } +} diff --git a/src/main/java/com/gtnewhorizons/postea/utility/IDRegistry.java b/src/main/java/com/gtnewhorizons/postea/utility/IDRegistry.java index 7471140..c8538e0 100644 --- a/src/main/java/com/gtnewhorizons/postea/utility/IDRegistry.java +++ b/src/main/java/com/gtnewhorizons/postea/utility/IDRegistry.java @@ -41,12 +41,11 @@ public abstract class IDRegistry { private static volatile boolean mappingApplied; /// Loads the retired id table of the world about to load. [#registerWorldSpecificIDs] records into it until - /// [#endWorld()]. + /// [#endWorld()]. [#isMappingApplied] stays false until [#onWorldInfoLoaded]. public static void beginWorld(File worldDirectory) { tableFile = new File(new File(worldDirectory, Postea.MODID), WorldIdTable.FILE_NAME); table = WorldIdTable.load(tableFile); - // No level.dat means no saved id map, and FML fires no remap event. - mappingApplied = !new File(worldDirectory, "level.dat").exists(); + mappingApplied = false; } public static void endWorld() { @@ -58,11 +57,17 @@ public abstract class IDRegistry { ITEM_NAME_TO_ID.clear(); } - /// Whether FML has applied the loading world's saved id map. + /// Whether the loading world's info has loaded on the server thread. FML applies the world's saved id map, if + /// it has one, during that load. public static boolean isMappingApplied() { return mappingApplied; } + /// Marks the loading world's info as loaded. Ignored outside a world begun by [#beginWorld]. + public static void onWorldInfoLoaded() { + if (tableFile != null) mappingApplied = true; + } + /** * Triggers all the callbacks registered by other mods that wish to identify a block that may no longer exist. * These callbacks effectively remove the need for dummies, and should help improve performance at runtime. diff --git a/src/main/resources/mixins.postea.early.json b/src/main/resources/mixins.postea.early.json index 669904f..dee5294 100644 --- a/src/main/resources/mixins.postea.early.json +++ b/src/main/resources/mixins.postea.early.json @@ -11,7 +11,8 @@ "MixinEntity", "MixinEntityPlayer", "MixinItemStack", - "MixinNBTTagList" + "MixinNBTTagList", + "MixinSaveHandler" ], "client": [], "server": [] diff --git a/src/test/java/com/gtnewhorizons/postea/utility/IDRegistryTest.java b/src/test/java/com/gtnewhorizons/postea/utility/IDRegistryTest.java new file mode 100644 index 0000000..2f70f7c --- /dev/null +++ b/src/test/java/com/gtnewhorizons/postea/utility/IDRegistryTest.java @@ -0,0 +1,34 @@ +package com.gtnewhorizons.postea.utility; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class IDRegistryTest { + + @TempDir + File world; + + @AfterEach + void endWorld() { + IDRegistry.endWorld(); + } + + @Test + void theMappingGateOpensOnlyWhenTheBegunWorldsInfoLoads() { + IDRegistry.beginWorld(world); + assertFalse(IDRegistry.isMappingApplied()); + + IDRegistry.onWorldInfoLoaded(); + assertTrue(IDRegistry.isMappingApplied()); + + IDRegistry.endWorld(); + IDRegistry.onWorldInfoLoaded(); + assertFalse(IDRegistry.isMappingApplied()); + } +}