From 1582b2dcacad0bc885d5ea07a913357435658157 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sat, 31 Oct 2020 20:44:03 +0100 Subject: [PATCH] Migrating stats --- .../mixin/MixinPlayerAdvancementTracker.java | 6 +-- .../simpleauth/mixin/MixinPlayerManager.java | 49 +++++++++++++++++++ .../mixin/ServerStatHandlerAccessor.java | 17 +++++++ src/main/resources/mixins.simpleauth.json | 3 +- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/ServerStatHandlerAccessor.java diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.java index ba4db49..61a7581 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.java @@ -30,16 +30,16 @@ public class MixinPlayerAdvancementTracker { @Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("HEAD")) private void startMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) { - if(config.experimental.premiumAutologin && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.advancementFile.isFile()) { + if(config.experimental.premiumAutologin && !config.experimental.forceoOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.advancementFile.isFile()) { // Migrate - String playername = owner.getGameProfile().getName().toLowerCase(); + String playername = owner.getGameProfile().getName(); this.advancementFile = new File(this.advancementFile.getParent(), PlayerEntity.getOfflinePlayerUuid(playername).toString() + ".json"); } } @Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("TAIL")) private void endMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) { - if(config.experimental.premiumAutologin && ((PlayerAuth) this.owner).isUsingMojangAccount()) { + if(config.experimental.premiumAutologin && !config.experimental.forceoOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount()) { this.advancementFile = new File(this.advancementFile.getParent(), owner.getUuid() + ".json"); } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java index 1e2788e..a101c7d 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java @@ -1,25 +1,39 @@ package org.samo_lego.simpleauth.mixin; import com.mojang.authlib.GameProfile; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.ClientConnection; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.stat.ServerStatHandler; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; import org.samo_lego.simpleauth.event.entity.player.PrePlayerJoinCallback; +import org.samo_lego.simpleauth.utils.PlayerAuth; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; 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.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; +import java.io.File; import java.net.SocketAddress; +import java.util.UUID; + +import static org.samo_lego.simpleauth.SimpleAuth.config; @Mixin(PlayerManager.class) public abstract class MixinPlayerManager { + @Shadow @Final private MinecraftServer server; + @Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At("RETURN")) private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEntity serverPlayerEntity, CallbackInfo ci) { PlayerJoinServerCallback.EVENT.invoker().onPlayerJoin(serverPlayerEntity); @@ -42,4 +56,39 @@ public abstract class MixinPlayerManager { cir.setReturnValue(returnText); } } + + @ModifyVariable( + method = "createStatHandler(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/stat/ServerStatHandler;", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/player/PlayerEntity;getName()Lnet/minecraft/text/Text;" + ), + ordinal = 1 + ) + private File migrateOfflineStats(File file, PlayerEntity player) { + if(config.experimental.premiumAutologin && !config.experimental.forceoOfflineUuids && ((PlayerAuth) player).isUsingMojangAccount()) { + String playername = player.getGameProfile().getName(); + file = new File(file.getParent(), PlayerEntity.getOfflinePlayerUuid(playername) + ".json"); + } + return file; + } + + @Inject( + method = "createStatHandler(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/stat/ServerStatHandler;", + at = @At( + value = "INVOKE", + target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;" + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void migrateOfflineStats(PlayerEntity player, CallbackInfoReturnable cir, UUID uUID, ServerStatHandler serverStatHandler, File serverStatsDir, File playerStatFile) { + File onlineFile = new File(serverStatsDir, uUID + ".json"); + if(config.experimental.premiumAutologin && !config.experimental.forceoOfflineUuids && ((PlayerAuth) player).isUsingMojangAccount() && !onlineFile.exists()) { + ((ServerStatHandlerAccessor) serverStatHandler).setFile(onlineFile); + System.out.println("File: " + ((ServerStatHandlerAccessor) serverStatHandler).getFile().getName()); + } + else + System.out.println("Nope: " + ((ServerStatHandlerAccessor) serverStatHandler).getFile().getName()); + + } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/ServerStatHandlerAccessor.java b/src/main/java/org/samo_lego/simpleauth/mixin/ServerStatHandlerAccessor.java new file mode 100644 index 0000000..f329057 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/mixin/ServerStatHandlerAccessor.java @@ -0,0 +1,17 @@ +package org.samo_lego.simpleauth.mixin; + +import net.minecraft.stat.ServerStatHandler; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import java.io.File; + +@Mixin(ServerStatHandler.class) +public interface ServerStatHandlerAccessor { + + @Accessor("file") + File getFile(); + + @Accessor("file") + void setFile(File file); +} diff --git a/src/main/resources/mixins.simpleauth.json b/src/main/resources/mixins.simpleauth.json index b9b884e..0713648 100644 --- a/src/main/resources/mixins.simpleauth.json +++ b/src/main/resources/mixins.simpleauth.json @@ -11,7 +11,8 @@ "MixinServerPlayerEntity", "MixinServerPlayNetworkHandler", "MixinSlot", - "MixinWorldSaveHandler" + "MixinWorldSaveHandler", + "ServerStatHandlerAccessor" ], "injectors": { "defaultRequire": 1