Implementing #26

This commit is contained in:
samo_lego 2020-10-11 22:03:34 +02:00
parent fcab8f8a46
commit 25cdfc961c
6 changed files with 93 additions and 55 deletions

View File

@ -146,10 +146,10 @@ public class SimpleAuth implements DedicatedServerModInitializer {
data.addProperty("password", playerCache.password); data.addProperty("password", playerCache.password);
JsonObject lastLocation = new JsonObject(); JsonObject lastLocation = new JsonObject();
lastLocation.addProperty("dim", playerCache.lastDim); lastLocation.addProperty("dim", playerCache.lastLocation.lastDim);
lastLocation.addProperty("x", playerCache.lastX); lastLocation.addProperty("x", playerCache.lastLocation.lastX);
lastLocation.addProperty("y", playerCache.lastY); lastLocation.addProperty("y", playerCache.lastLocation.lastY);
lastLocation.addProperty("z", playerCache.lastZ); lastLocation.addProperty("z", playerCache.lastLocation.lastZ);
data.addProperty("lastLocation", lastLocation.toString()); data.addProperty("lastLocation", lastLocation.toString());
@ -248,8 +248,13 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Marking player as not authenticated // Marking player as not authenticated
String uuid = convertUuid(player); String uuid = convertUuid(player);
playerCacheMap.put(uuid, new PlayerCache(uuid, player)); PlayerCache cache = playerCacheMap.get(uuid);
playerCacheMap.get(uuid).isAuthenticated = false; if(cache == null) {
cache = new PlayerCache(uuid, player);
playerCacheMap.put(uuid, cache);
}
cache.isAuthenticated = false;
// Teleporting player to spawn to hide its position // Teleporting player to spawn to hide its position
if(config.main.spawnOnJoin) if(config.main.spawnOnJoin)
@ -303,8 +308,8 @@ public class SimpleAuth implements DedicatedServerModInitializer {
config.worldSpawn.x, config.worldSpawn.x,
config.worldSpawn.y, config.worldSpawn.y,
config.worldSpawn.z, config.worldSpawn.z,
90, config.worldSpawn.yaw,
90 config.worldSpawn.pitch
); );
return; return;
} }
@ -312,22 +317,22 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Puts player to last cached position // Puts player to last cached position
try { try {
player.teleport( player.teleport(
server.getWorld(RegistryKey.of(Registry.DIMENSION, new Identifier(cache.lastDim))), server.getWorld(RegistryKey.of(Registry.DIMENSION, new Identifier(cache.lastLocation.lastDim))),
cache.lastX, cache.lastLocation.lastX,
cache.lastY, cache.lastLocation.lastY,
cache.lastZ, cache.lastLocation.lastZ,
0, cache.lastLocation.lastYaw,
0 cache.lastLocation.lastPitch
); );
} catch (Error e) { } catch (Error e) {
player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false); player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false);
logError("Couldn't teleport player " + player.getName().asString()); logError("Couldn't teleport player " + player.getName().asString());
logError( logError(
String.format("Last recorded position is X: %s, Y: %s, Z: %s in dimension %s", String.format("Last recorded position is X: %s, Y: %s, Z: %s in dimension %s",
cache.lastX, cache.lastLocation.lastX,
cache.lastY, cache.lastLocation.lastY,
cache.lastZ, cache.lastLocation.lastZ,
cache.lastDim cache.lastLocation.lastDim
)); ));
} }
} }

View File

