From cfce56df959188d7215bab0e4365011c8bdcc431 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Wed, 15 Jul 2020 20:40:03 +0200 Subject: [PATCH] Cleanup --- gradle.properties | 6 +-- .../org/samo_lego/simpleauth/SimpleAuth.java | 41 +++++++++++-------- .../simpleauth/commands/AccountCommand.java | 17 ++++---- .../simpleauth/commands/AuthCommand.java | 27 ++++++------ .../simpleauth/commands/LoginCommand.java | 9 ++-- .../simpleauth/commands/RegisterCommand.java | 9 ++-- .../simpleauth/event/AuthEventHandler.java | 2 +- .../simpleauth/storage/AuthConfig.java | 11 ++--- .../simpleauth/storage/PlayerCache.java | 10 ++--- .../simpleauth/utils/AuthHelper.java | 2 +- 10 files changed, 73 insertions(+), 61 deletions(-) diff --git a/gradle.properties b/gradle.properties index e9f1289..11e0896 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G # Fabric properties minecraft_version=1.16.1 -yarn_mappings=1.16.1+build.20 -loader_version=0.8.9+build.203 +yarn_mappings=1.16.1+build.21 +loader_version=0.9.0+build.204 #Fabric api fabric_version=0.14.1+build.372-1.16 # Mod Properties -mod_version = 1.4.6 +mod_version = 1.4.7 maven_group = org.samo_lego archives_base_name = simpleauth diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 4a6cee5..7fce2a2 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -35,6 +35,8 @@ import java.util.HashMap; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; import static org.samo_lego.simpleauth.utils.CarpetHelper.isPlayerCarpetFake; @@ -43,9 +45,11 @@ import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; public class SimpleAuth implements DedicatedServerModInitializer { private static final Logger LOGGER = LogManager.getLogger(); - public static SimpleAuthDatabase db = new SimpleAuthDatabase(); + public static SimpleAuthDatabase DB = new SimpleAuthDatabase(); - // HashMap of players that are not authenticated + public static final ExecutorService THREADPOOL = Executors.newCachedThreadPool(); + + // HashMap of players that are not authenticated // Rather than storing all the authenticated players, we just store ones that are not authenticated // It stores some data as well, e.g. login tries and user password public static HashMap deauthenticatedUsers = new HashMap<>(); @@ -73,13 +77,13 @@ public class SimpleAuth implements DedicatedServerModInitializer { LOGGER.info("[SimpleAuth] This mod wouldn't exist without the awesome Fabric Community. TYSM guys!"); // Creating data directory (database and config files are stored there) - File file = new File(gameDirectory + "/mods/SimpleAuth/levelDBStore"); + File file = new File(gameDirectory + "/mods/SimpleAuth/leveldbStore"); if (!file.exists() && !file.mkdirs()) throw new RuntimeException("[SimpleAuth] Error creating directory!"); // Loading config config = AuthConfig.load(new File(gameDirectory + "/mods/SimpleAuth/config.json")); // Connecting to db - db.openConnection(); + DB.openConnection(); try { serverProp.load(new FileReader(gameDirectory + "/server.properties")); @@ -118,7 +122,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { private void onStopServer() { LOGGER.info("[SimpleAuth] Shutting down SimpleAuth."); - WriteBatch batch = db.getLevelDBStore().createWriteBatch(); + WriteBatch batch = DB.getLevelDBStore().createWriteBatch(); // Writing coords of de-authenticated players to database deauthenticatedUsers.forEach((uuid, playerCache) -> { JsonObject data = new JsonObject(); @@ -136,22 +140,26 @@ public class SimpleAuth implements DedicatedServerModInitializer { }); try { // Writing and closing batch - db.getLevelDBStore().write(batch); + DB.getLevelDBStore().write(batch); batch.close(); } catch (IOException e) { LOGGER.error("[SimpleAuth] Error saving player data! " + e.getMessage()); } // Closing DB connection - db.close(); + DB.close(); } - // Getting some config options - private static Text notAuthenticated() { - if(SimpleAuth.config.main.enableGlobalPassword) { - return new LiteralText(SimpleAuth.config.lang.loginRequired); - } - return new LiteralText(SimpleAuth.config.lang.notAuthenticated); + // Getting not authenticated text + private static LiteralText notAuthenticated(ServerPlayerEntity player) { + final PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); + if(SimpleAuth.config.main.enableGlobalPassword || cache.isRegistered) + return new LiteralText( + SimpleAuth.config.lang.notAuthenticated + "\n" + SimpleAuth.config.lang.loginRequired + ); + return new LiteralText( + SimpleAuth.config.lang.notAuthenticated+ "\n" + SimpleAuth.config.lang.registerRequired + ); } // Authenticates player and sends the message @@ -185,7 +193,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { // De-authenticates player public static void deauthenticatePlayer(ServerPlayerEntity player) { - if(db.isClosed()) + if(DB.isClosed()) return; // Marking player as not authenticated, (re)setting login tries to zero @@ -197,7 +205,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { teleportPlayer(player, true); // Player is now not authenticated - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); // Setting the player to be invisible to mobs and also invulnerable player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable); @@ -206,7 +214,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { timer.schedule(new TimerTask() { @Override public void run() { - if(!SimpleAuth.isAuthenticated(player) && player.networkHandler.getConnection().isOpen()) // Kicking player if not authenticated + // Kicking player if not authenticated + if(!SimpleAuth.isAuthenticated(player) && player.networkHandler.getConnection().isOpen()) player.networkHandler.disconnect(new LiteralText(SimpleAuth.config.lang.timeExpired)); } }, SimpleAuth.config.main.delay * 1000); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java index 96fef00..0afb645 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java @@ -13,6 +13,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; +import static org.samo_lego.simpleauth.SimpleAuth.THREADPOOL; import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; @@ -71,10 +72,10 @@ public class AccountCommand { return 0; } - // New thread to avoid lag spikes - new Thread(() -> { + // Different thread to avoid lag spikes + THREADPOOL.submit(() -> { if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) { - SimpleAuth.db.deleteUserData(convertUuid(player)); + SimpleAuth.DB.deleteUserData(convertUuid(player)); player.sendMessage( new LiteralText(config.lang.accountDeleted), false @@ -86,7 +87,7 @@ public class AccountCommand { new LiteralText(config.lang.wrongPassword), false ); - }).start(); + }); return 0; } @@ -102,8 +103,8 @@ public class AccountCommand { ); return 0; } - // New thread to avoid lag spikes - new Thread(() -> { + // Different thread to avoid lag spikes + THREADPOOL.submit(() -> { if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) { if (newPass.length() < config.main.minPasswordChars) { player.sendMessage(new LiteralText( @@ -122,7 +123,7 @@ public class AccountCommand { String hash = AuthHelper.hashPass(newPass.toCharArray()); playerdata.addProperty("password", hash); - SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString()); + SimpleAuth.DB.updateUserData(convertUuid(player), playerdata.toString()); player.sendMessage( new LiteralText(config.lang.passwordUpdated), false @@ -133,7 +134,7 @@ public class AccountCommand { new LiteralText(config.lang.wrongPassword), false ); - }).start(); + }); return 0; } } 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 bd88120..9008f0b 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -22,8 +22,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; -import static org.samo_lego.simpleauth.SimpleAuth.config; -import static org.samo_lego.simpleauth.SimpleAuth.db; +import static org.samo_lego.simpleauth.SimpleAuth.*; public class AuthCommand { private static final Logger LOGGER = LogManager.getLogger(); @@ -114,13 +113,13 @@ public class AuthCommand { private static int setGlobalPassword(ServerCommandSource source, String pass) { // Getting the player who send the command Entity sender = source.getEntity(); - // New thread to avoid lag spikes - new Thread(() -> { + // Different thread to avoid lag spikes + THREADPOOL.submit(() -> { // Writing the global pass to config config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray()); config.main.enableGlobalPassword = true; config.save(new File("./mods/SimpleAuth/config.json")); - }).start(); + }); if(sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false); @@ -150,10 +149,10 @@ public class AuthCommand { // Deleting (unregistering) user's account private static int removeAccount(ServerCommandSource source, String uuid) { Entity sender = source.getEntity(); - new Thread(() -> { - db.deleteUserData(uuid); + THREADPOOL.submit(() -> { + DB.deleteUserData(uuid); SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); - }).start(); + }); if(sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false); @@ -167,19 +166,19 @@ public class AuthCommand { // Getting the player who send the command Entity sender = source.getEntity(); - new Thread(() -> { + THREADPOOL.submit(() -> { // JSON object holding password (may hold some other info in the future) JsonObject playerdata = new JsonObject(); String hash = AuthHelper.hashPass(password.toCharArray()); playerdata.addProperty("password", hash); - if (db.registerUser(uuid, playerdata.toString())) { + if (DB.registerUser(uuid, playerdata.toString())) { if (sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); else LOGGER.info(config.lang.userdataUpdated); } - }).start(); + }); return 0; } @@ -188,18 +187,18 @@ public class AuthCommand { // Getting the player who send the command Entity sender = source.getEntity(); - new Thread(() -> { + THREADPOOL.submit(() -> { // JSON object holding password (may hold some other info in the future) JsonObject playerdata = new JsonObject(); String hash = AuthHelper.hashPass(password.toCharArray()); playerdata.addProperty("password", hash); - db.updateUserData(uuid, playerdata.toString()); + DB.updateUserData(uuid, playerdata.toString()); if (sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); else LOGGER.info(config.lang.userdataUpdated); - }).start(); + }); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index d784a75..84671e1 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -12,6 +12,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; +import static org.samo_lego.simpleauth.SimpleAuth.THREADPOOL; import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; @@ -38,8 +39,8 @@ public class LoginCommand { player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); return 0; } - // Putting rest of the command in new thread to avoid lag spikes - new Thread(() -> { + // Putting rest of the command in different thread to avoid lag spikes + THREADPOOL.submit(() -> { int maxLoginTries = config.main.maxLoginTries; int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); @@ -52,7 +53,7 @@ public class LoginCommand { return; } else if(passwordResult == -1) { - player.sendMessage(new LiteralText(config.lang.notRegistered), false); + player.sendMessage(new LiteralText(config.lang.registerRequired), false); return; } // Kicking the player out @@ -64,7 +65,7 @@ public class LoginCommand { player.sendMessage(new LiteralText(config.lang.wrongPassword), false); // ++ the login tries SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1; - }).start(); + }); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index 3eec9c1..1fcdbd4 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -13,6 +13,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; +import static org.samo_lego.simpleauth.SimpleAuth.THREADPOOL; import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; @@ -48,8 +49,8 @@ public class RegisterCommand { player.sendMessage(new LiteralText(config.lang.matchPassword), false); return 0; } - // New thread to avoid lag spikes - new Thread(() -> { + // Different thread to avoid lag spikes + THREADPOOL.submit(() -> { if(pass1.length() < config.main.minPasswordChars) { player.sendMessage(new LiteralText( String.format(config.lang.minPasswordChars, config.main.minPasswordChars) @@ -67,12 +68,12 @@ public class RegisterCommand { JsonObject playerdata = new JsonObject(); playerdata.addProperty("password", hash); - if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) { + if (SimpleAuth.DB.registerUser(convertUuid(player), playerdata.toString())) { SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess)); return; } player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false); - }).start(); + }); return 0; } } 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 20d12db..5e7c7f8 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -91,7 +91,7 @@ public class AuthEventHandler { if(playerCache.isRegistered) player.sendMessage(new LiteralText(config.lang.loginRequired), false); else - player.sendMessage(new LiteralText(config.lang.notRegistered), false); + player.sendMessage(new LiteralText(config.lang.registerRequired), false); } else { deauthenticatePlayer(player); 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 6cf88a2..16e7037 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -68,24 +68,23 @@ public class AuthConfig { public String wrongPassword = "§4Wrong password!"; public String matchPassword = "§6Passwords must match!"; public String passwordUpdated = "§aYour password was updated successfully!"; - public String loginRequired = "§cYou are not authenticated!\n§6Use /login to authenticate!"; + public String loginRequired = "§6Use /login to authenticate!"; public String loginTriesExceeded = "§4Too many login tries."; public String globalPasswordSet = "§aGlobal password was successfully set!"; public String cannotChangePassword = "§cYou cannot change password!"; public String cannotUnregister = "§cYou cannot unregister this account!"; - public String notAuthenticated = "§cYou are not authenticated!\n§6Try with /login or /register."; + public String notAuthenticated = "§cYou are not authenticated!"; public String alreadyAuthenticated = "§6You are already authenticated."; public String successfullyAuthenticated = "§aYou are now authenticated."; public String successfulLogout = "§aLogged out successfully."; public String timeExpired = "§cTime for authentication has expired."; - public String notRegistered = "§6This account is not yet registered! Type `/register` first"; + public String registerRequired = "§6Type /register to claim this account."; public String alreadyRegistered = "§6This account name is already registered!"; public String registerSuccess = "§aYou are now authenticated."; public String userdataDeleted = "§aUserdata deleted."; public String userdataUpdated = "§aUserdata updated."; public String accountDeleted = "§aYour account was successfully deleted!"; public String configurationReloaded = "§aConfiguration file was reloaded successfully."; - public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!"; public String maxPasswordChars = "§6Password can be at most %d characters long!"; public String minPasswordChars = "§6Password needs to be at least %d characters long!"; public String disallowedUsername = "§6Invalid username characters! Allowed character regex: %s"; @@ -130,7 +129,9 @@ public class AuthConfig { public static AuthConfig load(File file) { AuthConfig config; if (file.exists()) { - try (FileReader fileReader = new FileReader(file)) { + try (BufferedReader fileReader = new BufferedReader( + new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8) + )) { config = gson.fromJson(fileReader, AuthConfig.class); } catch (IOException e) { throw new RuntimeException("[SimpleAuth] Problem occurred when trying to load config: ", e); 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 495942c..4da6ade 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -6,8 +6,8 @@ import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import net.minecraft.server.network.ServerPlayerEntity; +import static org.samo_lego.simpleauth.SimpleAuth.DB; import static org.samo_lego.simpleauth.SimpleAuth.config; -import static org.samo_lego.simpleauth.SimpleAuth.db; public class PlayerCache { public boolean isRegistered; @@ -29,7 +29,7 @@ public class PlayerCache { public PlayerCache(String uuid, ServerPlayerEntity player) { - if(db.isClosed()) + if(DB.isClosed()) return; if(player != null) { @@ -44,8 +44,8 @@ public class PlayerCache { this.lastZ = player.getZ(); } - if(db.isUserRegistered(uuid)) { - String data = db.getData(uuid); + if(DB.isUserRegistered(uuid)) { + String data = DB.getData(uuid); // Getting (hashed) password JsonObject json = gson.fromJson(data, JsonObject.class); @@ -66,7 +66,7 @@ public class PlayerCache { // Removing location data from DB json.remove("lastLocation"); - db.updateUserData(uuid, json.toString()); + DB.updateUserData(uuid, json.toString()); } } catch (JsonSyntaxException ignored) { // Player didn't have any coords in db to tp to diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index f923776..fa9e692 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -41,7 +41,7 @@ public class AuthHelper { hashed = SimpleAuth.deauthenticatedUsers.get(uuid).password; // Hashed password from DB else { - JsonObject json = parser.parse(SimpleAuth.db.getData(uuid)).getAsJsonObject(); + JsonObject json = parser.parse(SimpleAuth.DB.getData(uuid)).getAsJsonObject(); hashed = json.get("password").getAsString(); }