Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Auto-migrate player data (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Nov 9, 2020
1 parent 85b454b commit c15e830
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.10.1+build.209
fabric_version=0.24.0+build.411-1.16

# Mod Properties
mod_version = 1.6.2
mod_version = 1.6.3
maven_group = org.samo_lego
archives_base_name = simpleauth

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/samo_lego/simpleauth/SimpleAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -30,6 +31,7 @@ public class MixinServerLoginNetworkHandler {
/**
* Fake state of current player.
*/
@Unique
private boolean acceptCrackedPlayer = false;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/mixins.simpleauth.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"MixinPlayerManager",
"MixinServerLoginNetworkHandler",
"MixinServerPlayNetworkHandler",
"MixinSlot"
"MixinSlot",
"MixinWorldSaveHandler"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit c15e830

Please sign in to comment.