From 206fedfd8765987da659f4b9ef6bcee78a79f3a2 Mon Sep 17 00:00:00 2001 From: Petr Ilin Date: Fri, 23 Dec 2022 02:36:45 +0300 Subject: Refactoring + Fixes --- .../limboauth/command/ChangePasswordCommand.java | 19 +++++---- .../limboauth/command/DestroySessionCommand.java | 5 +-- .../command/ForceChangePasswordCommand.java | 13 +++---- .../limboauth/command/ForceUnregisterCommand.java | 7 ++-- .../limboauth/command/LimboAuthCommand.java | 13 +++---- .../elytrium/limboauth/command/PremiumCommand.java | 18 ++++----- .../elytrium/limboauth/command/TotpCommand.java | 45 +++++++++++----------- .../limboauth/command/UnregisterCommand.java | 13 +++---- 8 files changed, 63 insertions(+), 70 deletions(-) (limited to 'src/main/java/net/elytrium/limboauth/command') diff --git a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java index 53280ec..c58448e 100644 --- a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java @@ -29,7 +29,6 @@ import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class ChangePasswordCommand implements SimpleCommand { @@ -69,32 +68,32 @@ public class ChangePasswordCommand implements SimpleCommand { if (this.needOldPass) { RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, ((Player) source).getUsername()); if (player == null) { - source.sendMessage(this.notRegistered, MessageType.SYSTEM); + source.sendMessage(this.notRegistered); return; } else if (player.getHash().isEmpty()) { - source.sendMessage(this.crackedCommand, MessageType.SYSTEM); + source.sendMessage(this.crackedCommand); } else if (!AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { - source.sendMessage(this.wrongPassword, MessageType.SYSTEM); + source.sendMessage(this.wrongPassword); return; } } try { UpdateBuilder updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("NICKNAME", ((Player) source).getUsername()); - updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(this.needOldPass ? args[1] : args[0])); + updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, ((Player) source).getUsername()); + updateBuilder.updateColumnValue(RegisteredPlayer.HASH_FIELD, AuthSessionHandler.genHash(this.needOldPass ? args[1] : args[0])); updateBuilder.update(); - source.sendMessage(this.successful, MessageType.SYSTEM); + source.sendMessage(this.successful); } catch (SQLException e) { - source.sendMessage(this.errorOccurred, MessageType.SYSTEM); + source.sendMessage(this.errorOccurred); e.printStackTrace(); } } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } else { - source.sendMessage(this.notPlayer, MessageType.SYSTEM); + source.sendMessage(this.notPlayer); } } diff --git a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java index 53f460f..95e002c 100644 --- a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java @@ -24,7 +24,6 @@ import com.velocitypowered.api.proxy.Player; import net.elytrium.java.commons.mc.serialization.Serializer; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class DestroySessionCommand implements SimpleCommand { @@ -48,9 +47,9 @@ public class DestroySessionCommand implements SimpleCommand { if (source instanceof Player) { this.plugin.removePlayerFromCache(((Player) source).getUsername()); - source.sendMessage(this.successful, MessageType.SYSTEM); + source.sendMessage(this.successful); } else { - source.sendMessage(this.notPlayer, MessageType.SYSTEM); + source.sendMessage(this.notPlayer); } } diff --git a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java index 6704650..1186856 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java @@ -32,7 +32,6 @@ import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class ForceChangePasswordCommand implements SimpleCommand { @@ -72,20 +71,20 @@ public class ForceChangePasswordCommand implements SimpleCommand { Serializer serializer = LimboAuth.getSerializer(); try { UpdateBuilder updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("LOWERCASENICKNAME", nickname.toLowerCase(Locale.ROOT)); - updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(newPassword)); + updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, nickname.toLowerCase(Locale.ROOT)); + updateBuilder.updateColumnValue(RegisteredPlayer.HASH_FIELD, AuthSessionHandler.genHash(newPassword)); updateBuilder.update(); this.server.getPlayer(nickname) - .ifPresent(player -> player.sendMessage(serializer.deserialize(MessageFormat.format(this.message, newPassword)), MessageType.SYSTEM)); + .ifPresent(player -> player.sendMessage(serializer.deserialize(MessageFormat.format(this.message, newPassword)))); - source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, nickname)), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, nickname))); } catch (SQLException e) { - source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, nickname)), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, nickname))); e.printStackTrace(); } } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } diff --git a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java index f065d25..2fd12ca 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java @@ -30,7 +30,6 @@ import net.elytrium.java.commons.mc.velocity.commands.SuggestUtils; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class ForceUnregisterCommand implements SimpleCommand { @@ -74,13 +73,13 @@ public class ForceUnregisterCommand implements SimpleCommand { this.playerDao.deleteById(playerNick.toLowerCase(Locale.ROOT)); this.plugin.removePlayerFromCache(playerNick); this.server.getPlayer(playerNick).ifPresent(player -> player.disconnect(this.kick)); - source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, playerNick)), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, playerNick))); } catch (SQLException e) { - source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, playerNick)), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, playerNick))); e.printStackTrace(); } } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } diff --git a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java index ff31023..6d4a24c 100644 --- a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java @@ -26,7 +26,6 @@ import java.util.stream.Collectors; import net.elytrium.java.commons.mc.serialization.Serializer; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -103,10 +102,10 @@ public class LimboAuthCommand implements SimpleCommand { if (command.equalsIgnoreCase("reload") && source.hasPermission("limboauth.admin.reload")) { try { this.plugin.reload(); - source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD)); } catch (Exception e) { e.printStackTrace(); - source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED), MessageType.SYSTEM); + source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED)); } return; @@ -144,16 +143,16 @@ public class LimboAuthCommand implements SimpleCommand { private void showHelp(CommandSource source) { for (Component component : HELP_MESSAGE) { - source.sendMessage(component, MessageType.SYSTEM); + source.sendMessage(component); } List> availableSubcommands = SUBCOMMANDS.entrySet().stream() .filter(command -> source.hasPermission("limboauth.admin." + command.getKey())) .collect(Collectors.toList()); if (availableSubcommands.size() > 0) { - source.sendMessage(AVAILABLE_SUBCOMMANDS_MESSAGE, MessageType.SYSTEM); - availableSubcommands.forEach(command -> source.sendMessage(command.getValue(), MessageType.SYSTEM)); + source.sendMessage(AVAILABLE_SUBCOMMANDS_MESSAGE); + availableSubcommands.forEach(command -> source.sendMessage(command.getValue())); } else { - source.sendMessage(NO_AVAILABLE_SUBCOMMANDS_MESSAGE, MessageType.SYSTEM); + source.sendMessage(NO_AVAILABLE_SUBCOMMANDS_MESSAGE); } } } diff --git a/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java b/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java index cc73652..f4063d7 100644 --- a/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java @@ -23,12 +23,12 @@ import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; +import java.util.Locale; import net.elytrium.java.commons.mc.serialization.Serializer; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class PremiumCommand implements SimpleCommand { @@ -73,34 +73,34 @@ public class PremiumCommand implements SimpleCommand { String username = ((Player) source).getUsername(); RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); if (player == null) { - source.sendMessage(this.notRegistered, MessageType.SYSTEM); + source.sendMessage(this.notRegistered); } else if (player.getHash().isEmpty()) { - source.sendMessage(this.alreadyPremium, MessageType.SYSTEM); + source.sendMessage(this.alreadyPremium); } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { - if (this.plugin.isPremiumExternal(username)) { + if (this.plugin.isPremiumExternal(username.toLowerCase(Locale.ROOT)).getState() == LimboAuth.PremiumState.PREMIUM) { try { player.setHash(""); this.playerDao.update(player); this.plugin.removePlayerFromCache(username); ((Player) source).disconnect(this.successful); } catch (SQLException e) { - source.sendMessage(this.errorOccurred, MessageType.SYSTEM); + source.sendMessage(this.errorOccurred); e.printStackTrace(); } } else { - source.sendMessage(this.notPremium, MessageType.SYSTEM); + source.sendMessage(this.notPremium); } } else { - source.sendMessage(this.wrongPassword, MessageType.SYSTEM); + source.sendMessage(this.wrongPassword); } return; } } - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } else { - source.sendMessage(this.notPlayer, MessageType.SYSTEM); + source.sendMessage(this.notPlayer); } } diff --git a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java index 7609020..0ad71f5 100644 --- a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java @@ -36,7 +36,6 @@ import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -95,7 +94,7 @@ public class TotpCommand implements SimpleCommand { if (source instanceof Player) { if (args.length == 0) { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } else { String username = ((Player) source).getUsername(); @@ -105,32 +104,32 @@ public class TotpCommand implements SimpleCommand { if (this.needPassword ? args.length == 2 : args.length == 1) { playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username); if (playerInfo == null) { - source.sendMessage(this.notRegistered, MessageType.SYSTEM); + source.sendMessage(this.notRegistered); return; } else if (playerInfo.getHash().isEmpty()) { - source.sendMessage(this.crackedCommand, MessageType.SYSTEM); + source.sendMessage(this.crackedCommand); return; } else if (this.needPassword && !AuthSessionHandler.checkPassword(args[1], playerInfo, this.playerDao)) { - source.sendMessage(this.wrongPassword, MessageType.SYSTEM); + source.sendMessage(this.wrongPassword); return; } if (!playerInfo.getTotpToken().isEmpty()) { - source.sendMessage(this.alreadyEnabled, MessageType.SYSTEM); + source.sendMessage(this.alreadyEnabled); return; } String secret = this.secretGenerator.generate(); try { updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("NICKNAME", username); - updateBuilder.updateColumnValue("TOTPTOKEN", secret); + updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, username); + updateBuilder.updateColumnValue(RegisteredPlayer.TOTP_TOKEN_FIELD, secret); updateBuilder.update(); } catch (SQLException e) { - source.sendMessage(this.errorOccurred, MessageType.SYSTEM); + source.sendMessage(this.errorOccurred); e.printStackTrace(); } - source.sendMessage(this.successful, MessageType.SYSTEM); + source.sendMessage(this.successful); QrData data = new QrData.Builder() .label(username) @@ -138,50 +137,50 @@ public class TotpCommand implements SimpleCommand { .issuer(this.issuer) .build(); String qrUrl = this.qrGeneratorUrl.replace("{data}", URLEncoder.encode(data.getUri(), StandardCharsets.UTF_8)); - source.sendMessage(this.qr.clickEvent(ClickEvent.openUrl(qrUrl)), MessageType.SYSTEM); + source.sendMessage(this.qr.clickEvent(ClickEvent.openUrl(qrUrl))); Serializer serializer = LimboAuth.getSerializer(); source.sendMessage(serializer.deserialize(MessageFormat.format(this.token, secret)) - .clickEvent(ClickEvent.copyToClipboard(secret)), MessageType.SYSTEM); + .clickEvent(ClickEvent.copyToClipboard(secret))); String codes = String.join(", ", this.codesGenerator.generateCodes(this.recoveryCodesAmount)); source.sendMessage(serializer.deserialize(MessageFormat.format(this.recovery, codes)) - .clickEvent(ClickEvent.copyToClipboard(codes)), MessageType.SYSTEM); + .clickEvent(ClickEvent.copyToClipboard(codes))); } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } else if (args[0].equalsIgnoreCase("disable")) { if (args.length == 2) { playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username); if (playerInfo == null) { - source.sendMessage(this.notRegistered, MessageType.SYSTEM); + source.sendMessage(this.notRegistered); return; } if (AuthSessionHandler.getTotpCodeVerifier().isValidCode(playerInfo.getTotpToken(), args[1])) { try { updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("NICKNAME", username); - updateBuilder.updateColumnValue("TOTPTOKEN", ""); + updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, username); + updateBuilder.updateColumnValue(RegisteredPlayer.TOTP_TOKEN_FIELD, ""); updateBuilder.update(); - source.sendMessage(this.disabled, MessageType.SYSTEM); + source.sendMessage(this.disabled); } catch (SQLException e) { - source.sendMessage(this.errorOccurred, MessageType.SYSTEM); + source.sendMessage(this.errorOccurred); e.printStackTrace(); } } else { - source.sendMessage(this.wrong, MessageType.SYSTEM); + source.sendMessage(this.wrong); } } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } else { - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } } } else { - source.sendMessage(this.notPlayer, MessageType.SYSTEM); + source.sendMessage(this.notPlayer); } } diff --git a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java index b5ab2a0..b6c11ff 100644 --- a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java @@ -30,7 +30,6 @@ import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.event.AuthUnregisterEvent; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.text.Component; public class UnregisterCommand implements SimpleCommand { @@ -73,9 +72,9 @@ public class UnregisterCommand implements SimpleCommand { String username = ((Player) source).getUsername(); RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); if (player == null) { - source.sendMessage(this.notRegistered, MessageType.SYSTEM); + source.sendMessage(this.notRegistered); } else if (player.getHash().isEmpty()) { - source.sendMessage(this.crackedCommand, MessageType.SYSTEM); + source.sendMessage(this.crackedCommand); } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { try { this.plugin.getServer().getEventManager().fireAndForget(new AuthUnregisterEvent(username)); @@ -83,20 +82,20 @@ public class UnregisterCommand implements SimpleCommand { this.plugin.removePlayerFromCache(username); ((Player) source).disconnect(this.successful); } catch (SQLException e) { - source.sendMessage(this.errorOccurred, MessageType.SYSTEM); + source.sendMessage(this.errorOccurred); e.printStackTrace(); } } else { - source.sendMessage(this.wrongPassword, MessageType.SYSTEM); + source.sendMessage(this.wrongPassword); } return; } } - source.sendMessage(this.usage, MessageType.SYSTEM); + source.sendMessage(this.usage); } else { - source.sendMessage(this.notPlayer, MessageType.SYSTEM); + source.sendMessage(this.notPlayer); } } -- cgit