Auto-migrate player data (#23)

This commit is contained in:
samo_lego 2020-10-25 22:37:42 +01:00
parent d6d369c952
commit 4247fad3c0
5 changed files with 96 additions and 5 deletions

View File

@ -10,7 +10,7 @@ loader_version=0.9.3+build.207
fabric_version=0.20.2+build.402-1.16
# Mod Properties
mod_version = 1.6.2
mod_version = 1.6.3
maven_group = org.samo_lego
archives_base_name = simpleauth

View File

@ -53,15 +53,15 @@ public class SimpleAuth implements DedicatedServerModInitializer {
*/
public static HashSet<String> mojangAccountNamesCache = new HashSet<>();
public static HashSet<String> accountStatusCache = new HashSet<>();
// Getting game directory
public static final Path gameDirectory = FabricLoader.getInstance().getGameDir();
// Server properties
public static Properties serverProp = new Properties();
// Mod config
/**
* Config of the SimpleAuth mod.
*/
public static AuthConfig config;
@Override

View File

@ -7,6 +7,7 @@ import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.text.TranslatableText;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@ -30,6 +31,7 @@ public class MixinServerLoginNetworkHandler {
/**
* Fake state of current player.
*/
@Unique
private boolean acceptCrackedPlayer = false;
/**

View File

@ -0,0 +1,88 @@
package org.samo_lego.simpleauth.mixin;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.WorldSaveHandler;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import static org.samo_lego.simpleauth.SimpleAuth.config;
import static org.samo_lego.simpleauth.SimpleAuth.mojangAccountNamesCache;
@Mixin(WorldSaveHandler.class)
public class MixinWorldSaveHandler {
@Final
@Shadow
private File playerDataDir;
@Unique
private boolean fileExists;
@Final
@Shadow
private static Logger LOGGER;
/**
* Saves whether player save file exists.
*
* @param playerEntity
* @param cir
* @param compoundTag
* @param file
*/
@Inject(
method = "loadPlayerData(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/nbt/CompoundTag;",
at = @At(
value = "INVOKE",
target = "Ljava/io/File;exists()Z"
),
locals = LocalCapture.CAPTURE_FAILHARD
)
private void fileExists(PlayerEntity playerEntity, CallbackInfoReturnable<CompoundTag> cir, CompoundTag compoundTag, File file) {
// @ModifyVariable cannot capture locals
this.fileExists = file.exists();
}
/**
* Loads offline-uuid player data to compoundTag in order to migrate from offline to online.
*
* @param compoundTag null compound tag.
* @param player player who might need migration of datd.
* @return compoundTag containing migrated data.
*/
@ModifyVariable(
method = "loadPlayerData(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/nbt/CompoundTag;",
at = @At(
value = "INVOKE",
target = "Ljava/io/File;exists()Z"
)
)
private CompoundTag migratePlayerData(CompoundTag compoundTag, PlayerEntity player) {
// Checking for offline player data only if online doesn't exist yet
if(config.experimental.premiumAutologin && mojangAccountNamesCache.contains(player.getGameProfile().getName()) && !this.fileExists) {
File file = new File(this.playerDataDir, PlayerEntity.getOfflinePlayerUuid(player.getGameProfile().getName()) + ".dat");
if (file.exists() && file.isFile())
try {
compoundTag = NbtIo.readCompressed(new FileInputStream(file));
}
catch (IOException e) {
LOGGER.warn("Failed to load player data for {}", player.getGameProfile().getName());
}
}
return compoundTag;
}
}

View File

@ -8,7 +8,8 @@
"MixinPlayerManager",
"MixinServerLoginNetworkHandler",
"MixinServerPlayNetworkHandler",
"MixinSlot"
"MixinSlot",
"MixinWorldSaveHandler"
],
"injectors": {
"defaultRequire": 1