Keep the singleplayer host's saved player when its read fails

This commit is contained in:
Ruling-0 committed 2026-11-25 14:45:59 -06:00
1 parent 93c77c7e5f
commit 1f9213041b
3 files changed
+58 -21

No files matched your search

@@ -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;
}
}
@@ -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<NBTTagCompound> cir) {
if (cir.getReturnValue() != null) return;
WorldServer overworld = DimensionManager.getWorld(0);
if (overworld != null) cir.setReturnValue(
overworld.getWorldInfo()
.getPlayerNBTTagCompound());
}
}
@@ -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) {