From 0e369bf99fac8f50f78bbb4cdce2ee4cd26bf88e Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sat, 31 Oct 2020 17:56:13 +0100 Subject: [PATCH] Migrating advancements (#23) --- .../mixin/MixinPlayerAdvancementTracker.java | 47 +++++++++++++++++++ .../simpleauth/mixin/MixinPlayerManager.java | 1 - .../simpleauth/storage/AuthConfig.java | 15 ++++++ src/main/resources/mixins.simpleauth.json | 1 + 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.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 new file mode 100644 index 0000000..d2076ba --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerAdvancementTracker.java @@ -0,0 +1,47 @@ +package org.samo_lego.simpleauth.mixin; + +import net.minecraft.advancement.PlayerAdvancementTracker; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.ServerAdvancementLoader; +import net.minecraft.server.network.ServerPlayerEntity; +import org.samo_lego.simpleauth.utils.PlayerAuth; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +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.callback.CallbackInfo; + +import java.io.File; + +import static org.samo_lego.simpleauth.SimpleAuth.config; + +@Mixin(PlayerAdvancementTracker.class) +public class MixinPlayerAdvancementTracker { + + @Mutable + @Shadow + @Final + private File advancementFile; + + @Shadow + private ServerPlayerEntity owner; + + @Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("HEAD")) + private void startMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) { + System.out.println(this.advancementFile.isFile()); + if(config.experimental.premiumAutologin && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.advancementFile.isFile()) { + // Migrate + String playername = owner.getGameProfile().getName().toLowerCase(); + 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()) { + 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 d0ae290..1e2788e 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java @@ -30,7 +30,6 @@ public abstract class MixinPlayerManager { PlayerLeaveServerCallback.EVENT.invoker().onPlayerLeave(serverPlayerEntity); } - // Method for kicking player for @Inject(method = "checkCanJoin(Ljava/net/SocketAddress;Lcom/mojang/authlib/GameProfile;)Lnet/minecraft/text/Text;", at = @At("HEAD"), cancellable = true) private void checkCanJoin(SocketAddress socketAddress, GameProfile profile, CallbackInfoReturnable cir) { // Getting the player that is trying to join the server diff --git a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java index 34b5647..30064ad 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -192,8 +192,23 @@ public class AuthConfig { * Whether players who have a valid session should skip the authentication process. * You have to set online-mode to true in server.properties! * (cracked players will still be able to enter, but they'll need to login) + * + * This protects premium usernames from being stolen, since cracked players + * with name that is found in Mojang database, are kicked. */ public boolean premiumAutologin = false; + /** + * Whether to modify player uuids to offline style. + * Note: this should be used only if you had your server + * running in offline mode and you made the switch to use + * AuthConfig#premiumAutoLogin AND your players already + * have e.g. villager discounts, which are based on uuid. + * Other things (advancements, playerdata) are migrated + * automatically, so think before enabling this. In case + * an online-mode player changes username, they'll loose all + * their stuff, unless you migrate it manually. + */ + public boolean forceoOfflineUuids = false; } public MainConfig main = new MainConfig(); diff --git a/src/main/resources/mixins.simpleauth.json b/src/main/resources/mixins.simpleauth.json index d1b281d..b9b884e 100644 --- a/src/main/resources/mixins.simpleauth.json +++ b/src/main/resources/mixins.simpleauth.json @@ -4,6 +4,7 @@ "compatibilityLevel": "JAVA_8", "mixins": [], "server": [ + "MixinPlayerAdvancementTracker", "MixinPlayerEntity", "MixinPlayerManager", "MixinServerLoginNetworkHandler",