diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index b0bb451..004a859 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -146,10 +146,10 @@ public class SimpleAuth implements DedicatedServerModInitializer { data.addProperty("password", playerCache.password); JsonObject lastLocation = new JsonObject(); - lastLocation.addProperty("dim", playerCache.lastDim); - lastLocation.addProperty("x", playerCache.lastX); - lastLocation.addProperty("y", playerCache.lastY); - lastLocation.addProperty("z", playerCache.lastZ); + lastLocation.addProperty("dim", playerCache.lastLocation.lastDim); + lastLocation.addProperty("x", playerCache.lastLocation.lastX); + lastLocation.addProperty("y", playerCache.lastLocation.lastY); + lastLocation.addProperty("z", playerCache.lastLocation.lastZ); data.addProperty("lastLocation", lastLocation.toString()); @@ -248,8 +248,13 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Marking player as not authenticated String uuid = convertUuid(player); - playerCacheMap.put(uuid, new PlayerCache(uuid, player)); - playerCacheMap.get(uuid).isAuthenticated = false; + PlayerCache cache = playerCacheMap.get(uuid); + if(cache == null) { + cache = new PlayerCache(uuid, player); + playerCacheMap.put(uuid, cache); + } + cache.isAuthenticated = false; + // Teleporting player to spawn to hide its position if(config.main.spawnOnJoin) @@ -303,8 +308,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { config.worldSpawn.x, config.worldSpawn.y, config.worldSpawn.z, - 90, - 90 + config.worldSpawn.yaw, + config.worldSpawn.pitch ); return; } @@ -312,22 +317,22 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Puts player to last cached position try { player.teleport( - server.getWorld(RegistryKey.of(Registry.DIMENSION, new Identifier(cache.lastDim))), - cache.lastX, - cache.lastY, - cache.lastZ, - 0, - 0 + server.getWorld(RegistryKey.of(Registry.DIMENSION, new Identifier(cache.lastLocation.lastDim))), + cache.lastLocation.lastX, + cache.lastLocation.lastY, + cache.lastLocation.lastZ, + cache.lastLocation.lastYaw, + cache.lastLocation.lastPitch ); } catch (Error e) { player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false); logError("Couldn't teleport player " + player.getName().asString()); logError( String.format("Last recorded position is X: %s, Y: %s, Z: %s in dimension %s", - cache.lastX, - cache.lastY, - cache.lastZ, - cache.lastDim + cache.lastLocation.lastX, + cache.lastLocation.lastY, + cache.lastLocation.lastZ, + cache.lastLocation.lastDim )); } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java index 7667984..0a9bf53 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.commands; import com.mojang.brigadier.CommandDispatcher; import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.DimensionArgumentType; +import net.minecraft.command.argument.RotationArgumentType; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.command.ServerCommandSource; @@ -48,17 +49,23 @@ public class AuthCommand { ctx.getSource().getEntityOrThrow().getEntityWorld().getRegistryKey().getValue(), ctx.getSource().getEntityOrThrow().getX(), ctx.getSource().getEntityOrThrow().getY(), - ctx.getSource().getEntityOrThrow().getZ() + ctx.getSource().getEntityOrThrow().getZ(), + ctx.getSource().getEntityOrThrow().yaw, + ctx.getSource().getEntityOrThrow().pitch )) .then(argument("dimension", DimensionArgumentType.dimension()) .then(argument("position", BlockPosArgumentType.blockPos()) - .executes(ctx -> setSpawn( - ctx.getSource(), - DimensionArgumentType.getDimensionArgument(ctx, "dimension").getRegistryKey().getValue(), - BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(), - // +1 to not spawn player in ground - BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getY() + 1, - BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getZ() + .then(argument("angle", RotationArgumentType.rotation()) + .executes(ctx -> setSpawn( + ctx.getSource(), + DimensionArgumentType.getDimensionArgument(ctx, "dimension").getRegistryKey().getValue(), + BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(), + // +1 to not spawn player in ground + BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getY() + 1, + BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getZ(), + RotationArgumentType.getRotation(ctx, "angle").toAbsoluteRotation(ctx.getSource()).y, + RotationArgumentType.getRotation(ctx, "angle").toAbsoluteRotation(ctx.getSource()).x + ) ) ) ) @@ -146,14 +153,18 @@ public class AuthCommand { * @param x x coordinate of the global spawn * @param y y coordinate of the global spawn * @param z z coordinate of the global spawn + * @param yaw player yaw (y rotation) + * @param pitch player pitch (x rotation) * @return 0 */ - private static int setSpawn(ServerCommandSource source, Identifier world, double x, double y, double z) { + private static int setSpawn(ServerCommandSource source, Identifier world, double x, double y, double z, float yaw, float pitch) { // Setting config values and saving config.worldSpawn.dimension = String.valueOf(world); config.worldSpawn.x = x; config.worldSpawn.y = y; config.worldSpawn.z = z; + config.worldSpawn.yaw = yaw; + config.worldSpawn.pitch = pitch; config.main.spawnOnJoin = true; config.save(new File("./mods/SimpleAuth/config.json")); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java index 18eafbd..1ffd8ee 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java @@ -5,10 +5,11 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import org.samo_lego.simpleauth.storage.PlayerCache; import static net.minecraft.server.command.CommandManager.literal; -import static org.samo_lego.simpleauth.SimpleAuth.config; -import static org.samo_lego.simpleauth.SimpleAuth.deauthenticatePlayer; +import static org.samo_lego.simpleauth.SimpleAuth.*; +import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; public class LogoutCommand { @@ -21,6 +22,14 @@ public class LogoutCommand { private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException { ServerPlayerEntity player = serverCommandSource.getPlayer(); + PlayerCache playerCache = playerCacheMap.get(convertUuid(player)); + playerCache.lastLocation.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); + playerCache.lastLocation.lastX = player.getX(); + playerCache.lastLocation.lastY = player.getY(); + playerCache.lastLocation.lastZ = player.getZ(); + playerCache.lastLocation.lastYaw = player.yaw; + playerCache.lastLocation.lastPitch = player.pitch; + deauthenticatePlayer(player); player.sendMessage(new LiteralText(config.lang.successfulLogout), false); return 1; diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index d42395c..d02ecdf 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -122,10 +122,12 @@ public class AuthEventHandler { playerCache.wasOnFire = player.isOnFire(); playerCache.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL); if(isAuthenticated(player)) { - playerCache.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); - playerCache.lastX = player.getX(); - playerCache.lastY = player.getY(); - playerCache.lastZ = player.getZ(); + playerCache.lastLocation.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); + playerCache.lastLocation.lastX = player.getX(); + playerCache.lastLocation.lastY = player.getY(); + playerCache.lastLocation.lastZ = player.getZ(); + playerCache.lastLocation.lastYaw = player.yaw; + playerCache.lastLocation.lastPitch = player.pitch; // Setting the session expire time if(config.main.sessionTimeoutTime != -1) 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 dfa379c..1aeaace 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -95,13 +95,15 @@ public class AuthConfig { * @see wiki */ public static class WorldSpawn { - /** - * Dimension id, e.g. "minecraft:overworld" - */ - public String dimension; - public double x; - public double y; - public double z; + /** + * Dimension id, e.g. "minecraft:overworld" + */ + public String dimension; + public double x; + public double y; + public double z; + public float yaw; + public float pitch; } } public static class LangConfig { diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index b1df3b0..7880644 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -50,10 +50,17 @@ public class PlayerCache { /** * Last recorded position before de-authentication. */ - public String lastDim; - public double lastX; - public double lastY; - public double lastZ; + public static class LastLocation { + public String lastDim; + public double lastX; + public double lastY; + public double lastZ; + public float lastYaw; + public float lastPitch; + } + + public PlayerCache.LastLocation lastLocation = new PlayerCache.LastLocation(); + private static final Gson gson = new Gson(); @@ -70,11 +77,13 @@ public class PlayerCache { this.wasOnFire = player.isOnFire(); // Setting position cache - this.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); + this.lastLocation.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); this.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL); - this.lastX = player.getX(); - this.lastY = player.getY(); - this.lastZ = player.getZ(); + this.lastLocation.lastX = player.getX(); + this.lastLocation.lastY = player.getY(); + this.lastLocation.lastZ = player.getZ(); + this.lastLocation.lastYaw = player.yaw; + this.lastLocation.lastPitch = player.pitch; } else { this.wasOnFire = false; @@ -110,14 +119,13 @@ public class PlayerCache { if (lastLoc != null) { // Getting DB coords JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class); - this.lastDim = lastLocation.get("dim").isJsonNull() ? config.worldSpawn.dimension : lastLocation.get("dim").getAsString(); - this.lastX = lastLocation.get("x").isJsonNull() ? config.worldSpawn.x : lastLocation.get("x").getAsDouble(); - this.lastY = lastLocation.get("y").isJsonNull() ? config.worldSpawn.y : lastLocation.get("y").getAsDouble(); - this.lastZ = lastLocation.get("z").isJsonNull() ? config.worldSpawn.z : lastLocation.get("z").getAsDouble(); + this.lastLocation.lastDim = lastLocation.get("dim").isJsonNull() ? config.worldSpawn.dimension : lastLocation.get("dim").getAsString(); + this.lastLocation.lastX = lastLocation.get("x").isJsonNull() ? config.worldSpawn.x : lastLocation.get("x").getAsDouble(); + this.lastLocation.lastY = lastLocation.get("y").isJsonNull() ? config.worldSpawn.y : lastLocation.get("y").getAsDouble(); + this.lastLocation.lastZ = lastLocation.get("z").isJsonNull() ? config.worldSpawn.z : lastLocation.get("z").getAsDouble(); + this.lastLocation.lastYaw = lastLocation.get("yaw") == null ? 90 : lastLocation.get("yaw").getAsFloat(); + this.lastLocation.lastPitch = lastLocation.get("pitch") == null ? 0 : lastLocation.get("pitch").getAsFloat(); - // Removing location data from DB - json.remove("lastLocation"); - DB.updateUserData(uuid, json.toString()); } } catch (JsonSyntaxException ignored) { // Player didn't have any coords in db to tp to @@ -130,6 +138,7 @@ public class PlayerCache { } this.isAuthenticated = false; this.loginTries = 0; + if(config.experimental.debugMode) logInfo("Cache created. Registered: " + this.isRegistered + ", hashed password: " + this.password + "."); }