From 1f9213041b94ff9c3bbc1da9079f75cb91bcf3d8 Mon Sep 17 00:00:00 2001 From: Ruling-0 <47913168+Ruling-0@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:30:53 -0600 Subject: [PATCH] Keep the singleplayer host's saved player when its read fails --- .../postea/mixins/PosteaLoadingPlugin.java | 3 ++ .../early/MixinIntegratedPlayerList.java | 46 +++++++++++++++++++ .../utility/PlayerDataFixerUtility.java | 30 ++++-------- 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/postea/mixins/early/MixinIntegratedPlayerList.java diff --git a/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java b/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java index 0d24d34..fdf6729 100644 --- a/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java +++ b/src/main/java/com/gtnewhorizons/postea/mixins/PosteaLoadingPlugin.java @@ -7,6 +7,7 @@ import java.util.Set; import com.gtnewhorizon.gtnhmixins.IEarlyMixinLoader; +import cpw.mods.fml.relauncher.FMLLaunchHandler; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; @IFMLLoadingPlugin.MCVersion("1.7.10") @@ -50,6 +51,8 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading mixins.add("MixinAnvilChunkLoader"); mixins.add("MixinNBTTagList"); mixins.add("MixinSaveHandler"); + if (FMLLaunchHandler.side() + .isClient()) mixins.add("MixinIntegratedPlayerList"); return mixins; } } diff --git a/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinIntegratedPlayerList.java b/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinIntegratedPlayerList.java new file mode 100644 index 0000000..1ef117a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/postea/mixins/early/MixinIntegratedPlayerList.java @@ -0,0 +1,46 @@ +package com.gtnewhorizons.postea.mixins.early; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.integrated.IntegratedPlayerList; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.DimensionManager; + +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.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +/// Keeps the singleplayer host's saved player in `level.dat` when the host's login fails before the player is read. +/// +/// A host whose read fails is logged out with a player that was never loaded. `IntegratedPlayerList` records host data +/// before the `playerNetServerHandler` check in `ServerConfigurationManager.writePlayerData`, and `level.dat` is saved +/// without a player while no host data exists. +@Mixin(IntegratedPlayerList.class) +public abstract class MixinIntegratedPlayerList { + + /// Skips a player that never finished logging in, as the superclass does. + @Inject( + method = "writePlayerData(Lnet/minecraft/entity/player/EntityPlayerMP;)V", + at = @At("HEAD"), + cancellable = true, + require = 1) + private void postea$skipUnloadedPlayer(EntityPlayerMP player, CallbackInfo ci) { + if (player.playerNetServerHandler == null) ci.cancel(); + } + + /// Returns the player read from `level.dat` while no host data has been written. + @Inject( + method = "getHostPlayerData()Lnet/minecraft/nbt/NBTTagCompound;", + at = @At("RETURN"), + cancellable = true, + require = 1) + private void postea$keepReadHostPlayer(CallbackInfoReturnable cir) { + if (cir.getReturnValue() != null) return; + WorldServer overworld = DimensionManager.getWorld(0); + if (overworld != null) cir.setReturnValue( + overworld.getWorldInfo() + .getPlayerNBTTagCompound()); + } +} diff --git a/src/main/java/com/gtnewhorizons/postea/utility/PlayerDataFixerUtility.java b/src/main/java/com/gtnewhorizons/postea/utility/PlayerDataFixerUtility.java index 293c892..6e20fe1 100644 --- a/src/main/java/com/gtnewhorizons/postea/utility/PlayerDataFixerUtility.java +++ b/src/main/java/com/gtnewhorizons/postea/utility/PlayerDataFixerUtility.java @@ -3,7 +3,6 @@ package com.gtnewhorizons.postea.utility; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; -import com.gtnewhorizons.postea.api.IVersionedTransformer; import com.gtnewhorizons.postea.api.PlayerDataTransformContext; import com.gtnewhorizons.postea.api.VersionedReplacementManager; @@ -14,27 +13,16 @@ public final class PlayerDataFixerUtility { private PlayerDataFixerUtility() {} - /// Transforms `tag` in place and returns a copy of the stamps it carried, or null if unstamped. + /// Transforms `tag` in place and returns a copy of the stamps it carried, or null if unstamped. A failing + /// transformer throws and leaves `tag` as read. public static NBTTagCompound onPlayerRead(EntityPlayerMP player, NBTTagCompound tag) { - NBTTagCompound stamps = VersionStamps.read(tag); - for (IVersionedTransformer transformer : VersionedReplacementManager.transformers()) { - int current = transformer.currentVersion(); - int stored = VersionStamps.stored(stamps, transformer.key()); - if (stored == current) continue; - VersionedTransformerLog.announce(transformer, stored, current); - try { - transformer.transformPlayer(new PlayerDataTransformContext(player, tag, stored)); - } catch (Throwable t) { - throw VersionedPass.failure( - t, - transformer, - stored, - current, - "Player", - player.getCommandSenderName() + " (" + player.getUniqueID() + ")"); - } - } - return stamps == null ? null : (NBTTagCompound) stamps.copy(); + return VersionedPass.runAtomically( + tag, + VersionedReplacementManager.transformers(), + "Player", + player.getCommandSenderName() + " (" + player.getUniqueID() + ")", + (transformer, data, stored) -> transformer + .transformPlayer(new PlayerDataTransformContext(player, data, stored))); } public static void onPlayerWrite(NBTTagCompound tag, NBTTagCompound carried) {