From 1f274c3034521c4e7f8e36b95023044d6833d803 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Wed, 18 Mar 2020 19:18:40 +0100 Subject: [PATCH] Added login tries tracking. --- .../simpleauth/commands/LoginCommand.java | 14 +++++++++++--- .../simpleauth/event/AuthEventHandler.java | 1 + .../org/samo_lego/simpleauth/utils/AuthConfig.java | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) 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 b3a2f9c..81c0c59 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -18,8 +18,9 @@ public class LoginCommand { private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); - //private static Text loginTriesExceeded = new LiteralText("§4Too many login tries."); + private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded); private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated); + private static int maxLoginTries = 3; public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/login" command @@ -41,6 +42,9 @@ public class LoginCommand { player.sendMessage(alreadyAuthenticated); return 0; } + else if(SimpleAuth.deauthenticatedUsers.get(player) >= maxLoginTries && maxLoginTries != -1) + player.networkHandler.disconnect(loginTriesExceeded); + else if(SimpleAuth.config.main.enableGlobalPassword) { if (AuthHelper.checkPass("globalPass", pass.toCharArray())) { SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); @@ -51,8 +55,12 @@ public class LoginCommand { SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); return 1; } - player.networkHandler.disconnect(wrongPassword); - //player.sendMessage(wrongPassword); + else if(maxLoginTries == 1) + player.networkHandler.disconnect(wrongPassword); + + player.sendMessage(wrongPassword); + SimpleAuth.deauthenticatedUsers.replace(player, SimpleAuth.deauthenticatedUsers.get(player) + 1); + 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 cfebee3..141c0f3 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -29,6 +29,7 @@ public class AuthEventHandler { // Player joining the server public static void onPlayerJoin(ServerPlayerEntity player) { + // Marking player as not authenticated, (re)setting login tries to zero SimpleAuth.deauthenticatedUsers.put(player, 0); /*CompoundTag loginTries = new CompoundTag(); loginTries.putInt("loginTries", 0); diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java index 264369e..759a29d 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java @@ -50,6 +50,9 @@ public class AuthConfig { public boolean playerInvulnerable = true; // If player should be invisible to mobs before authentication public boolean playerInvisible = true; + // Maximum login tries before kicking the player + // Set to -1 to allow unlimited, not recommended however + public int maxLoginTries = 1; // Time after which player will be kicked if not authenticated - in seconds public int delay = 60; // Disables registering and forces logging in with global password @@ -67,6 +70,7 @@ public class AuthConfig { public String matchPassword = "§6Passwords must match!"; public String passwordUpdated = "§4Your password was updated successfully!"; public String loginRequired = "§cYou are not authenticated!\n§6Use /login to authenticate!"; + public String loginTriesExceeded = "§4Too many login tries."; public String globalPasswordSet = "§aGlobal password was successfully set!"; public String cannotChangePassword = "§aYou cannot change password!"; public String cannotUnregister = "§aYou cannot unregister this account!";