@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.commands;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.DimensionArgumentType; import net.minecraft.command.argument.DimensionArgumentType;
import net.minecraft.command.argument.RotationArgumentType;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -48,17 +49,23 @@ public class AuthCommand {
ctx.getSource().getEntityOrThrow().getEntityWorld().getRegistryKey().getValue(), ctx.getSource().getEntityOrThrow().getEntityWorld().getRegistryKey().getValue(),
ctx.getSource().getEntityOrThrow().getX(), ctx.getSource().getEntityOrThrow().getX(),
ctx.getSource().getEntityOrThrow().getY(), 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("dimension", DimensionArgumentType.dimension())
.then(argument("position", BlockPosArgumentType.blockPos()) .then(argument("position", BlockPosArgumentType.blockPos())
.executes(ctx -> setSpawn( .then(argument("angle", RotationArgumentType.rotation())
ctx.getSource(), .executes(ctx -> setSpawn(
DimensionArgumentType.getDimensionArgument(ctx, "dimension").getRegistryKey().getValue(), ctx.getSource(),
BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(), DimensionArgumentType.getDimensionArgument(ctx, "dimension").getRegistryKey().getValue(),
// +1 to not spawn player in ground BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(),
BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getY() + 1, // +1 to not spawn player in ground
BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getZ() 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 x x coordinate of the global spawn
* @param y y coordinate of the global spawn * @param y y coordinate of the global spawn
* @param z z 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 * @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 // Setting config values and saving
config.worldSpawn.dimension = String.valueOf(world); config.worldSpawn.dimension = String.valueOf(world);
config.worldSpawn.x = x; config.worldSpawn.x = x;
config.worldSpawn.y = y; config.worldSpawn.y = y;
config.worldSpawn.z = z; config.worldSpawn.z = z;
config.worldSpawn.yaw = yaw;
config.worldSpawn.pitch = pitch;
config.main.spawnOnJoin = true; config.main.spawnOnJoin = true;
config.save(new File("./mods/SimpleAuth/config.json")); config.save(new File("./mods/SimpleAuth/config.json"));

View File

@ -5,10 +5,11 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import org.samo_lego.simpleauth.storage.PlayerCache;
import static net.minecraft.server.command.CommandManager.literal; import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.SimpleAuth.*;
import static org.samo_lego.simpleauth.SimpleAuth.deauthenticatePlayer; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class LogoutCommand { public class LogoutCommand {
@ -21,6 +22,14 @@ public class LogoutCommand {
private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException { private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException {
ServerPlayerEntity player = serverCommandSource.getPlayer(); 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); deauthenticatePlayer(player);
player.sendMessage(new LiteralText(config.lang.successfulLogout), false); player.sendMessage(new LiteralText(config.lang.successfulLogout), false);
return 1; return 1;

View File

@ -122,10 +122,12 @@ public class AuthEventHandler {
playerCache.wasOnFire = player.isOnFire(); playerCache.wasOnFire = player.isOnFire();
playerCache.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL); playerCache.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL);
if(isAuthenticated(player)) { if(isAuthenticated(player)) {
playerCache.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue()); playerCache.lastLocation.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue());
playerCache.lastX = player.getX(); playerCache.lastLocation.lastX = player.getX();
playerCache.lastY = player.getY(); playerCache.lastLocation.lastY = player.getY();
playerCache.lastZ = player.getZ(); playerCache.lastLocation.lastZ = player.getZ();
playerCache.lastLocation.lastYaw = player.yaw;
playerCache.lastLocation.lastPitch = player.pitch;
// Setting the session expire time // Setting the session expire time
if(config.main.sessionTimeoutTime != -1) if(config.main.sessionTimeoutTime != -1)

View File

@ -95,13 +95,15 @@ public class AuthConfig {
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Coordinate-Hiding" target="_blank">wiki</a> * @see <a href="https://github.com/samolego/SimpleAuth/wiki/Coordinate-Hiding" target="_blank">wiki</a>
*/ */
public static class WorldSpawn { public static class WorldSpawn {
/** /**
* Dimension id, e.g. "minecraft:overworld" * Dimension id, e.g. "minecraft:overworld"
*/ */
public String dimension; public String dimension;
public double x; public double x;
public double y; public double y;
public double z; public double z;
public float yaw;
public float pitch;
} }
} }
public static class LangConfig { public static class LangConfig {

View File

@ -50,10 +50,17 @@ public class PlayerCache {
/** /**
* Last recorded position before de-authentication. * Last recorded position before de-authentication.
*/ */
public String lastDim; public static class LastLocation {
public double lastX; public String lastDim;
public double lastY; public double lastX;
public double lastZ; 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(); private static final Gson gson = new Gson();
@ -70,11 +77,13 @@ public class PlayerCache {
this.wasOnFire = player.isOnFire(); this.wasOnFire = player.isOnFire();
// Setting position cache // 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.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL);
this.lastX = player.getX(); this.lastLocation.lastX = player.getX();
this.lastY = player.getY(); this.lastLocation.lastY = player.getY();
this.lastZ = player.getZ(); this.lastLocation.lastZ = player.getZ();
this.lastLocation.lastYaw = player.yaw;
this.lastLocation.lastPitch = player.pitch;
} }
else { else {
this.wasOnFire = false; this.wasOnFire = false;
@ -110,14 +119,13 @@ public class PlayerCache {
if (lastLoc != null) { if (lastLoc != null) {
// Getting DB coords // Getting DB coords
JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class); JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class);
this.lastDim = lastLocation.get("dim").isJsonNull() ? config.worldSpawn.dimension : lastLocation.get("dim").getAsString(); this.lastLocation.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.lastLocation.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.lastLocation.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.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) { } catch (JsonSyntaxException ignored) {
// Player didn't have any coords in db to tp to // Player didn't have any coords in db to tp to
@ -130,6 +138,7 @@ public class PlayerCache {
} }
this.isAuthenticated = false; this.isAuthenticated = false;
this.loginTries = 0; this.loginTries = 0;
if(config.experimental.debugMode) if(config.experimental.debugMode)
logInfo("Cache created. Registered: " + this.isRegistered + ", hashed password: " + this.password + "."); logInfo("Cache created. Registered: " + this.isRegistered + ", hashed password: " + this.password + ".");
} }