Skip to content

Commit

Permalink
Changed behavior of premiumAutologin
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Jul 24, 2023
1 parent f6ec6ba commit 0dc8a73
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
1) Add option to skip all authentication if it's already done by another thing (mod/proxy/etc):
- `skipAllAuthChecks`
2) Player will be re-mounted on entity if they were dismounted on login with `spawnOnJoin` enabled
3) Changed behavior of `premiumAutologin`. Now it only allows online players not to authorize when logging in

----
### 3.0.18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;
import xyz.nikitacartes.easyauth.utils.TranslationHelper;

import static net.minecraft.server.command.CommandManager.literal;
import static xyz.nikitacartes.easyauth.EasyAuth.mojangAccountNamesCache;

public class LogoutCommand {

Expand All @@ -25,12 +23,13 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException {
ServerPlayerEntity player = serverCommandSource.getPlayerOrThrow();

if (!mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase())) {
if (((PlayerAuth) player).isAuthenticated()) {
// player.getServer().getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.REMOVE_PLAYER, player));
((PlayerAuth) player).setAuthenticated(false);
player.sendMessage(TranslationHelper.getSuccessfulLogout(), false);
} else
} else {
player.sendMessage(TranslationHelper.getCannotLogout(), false);
}
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static xyz.nikitacartes.easyauth.EasyAuth.config;
import static xyz.nikitacartes.easyauth.EasyAuth.serverProp;

import java.nio.file.Path;

Expand All @@ -30,7 +31,7 @@ public class PlayerAdvancementTrackerMixin {

@Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("HEAD"))
private void startMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) {
if (config.main.premiumAutologin && !config.experimental.forcedOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.filePath.toFile().isFile()) {
if (Boolean.parseBoolean(serverProp.getProperty("online-mode")) && !config.experimental.forcedOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.filePath.toFile().isFile()) {
// Migrate
String playername = owner.getGameProfile().getName();
this.filePath = this.filePath.getParent().resolve(Uuids.getOfflinePlayerUuid(playername) + ".json");
Expand All @@ -39,7 +40,7 @@ private void startMigratingOfflineAdvancements(ServerAdvancementLoader advanceme

@Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("TAIL"))
private void endMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) {
if (config.main.premiumAutologin && !config.experimental.forcedOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount()) {
if (Boolean.parseBoolean(serverProp.getProperty("online-mode")) && !config.experimental.forcedOfflineUuids && ((PlayerAuth) this.owner).isUsingMojangAccount()) {
// Changes the file name to use online UUID
this.filePath = this.filePath.getParent().resolve(owner.getUuid() + ".json");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package xyz.nikitacartes.easyauth.mixin;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import org.objectweb.asm.Opcodes;
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.*;
import org.spongepowered.asm.mixin.injection.*;
import xyz.nikitacartes.easyauth.storage.PlayerCache;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;

import java.util.*;
Expand All @@ -23,9 +18,9 @@ public class PlayerListS2CPacketMixin {
@Shadow
private List<PlayerListS2CPacket.Entry> entries;

@Unique
private static boolean hideFromTabList(ServerPlayerEntity player) {
return !(PlayerCache.isAuthenticated(((PlayerAuth) player).getFakeUuid()) ||
(((PlayerAuth) player).isUsingMojangAccount() && config.main.premiumAutologin));
return ((PlayerAuth) player).isAuthenticated();
}
@ModifyVariable(
method = "<init>(Ljava/util/EnumSet;Ljava/util/Collection;)V",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
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.Text;
import net.minecraft.util.Uuids;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nikitacartes.easyauth.event.AuthEventHandler;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -26,6 +29,10 @@
@Mixin(PlayerManager.class)
public abstract class PlayerManagerMixin {

@Final
@Shadow
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) {
AuthEventHandler.onPlayerJoin(serverPlayerEntity);
Expand Down Expand Up @@ -59,7 +66,7 @@ private void checkCanJoin(SocketAddress socketAddress, GameProfile profile, Call
)
private void migrateOfflineStats(PlayerEntity player, CallbackInfoReturnable<ServerStatHandler> cir, UUID uUID, ServerStatHandler serverStatHandler, File serverStatsDir, File playerStatFile) {
File onlineFile = new File(serverStatsDir, uUID + ".json");
if (config.main.premiumAutologin && !config.experimental.forcedOfflineUuids && ((PlayerAuth) player).isUsingMojangAccount() && !onlineFile.exists()) {
if (server.isOnlineMode() && !config.experimental.forcedOfflineUuids && ((PlayerAuth) player).isUsingMojangAccount() && !onlineFile.exists()) {
String playername = player.getGameProfile().getName();
File offlineFile = new File(onlineFile.getParent(), Uuids.getOfflinePlayerUuid(playername) + ".json");
offlineFile.renameTo(onlineFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.mojang.authlib.GameProfile;
import net.minecraft.network.packet.c2s.login.LoginHelloC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.util.Uuids;
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;
Expand Down Expand Up @@ -33,6 +35,10 @@ public abstract class ServerLoginNetworkHandlerMixin {
@Shadow
ServerLoginNetworkHandler.State state;

@Final
@Shadow
MinecraftServer server;

@Inject(method = "acceptPlayer()V", at = @At("HEAD"))
private void acceptPlayer(CallbackInfo ci) {
if (config.experimental.forcedOfflineUuids) {
Expand All @@ -59,7 +65,7 @@ private void acceptPlayer(CallbackInfo ci) {
cancellable = true
)
private void checkPremium(LoginHelloC2SPacket packet, CallbackInfo ci) {
if (config.main.premiumAutologin) {
if (server.isOnlineMode()) {
try {
String playername = packet.name().toLowerCase();
Pattern pattern = Pattern.compile("^[a-z0-9_]{3,16}$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void hidePosition(boolean hide) {
cache.lastLocation.yaw = player.getYaw();
cache.lastLocation.pitch = player.getPitch();
cache.ridingEntityUUID = player.getVehicle() != null ? player.getVehicle().getUuid() : null;
LogDebug(String.format("Saving position of player %s", cache.lastLocation));
LogDebug(String.format("Saving vehicle of player %s", cache.ridingEntityUUID));

// Teleports player to spawn
player.teleport(
Expand All @@ -75,8 +77,10 @@ public void hidePosition(boolean hide) {
cache.lastLocation.yaw,
cache.lastLocation.pitch
);
LogDebug(String.format("Teleported player to %s", cache.lastLocation));
// Mount player to vehicle if it exists
if (cache.ridingEntityUUID != null) {
LogDebug(String.format("Mounting player to vehicle %s", cache.ridingEntityUUID));
ServerWorld world = server.getWorld(cache.lastLocation.dimension.getRegistryKey());
if (world == null) return;
Entity entity = world.getEntity(cache.ridingEntityUUID);
Expand Down Expand Up @@ -157,7 +161,7 @@ public boolean canSkipAuth() {
*/
@Override
public boolean isUsingMojangAccount() {
return mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase());
return server.isOnlineMode() && mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import java.io.FileInputStream;
import java.io.IOException;

import static xyz.nikitacartes.easyauth.EasyAuth.config;
import static xyz.nikitacartes.easyauth.EasyAuth.mojangAccountNamesCache;
import static xyz.nikitacartes.easyauth.EasyAuth.*;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogDebug;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogWarn;

Expand Down Expand Up @@ -71,7 +70,7 @@ private void fileExists(PlayerEntity playerEntity, CallbackInfoReturnable<NbtCom
private NbtCompound migratePlayerData(NbtCompound compoundTag, PlayerEntity player) {
// Checking for offline player data only if online doesn't exist yet
String playername = player.getGameProfile().getName().toLowerCase();
if (config.main.premiumAutologin && mojangAccountNamesCache.contains(playername) && !this.fileExists) {
if (Boolean.parseBoolean(serverProp.getProperty("online-mode")) && mojangAccountNamesCache.contains(playername) && !this.fileExists) {
LogDebug(String.format("Migrating data for %s", playername));
File file = new File(this.playerDataDir, Uuids.getOfflinePlayerUuid(player.getGameProfile().getName()) + ".dat");
if (file.exists() && file.isFile())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ public static AuthConfig load(File file) {
config.experimental.forcedOfflineUuids = false;
}
if (config.main.premiumAutologin) {
LogWarn("You cannot use server in offline mode and premiumAutologin! Disabling the latter.");
config.main.premiumAutologin = false;
}
} else {
if (!config.main.premiumAutologin) {
LogWarn("With online-mode enabled and premiumAutoLogin disabled, offline players will not be able to join.");
LogInfo("You have premiumAutologin enabled. Enable online-mode so that online player can not register.");
}
}
if (config.experimental.enableServerSideTranslation && !FabricLoader.getInstance().isModLoaded("server_translations_api")) {
Expand Down

0 comments on commit 0dc8a73

Please sign in to comment.