From 9ab30f2ee972ad52ebff33055d7b1ae9e18cb395 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Fri, 15 May 2020 14:09:28 +0200 Subject: [PATCH] Command text feedaback can be reloaded now. Deprecated ChangepwCommand.java and UnregisterCommand.java --- .../simpleauth/commands/AuthCommand.java | 18 ++-- .../simpleauth/commands/ChangepwCommand.java | 84 ------------------- .../simpleauth/commands/LoginCommand.java | 34 ++++---- .../simpleauth/commands/LogoutCommand.java | 5 +- .../simpleauth/commands/RegisterCommand.java | 30 +++---- .../commands/UnregisterCommand.java | 58 ------------- 6 files changed, 34 insertions(+), 195 deletions(-) delete mode 100644 src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java delete mode 100644 src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java 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 2899768..7a6b3f5 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -7,7 +7,6 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.SimpleAuth; @@ -29,12 +28,7 @@ import static org.samo_lego.simpleauth.SimpleAuth.db; public class AuthCommand { private static final Logger LOGGER = LogManager.getLogger(); - - private static Text userdataDeleted = new LiteralText(config.lang.userdataDeleted); - private static Text userdataUpdated = new LiteralText(config.lang.userdataUpdated); - private static Text configurationReloaded = new LiteralText(config.lang.configurationReloaded); - private static Text globalPasswordSet = new LiteralText(config.lang.globalPasswordSet); - + public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/auth" command dispatcher.register(literal("auth") @@ -110,7 +104,7 @@ public class AuthCommand { config = AuthConfig.load(new File("./mods/SimpleAuth/config.json")); if(sender != null) - ((PlayerEntity) sender).sendMessage(configurationReloaded, false); + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.configurationReloaded), false); else LOGGER.info(config.lang.configurationReloaded); return 1; @@ -126,7 +120,7 @@ public class AuthCommand { config.save(new File("./mods/SimpleAuth/config.json")); if(sender != null) - ((PlayerEntity) sender).sendMessage(globalPasswordSet, false); + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false); else LOGGER.info(config.lang.globalPasswordSet); return 1; @@ -153,7 +147,7 @@ public class AuthCommand { SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); if(sender != null) - ((PlayerEntity) sender).sendMessage(userdataDeleted, false); + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false); else LOGGER.info(config.lang.userdataDeleted); return 1; // Success @@ -171,7 +165,7 @@ public class AuthCommand { if(db.registerUser(uuid, playerdata.toString())) { if(sender != null) - ((PlayerEntity) sender).sendMessage(userdataUpdated, false); + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); else LOGGER.info(config.lang.userdataUpdated); return 1; @@ -191,7 +185,7 @@ public class AuthCommand { db.updateUserData(uuid, playerdata.toString()); if(sender != null) - ((PlayerEntity) sender).sendMessage(userdataUpdated, false); + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); else LOGGER.info(config.lang.userdataUpdated); return 1; diff --git a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java deleted file mode 100644 index 4f4cc08..0000000 --- a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.samo_lego.simpleauth.commands; - -import com.google.gson.JsonObject; -import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; -import org.samo_lego.simpleauth.SimpleAuth; -import org.samo_lego.simpleauth.utils.AuthHelper; - -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.utils.UuidConverter.convertUuid; - -public class ChangepwCommand { - private static Text enterNewPassword = new LiteralText(SimpleAuth.config.lang.enterNewPassword); - private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); - private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); - private static Text passwordUpdated = new LiteralText(SimpleAuth.config.lang.passwordUpdated); - private static Text cannotChangePassword = new LiteralText(SimpleAuth.config.lang.cannotChangePassword); - - public static void registerCommand(CommandDispatcher dispatcher) { - // Registering the "/changepw" command - dispatcher.register(literal("changepw") - .executes(ctx -> { - ctx.getSource().getPlayer().sendMessage(enterPassword, false); - return 1; - }) - .then(argument("oldPassword", word()) - .executes(ctx -> { - ctx.getSource().getPlayer().sendMessage(enterNewPassword, false); - return 1; - }) - .then(argument("newPassword", word()) - .executes( ctx -> changepw( - ctx.getSource(), - getString(ctx, "oldPassword"), - getString(ctx, "newPassword") - ) - ) - ) - ) - ); - } - - // Method called for checking the password and then changing it - private static int changepw(ServerCommandSource source, String oldPass, String newPass) throws CommandSyntaxException { - // Getting the player who send the command - ServerPlayerEntity player = source.getPlayer(); - - if (SimpleAuth.config.main.enableGlobalPassword) { - player.sendMessage(cannotChangePassword, false); - return 0; - } - else if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) { - if(newPass.length() < SimpleAuth.config.main.minPasswordChars) { - player.sendMessage(new LiteralText( - String.format(SimpleAuth.config.lang.minPasswordChars, SimpleAuth.config.main.minPasswordChars) - ), false); - return 0; - } - else if(newPass.length() > SimpleAuth.config.main.maxPasswordChars && SimpleAuth.config.main.maxPasswordChars != -1) { - player.sendMessage(new LiteralText( - String.format(SimpleAuth.config.lang.maxPasswordChars, SimpleAuth.config.main.maxPasswordChars) - ), false); - return 0; - } - // JSON object holding password (may hold some other info in the future) - JsonObject playerdata = new JsonObject(); - String hash = AuthHelper.hashPass(newPass.toCharArray()); - playerdata.addProperty("password", hash); - - SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString()); - player.sendMessage(passwordUpdated, false); - return 1; - } - player.sendMessage(wrongPassword, false); - 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 7f8b171..8670fce 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -2,11 +2,9 @@ package org.samo_lego.simpleauth.commands; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.utils.AuthHelper; @@ -14,16 +12,10 @@ 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.utils.UuidConverter.convertUuid; 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 notRegistered = new LiteralText(SimpleAuth.config.lang.notRegistered); - private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded); - private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated); - private static int maxLoginTries = SimpleAuth.config.main.maxLoginTries; public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/login" command @@ -32,7 +24,7 @@ public class LoginCommand { .executes(ctx -> login(ctx.getSource(), getString(ctx, "password")) // Tries to authenticate user )) .executes(ctx -> { - ctx.getSource().getPlayer().sendMessage(enterPassword, false); + ctx.getSource().getPlayer().sendMessage(new LiteralText(config.lang.enterPassword), false); return 0; })); } @@ -42,32 +34,34 @@ public class LoginCommand { // Getting the player who send the command ServerPlayerEntity player = source.getPlayer(); - String uuid = convertUuid(player); - int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); - if(SimpleAuth.isAuthenticated(player)) { - player.sendMessage(alreadyAuthenticated, false); + player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); return 0; } - else if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { - player.networkHandler.disconnect(loginTriesExceeded); + + String uuid = convertUuid(player); + int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); + int maxLoginTries = config.main.maxLoginTries; + + if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { + player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded)); return 0; } else if(passwordResult == 1) { - SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); + SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated)); return 1; } else if(passwordResult == -1) { - player.sendMessage(notRegistered, false); + player.sendMessage(new LiteralText(config.lang.notRegistered), false); return 0; } // Kicking the player out else if(maxLoginTries == 1) { - player.networkHandler.disconnect(wrongPassword); + player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword)); return 0; } // Sending wrong pass message - player.sendMessage(wrongPassword, false); + player.sendMessage(new LiteralText(config.lang.wrongPassword), false); // ++ the login tries SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1; return 0; 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 0774414..8c1cfa2 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java @@ -5,13 +5,12 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import static net.minecraft.server.command.CommandManager.literal; +import static org.samo_lego.simpleauth.SimpleAuth.config; public class LogoutCommand { - private static Text successfulLogout = new LiteralText(SimpleAuth.config.lang.successfulLogout); public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/logout" command @@ -23,7 +22,7 @@ public class LogoutCommand { private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException { ServerPlayerEntity player = serverCommandSource.getPlayer(); SimpleAuth.deauthenticatePlayer(player); - player.sendMessage(successfulLogout, false); + player.sendMessage(new LiteralText(config.lang.successfulLogout), false); return 1; } } 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 b0ae50e..ba6c85d 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -6,7 +6,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.utils.AuthHelper; @@ -14,16 +13,11 @@ 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.utils.UuidConverter.convertUuid; public class RegisterCommand { - private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); - private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); - private static Text alreadyRegistered = new LiteralText(SimpleAuth.config.lang.alreadyRegistered); - private static Text registerSuccess = new LiteralText(SimpleAuth.config.lang.registerSuccess); - private static Text matchPass = new LiteralText( SimpleAuth.config.lang.matchPassword); - private static Text loginRequired = new LiteralText(SimpleAuth.config.lang.loginRequired); public static void registerCommand(CommandDispatcher dispatcher) { @@ -34,7 +28,7 @@ public class RegisterCommand { .executes( ctx -> register(ctx.getSource(), getString(ctx, "password"), getString(ctx, "passwordAgain"))) )) .executes(ctx -> { - ctx.getSource().getPlayer().sendMessage(enterPassword, false); + ctx.getSource().getPlayer().sendMessage(new LiteralText(config.lang.enterPassword), false); return 0; })); } @@ -42,24 +36,24 @@ public class RegisterCommand { // Method called for hashing the password & writing to DB private static int register(ServerCommandSource source, String pass1, String pass2) throws CommandSyntaxException { ServerPlayerEntity player = source.getPlayer(); - if(SimpleAuth.config.main.enableGlobalPassword) { - player.sendMessage(loginRequired, false); + if(config.main.enableGlobalPassword) { + player.sendMessage(new LiteralText(config.lang.loginRequired), false); return 0; } else if(SimpleAuth.isAuthenticated(player)) { - player.sendMessage(alreadyAuthenticated, false); + player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); return 0; } else if(pass1.equals(pass2)) { - if(pass1.length() < SimpleAuth.config.main.minPasswordChars) { + if(pass1.length() < config.main.minPasswordChars) { player.sendMessage(new LiteralText( - String.format(SimpleAuth.config.lang.minPasswordChars, SimpleAuth.config.main.minPasswordChars) + String.format(config.lang.minPasswordChars, config.main.minPasswordChars) ), false); return 0; } - else if(pass1.length() > SimpleAuth.config.main.maxPasswordChars && SimpleAuth.config.main.maxPasswordChars != -1) { + else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { player.sendMessage(new LiteralText( - String.format(SimpleAuth.config.lang.maxPasswordChars, SimpleAuth.config.main.maxPasswordChars) + String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars) ), false); return 0; } @@ -69,13 +63,13 @@ public class RegisterCommand { playerdata.addProperty("password", hash); if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) { - SimpleAuth.authenticatePlayer(player, registerSuccess); + SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess)); return 1; } - player.sendMessage(alreadyRegistered, false); + player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false); return 0; } - player.sendMessage(matchPass, false); + player.sendMessage(new LiteralText(config.lang.matchPassword), false); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java deleted file mode 100644 index cd59a39..0000000 --- a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.samo_lego.simpleauth.commands; - -import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; -import org.samo_lego.simpleauth.SimpleAuth; -import org.samo_lego.simpleauth.utils.AuthHelper; - -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.utils.UuidConverter.convertUuid; - -public class UnregisterCommand { - private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); - private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); - private static Text accountDeleted = new LiteralText(SimpleAuth.config.lang.accountDeleted); - private static Text cannotUnregister = new LiteralText(SimpleAuth.config.lang.cannotUnregister); - - public static void registerCommand(CommandDispatcher dispatcher) { - // Registering the "/unregister" command - dispatcher.register(literal("unregister") - .executes(ctx -> { - ctx.getSource().getPlayer().sendMessage(enterPassword, false); - return 1; - }) - .then(argument("password", word()) - .executes( ctx -> unregister( - ctx.getSource(), - getString(ctx, "password") - ) - ) - ) - ); - } - - // Method called for checking the password and then removing user's account from db - private static int unregister(ServerCommandSource source, String pass) throws CommandSyntaxException { - // Getting the player who send the command - ServerPlayerEntity player = source.getPlayer(); - if (SimpleAuth.config.main.enableGlobalPassword) { - player.sendMessage(cannotUnregister, false); - return 0; - } - else if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) { - SimpleAuth.deauthenticatePlayer(player); - SimpleAuth.db.deleteUserData(convertUuid(player)); - player.sendMessage(accountDeleted, false); - return 1; - } - player.sendMessage(wrongPassword, false); - return 0; - } -}