diff options
Diffstat (limited to 'src/main/java/net/elytrium/limboauth/command')
8 files changed, 297 insertions, 258 deletions
diff --git a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java index 806add0..f280053 100644 --- a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java @@ -24,36 +24,38 @@ import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; +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.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class ChangePasswordCommand implements SimpleCommand { private final Dao<RegisteredPlayer, String> playerDao; - private final Component notPlayer; private final boolean needOldPass; private final Component notRegistered; + private final Component crackedCommand; private final Component wrongPassword; private final Component successful; private final Component errorOccurred; private final Component usage; - private final Component crackedCommand; + private final Component notPlayer; public ChangePasswordCommand(Dao<RegisteredPlayer, String> playerDao) { this.playerDao = playerDao; - this.notPlayer = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); + Serializer serializer = LimboAuth.getSerializer(); this.needOldPass = Settings.IMP.MAIN.CHANGE_PASSWORD_NEED_OLD_PASSWORD; - this.notRegistered = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); - this.wrongPassword = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); - this.successful = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.CHANGE_PASSWORD_SUCCESSFUL); - this.errorOccurred = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.CHANGE_PASSWORD_USAGE); - this.crackedCommand = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); + this.notRegistered = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); + this.crackedCommand = serializer.deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); + this.wrongPassword = serializer.deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); + this.successful = serializer.deserialize(Settings.IMP.MAIN.STRINGS.CHANGE_PASSWORD_SUCCESSFUL); + this.errorOccurred = serializer.deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); + this.usage = serializer.deserialize(Settings.IMP.MAIN.STRINGS.CHANGE_PASSWORD_USAGE); + this.notPlayer = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); } @Override @@ -61,45 +63,42 @@ public class ChangePasswordCommand implements SimpleCommand { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if (!(source instanceof Player)) { - source.sendMessage(this.notPlayer); - return; - } - - if (this.needOldPass ? args.length == 2 : args.length == 1) { - if (this.needOldPass) { - RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, ((Player) source).getUsername()); - if (player == null) { - source.sendMessage(this.notRegistered); - return; - } else if (player.getHash().isEmpty()) { - source.sendMessage(this.crackedCommand); - } else if (!AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { - source.sendMessage(this.wrongPassword); - return; + if (source instanceof Player) { + if (this.needOldPass ? args.length == 2 : args.length == 1) { + if (this.needOldPass) { + RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, ((Player) source).getUsername()); + if (player == null) { + source.sendMessage(this.notRegistered); + return; + } else if (player.getHash().isEmpty()) { + source.sendMessage(this.crackedCommand); + } else if (!AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { + source.sendMessage(this.wrongPassword); + return; + } } - } - try { - UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("NICKNAME", ((Player) source).getUsername()); - updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(this.needOldPass ? args[1] : args[0])); - updateBuilder.update(); + try { + UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder(); + updateBuilder.where().eq("NICKNAME", ((Player) source).getUsername()); + updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(this.needOldPass ? args[1] : args[0])); + updateBuilder.update(); - source.sendMessage(this.successful); - } catch (SQLException e) { - source.sendMessage(this.errorOccurred); - e.printStackTrace(); + source.sendMessage(this.successful); + } catch (SQLException e) { + source.sendMessage(this.errorOccurred); + e.printStackTrace(); + } + } else { + source.sendMessage(this.usage); } - - return; + } else { + source.sendMessage(this.notPlayer); } - - source.sendMessage(this.usage); } @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.changepassword") != Tristate.FALSE; + return invocation.source().getPermissionValue("limboauth.commands.changepassword") == Tristate.TRUE; } } diff --git a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java index 5cabe9b..95e002c 100644 --- a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java @@ -21,40 +21,40 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.permission.Tristate; 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.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class DestroySessionCommand implements SimpleCommand { private final LimboAuth plugin; - private final Component notPlayer; private final Component successful; + private final Component notPlayer; public DestroySessionCommand(LimboAuth plugin) { this.plugin = plugin; - this.notPlayer = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); - this.successful = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.DESTROY_SESSION_SUCCESSFUL); + Serializer serializer = LimboAuth.getSerializer(); + this.successful = serializer.deserialize(Settings.IMP.MAIN.STRINGS.DESTROY_SESSION_SUCCESSFUL); + this.notPlayer = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); } @Override public void execute(SimpleCommand.Invocation invocation) { CommandSource source = invocation.source(); - if (!(source instanceof Player)) { + if (source instanceof Player) { + this.plugin.removePlayerFromCache(((Player) source).getUsername()); + source.sendMessage(this.successful); + } else { source.sendMessage(this.notPlayer); - return; } - - this.plugin.removePlayerFromCache(((Player) source).getUsername()); - source.sendMessage(this.successful); } @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.destroysession") != Tristate.FALSE; + return invocation.source().getPermissionValue("limboauth.commands.destroysession") == Tristate.TRUE; } } diff --git a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java index dd2ee4d..2d47b61 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java @@ -26,12 +26,13 @@ import java.sql.SQLException; import java.text.MessageFormat; import java.util.List; import java.util.Locale; +import net.elytrium.java.commons.mc.serialization.Serializer; +import net.elytrium.java.commons.mc.velocity.commands.SuggestUtils; +import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; -import net.elytrium.limboauth.utils.SuggestUtils; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class ForceChangePasswordCommand implements SimpleCommand { @@ -50,12 +51,12 @@ public class ForceChangePasswordCommand implements SimpleCommand { this.message = Settings.IMP.MAIN.STRINGS.FORCE_CHANGE_PASSWORD_MESSAGE; this.successful = Settings.IMP.MAIN.STRINGS.FORCE_CHANGE_PASSWORD_SUCCESSFUL; this.notSuccessful = Settings.IMP.MAIN.STRINGS.FORCE_CHANGE_PASSWORD_NOT_SUCCESSFUL; - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.FORCE_CHANGE_PASSWORD_USAGE); + this.usage = LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.FORCE_CHANGE_PASSWORD_USAGE); } @Override public List<String> suggest(SimpleCommand.Invocation invocation) { - return SuggestUtils.suggestPlayers(invocation.arguments(), this.server); + return SuggestUtils.suggestPlayers(this.server, invocation.arguments(), 0); } @Override @@ -67,26 +68,23 @@ public class ForceChangePasswordCommand implements SimpleCommand { String nickname = args[0]; String newPassword = args[1]; + Serializer serializer = LimboAuth.getSerializer(); try { UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder(); updateBuilder.where().eq("LOWERCASENICKNAME", nickname.toLowerCase(Locale.ROOT)); updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(newPassword)); updateBuilder.update(); - this.server.getPlayer(nickname).ifPresent(player -> - player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.message, newPassword))) - ); + this.server.getPlayer(nickname).ifPresent(player -> player.sendMessage(serializer.deserialize(MessageFormat.format(this.message, newPassword)))); - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.successful, nickname))); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, nickname))); } catch (SQLException e) { - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.notSuccessful, nickname))); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, nickname))); e.printStackTrace(); } - - return; + } else { + source.sendMessage(this.usage); } - - source.sendMessage(this.usage); } @Override diff --git a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java index dabe70b..2fd12ca 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java @@ -25,12 +25,12 @@ import java.sql.SQLException; import java.text.MessageFormat; import java.util.List; import java.util.Locale; +import net.elytrium.java.commons.mc.serialization.Serializer; +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.elytrium.limboauth.utils.SuggestUtils; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class ForceUnregisterCommand implements SimpleCommand { @@ -48,15 +48,16 @@ public class ForceUnregisterCommand implements SimpleCommand { this.server = server; this.playerDao = playerDao; - this.kick = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_KICK); + Serializer serializer = LimboAuth.getSerializer(); + this.kick = serializer.deserialize(Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_KICK); this.successful = Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_SUCCESSFUL; this.notSuccessful = Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_NOT_SUCCESSFUL; - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_USAGE); + this.usage = serializer.deserialize(Settings.IMP.MAIN.STRINGS.FORCE_UNREGISTER_USAGE); } @Override public List<String> suggest(SimpleCommand.Invocation invocation) { - return SuggestUtils.suggestPlayers(invocation.arguments(), this.server); + return SuggestUtils.suggestPlayers(this.server, invocation.arguments(), 0); } @Override @@ -67,20 +68,19 @@ public class ForceUnregisterCommand implements SimpleCommand { if (args.length == 1) { String playerNick = args[0]; + Serializer serializer = LimboAuth.getSerializer(); try { this.playerDao.deleteById(playerNick.toLowerCase(Locale.ROOT)); this.plugin.removePlayerFromCache(playerNick); this.server.getPlayer(playerNick).ifPresent(player -> player.disconnect(this.kick)); - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.successful, playerNick))); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, playerNick))); } catch (SQLException e) { - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.notSuccessful, playerNick))); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.notSuccessful, playerNick))); e.printStackTrace(); } - - return; + } else { + source.sendMessage(this.usage); } - - source.sendMessage(this.usage); } @Override diff --git a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java index 49a5079..3dd4c8d 100644 --- a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java @@ -21,15 +21,44 @@ import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.Stream; +import net.elytrium.java.commons.mc.serialization.Serializer; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.format.NamedTextColor; public class LimboAuthCommand implements SimpleCommand { + private static final List<Component> HELP_MESSAGE = List.of( + Component.text("This server is using LimboAuth and LimboAPI.", NamedTextColor.YELLOW), + Component.text("(C) 2021 - 2022 Elytrium", NamedTextColor.YELLOW), + Component.text("https://elytrium.net/github/", NamedTextColor.GREEN), + Component.empty() + ); + private static final Map<String, Component> SUBCOMMANDS = Map.of( + /* + "serverstats", Component.textOfChildren( + Component.text(" /limboauth serverstats", NamedTextColor.GREEN), + Component.text(" - ", NamedTextColor.DARK_GRAY), + Component.text("Query for server stats.", NamedTextColor.YELLOW) + ), + "playerstats", Component.textOfChildren( + Component.text(" /limboauth playerstats <player>", NamedTextColor.GREEN), + Component.text(" - ", NamedTextColor.DARK_GRAY), + Component.text("Query for stats of specified player.", NamedTextColor.YELLOW) + ), + */ + "reload", Component.textOfChildren( + Component.text(" /limboauth reload", NamedTextColor.GREEN), + Component.text(" - ", NamedTextColor.DARK_GRAY), + Component.text("Reload config.", NamedTextColor.YELLOW) + ) + ); + private static final Component AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("Available subcommands:", NamedTextColor.WHITE); + private static final Component NO_AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("There is no available subcommands for you.", NamedTextColor.WHITE); + private final LimboAuth plugin; public LimboAuthCommand(LimboAuth plugin) { @@ -42,17 +71,22 @@ public class LimboAuthCommand implements SimpleCommand { String[] args = invocation.arguments(); if (args.length == 0) { - return this.getSubCommands() + return SUBCOMMANDS.keySet().stream() .filter(cmd -> source.hasPermission("limboauth.admin." + cmd)) .collect(Collectors.toList()); } else if (args.length == 1) { - return this.getSubCommands() + String argument = args[0]; + return SUBCOMMANDS.keySet().stream() .filter(cmd -> source.hasPermission("limboauth.admin." + cmd)) - .filter(str -> str.regionMatches(true, 0, args[0], 0, args[0].length())) + .filter(str -> str.regionMatches(true, 0, argument, 0, argument.length())) .collect(Collectors.toList()); + /* + } else if (args[0].equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) { + return SuggestUtils.suggestPlayers(this.plugin.getServer(), args, 2); + */ + } else { + return ImmutableList.of(); } - - return ImmutableList.of(); } @Override @@ -60,40 +94,63 @@ public class LimboAuthCommand implements SimpleCommand { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if (args.length == 1) { - if (args[0].equalsIgnoreCase("reload") && source.hasPermission("limboauth.admin.reload")) { - try { - this.plugin.reload(); - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD)); - } catch (Exception e) { - source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED)); - e.printStackTrace(); + int argsAmount = args.length; + if (argsAmount > 0) { + String command = args[0]; + Serializer serializer = LimboAuth.getSerializer(); + if (argsAmount == 1) { + if (command.equalsIgnoreCase("reload") && source.hasPermission("limboauth.admin.reload")) { + try { + this.plugin.reload(); + source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD)); + } catch (Exception e) { + e.printStackTrace(); + source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED)); + } + + return; + } + /* + else if (command.equalsIgnoreCase("serverstats") && source.hasPermission("limboauth.admin.serverstats")) { + return; + } else if (command.equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) { + source.sendMessage(Component.text("Please specify a player.")); + return; } + */ + } + /* + else if (argsAmount == 2) { + if (command.equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) { + RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.plugin.getPlayerDao(), args[1]); + if (player == null) { + source.sendMessage(Component.text("Игрок даезент екзистс.")); + } else { + source.sendMessage(Component.text("Стата геймера под ником {player}:")); + source.sendMessage(Component.empty()); + source.sendMessage(Component.text("Ласт айпи: " + player.getIP())); + source.sendMessage(Component.text("2fa: " + (player.getTotpToken().isEmpty() ? "Нет" : "Есть"))); + } - return; + return; + } } + */ } this.showHelp(source); } private void showHelp(CommandSource source) { - source.sendMessage(Component.text("§eThis server is using LimboAuth and LimboAPI")); - source.sendMessage(Component.text("§e(С) 2021 - 2022 Elytrium")); - source.sendMessage(Component.text("§ahttps://ely.su/github/")); - source.sendMessage(Component.text("§r")); - source.sendMessage(Component.text("§fAvailable subcommands:")); - // Java moment - this.getSubCommands() - .filter(cmd -> source.hasPermission("limboauth.admin." + cmd)) - .forEach(cmd -> { - if (cmd.equals("reload")) { - source.sendMessage(Component.text(" §a/limboauth reload §8- §eReload config")); - } - }); - } - - private Stream<String> getSubCommands() { - return Stream.of("reload"); + HELP_MESSAGE.forEach(source::sendMessage); + List<Map.Entry<String, Component>> availableSubcommands = SUBCOMMANDS.entrySet().stream() + .filter(command -> source.hasPermission("limboauth.admin." + command.getKey())) + .collect(Collectors.toList()); + if (availableSubcommands.size() > 0) { + source.sendMessage(AVAILABLE_SUBCOMMANDS_MESSAGE); + availableSubcommands.forEach(command -> source.sendMessage(command.getValue())); + } else { + 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 fd62a00..78674d0 100644 --- a/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java @@ -23,19 +23,19 @@ import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; +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.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class PremiumCommand implements SimpleCommand { private final LimboAuth plugin; private final Dao<RegisteredPlayer, String> playerDao; - private final Component notPlayer; + private final String confirmKeyword; private final Component notRegistered; private final Component alreadyPremium; private final Component successful; @@ -43,19 +43,22 @@ public class PremiumCommand implements SimpleCommand { private final Component notPremium; private final Component wrongPassword; private final Component usage; + private final Component notPlayer; public PremiumCommand(LimboAuth plugin, Dao<RegisteredPlayer, String> playerDao) { this.plugin = plugin; this.playerDao = playerDao; - this.notPlayer = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); - this.notRegistered = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); - this.alreadyPremium = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.ALREADY_PREMIUM); - this.successful = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.PREMIUM_SUCCESSFUL); - this.errorOccurred = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); - this.notPremium = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PREMIUM); - this.wrongPassword = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.PREMIUM_USAGE); + Serializer serializer = LimboAuth.getSerializer(); + this.confirmKeyword = Settings.IMP.MAIN.CONFIRM_KEYWORD; + this.notRegistered = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); + this.alreadyPremium = serializer.deserialize(Settings.IMP.MAIN.STRINGS.ALREADY_PREMIUM); + this.successful = serializer.deserialize(Settings.IMP.MAIN.STRINGS.PREMIUM_SUCCESSFUL); + this.errorOccurred = serializer.deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); + this.notPremium = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PREMIUM); + this.wrongPassword = serializer.deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); + this.usage = serializer.deserialize(Settings.IMP.MAIN.STRINGS.PREMIUM_USAGE); + this.notPlayer = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); } @Override @@ -63,46 +66,45 @@ public class PremiumCommand implements SimpleCommand { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if (!(source instanceof Player)) { - source.sendMessage(this.notPlayer); - return; - } - - if (args.length == 2) { - if (args[1].equalsIgnoreCase("confirm")) { - String username = ((Player) source).getUsername(); - RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); - if (player == null) { - source.sendMessage(this.notRegistered); - } else if (player.getHash().isEmpty()) { - source.sendMessage(this.alreadyPremium); - } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { - if (this.plugin.isPremiumExternal(username)) { - try { - player.setHash(""); - this.playerDao.update(player); - this.plugin.removePlayerFromCache(username); - ((Player) source).disconnect(this.successful); - } catch (SQLException e) { - source.sendMessage(this.errorOccurred); - e.printStackTrace(); + if (source instanceof Player) { + if (args.length == 2) { + if (this.confirmKeyword.equalsIgnoreCase(args[1])) { + String username = ((Player) source).getUsername(); + RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); + if (player == null) { + source.sendMessage(this.notRegistered); + } else if (player.getHash().isEmpty()) { + source.sendMessage(this.alreadyPremium); + } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { + if (this.plugin.isPremiumExternal(username)) { + try { + player.setHash(""); + this.playerDao.update(player); + this.plugin.removePlayerFromCache(username); + ((Player) source).disconnect(this.successful); + } catch (SQLException e) { + source.sendMessage(this.errorOccurred); + e.printStackTrace(); + } + } else { + source.sendMessage(this.notPremium); } } else { - source.sendMessage(this.notPremium); + source.sendMessage(this.wrongPassword); } - } else { - source.sendMessage(this.wrongPassword); - } - return; + return; + } } - } - source.sendMessage(this.usage); + source.sendMessage(this.usage); + } else { + source.sendMessage(this.notPlayer); + } } @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.premium") != Tristate.FALSE; + return invocation.source().getPermissionValue("limboauth.commands.premium") == Tristate.TRUE; } } diff --git a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java index e9693ae..54d9c3e 100644 --- a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java @@ -31,12 +31,13 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.text.MessageFormat; +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.text.Component; import net.kyori.adventure.text.event.ClickEvent; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class TotpCommand implements SimpleCommand { @@ -65,52 +66,49 @@ public class TotpCommand implements SimpleCommand { public TotpCommand(Dao<RegisteredPlayer, String> playerDao) { this.playerDao = playerDao; - this.notPlayer = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_USAGE); + Serializer serializer = LimboAuth.getSerializer(); + this.notPlayer = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); + this.usage = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_USAGE); this.needPassword = Settings.IMP.MAIN.TOTP_NEED_PASSWORD; - this.notRegistered = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); - this.wrongPassword = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); - this.alreadyEnabled = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_ALREADY_ENABLED); - this.errorOccurred = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); - this.successful = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_SUCCESSFUL); + this.notRegistered = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); + this.wrongPassword = serializer.deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); + this.alreadyEnabled = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_ALREADY_ENABLED); + this.errorOccurred = serializer.deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); + this.successful = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_SUCCESSFUL); this.issuer = Settings.IMP.MAIN.TOTP_ISSUER; this.qrGeneratorUrl = Settings.IMP.MAIN.QR_GENERATOR_URL; - this.qr = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_QR); + this.qr = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_QR); this.token = Settings.IMP.MAIN.STRINGS.TOTP_TOKEN; this.recoveryCodesAmount = Settings.IMP.MAIN.TOTP_RECOVERY_CODES_AMOUNT; this.recovery = Settings.IMP.MAIN.STRINGS.TOTP_RECOVERY; - this.disabled = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_DISABLED); - this.wrong = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.TOTP_WRONG); - this.crackedCommand = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); + this.disabled = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_DISABLED); + this.wrong = serializer.deserialize(Settings.IMP.MAIN.STRINGS.TOTP_WRONG); + this.crackedCommand = serializer.deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); } + // TODO: Rewrite. @Override public void execute(SimpleCommand.Invocation invocation) { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if (!(source instanceof Player)) { - source.sendMessage(this.notPlayer); - return; - } + if (source instanceof Player) { + if (args.length == 0) { + source.sendMessage(this.usage); + } else { + String username = ((Player) source).getUsername(); - if (args.length == 0) { - source.sendMessage(this.usage); - } else { - String username = ((Player) source).getUsername(); - - RegisteredPlayer playerInfo; - UpdateBuilder<RegisteredPlayer, String> updateBuilder; - switch (args[0]) { - case "enable": { + RegisteredPlayer playerInfo; + UpdateBuilder<RegisteredPlayer, String> updateBuilder; + if (args[0].equalsIgnoreCase("enable")) { if (this.needPassword ? args.length == 2 : args.length == 1) { playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username); - if (playerInfo == null) { source.sendMessage(this.notRegistered); return; } else if (playerInfo.getHash().isEmpty()) { source.sendMessage(this.crackedCommand); + return; } else if (this.needPassword && !AuthSessionHandler.checkPassword(args[1], playerInfo, this.playerDao)) { source.sendMessage(this.wrongPassword); return; @@ -122,7 +120,6 @@ public class TotpCommand implements SimpleCommand { } String secret = this.secretGenerator.generate(); - try { updateBuilder = this.playerDao.updateBuilder(); updateBuilder.where().eq("NICKNAME", username); @@ -132,7 +129,6 @@ public class TotpCommand implements SimpleCommand { source.sendMessage(this.errorOccurred); e.printStackTrace(); } - source.sendMessage(this.successful); QrData data = new QrData.Builder() @@ -140,69 +136,54 @@ public class TotpCommand implements SimpleCommand { .secret(secret) .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))); - source.sendMessage( - LegacyComponentSerializer.legacyAmpersand().deserialize( - MessageFormat.format(this.token, secret) - ).clickEvent(ClickEvent.copyToClipboard(secret)) - ); - + Serializer serializer = LimboAuth.getSerializer(); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.token, secret)).clickEvent(ClickEvent.copyToClipboard(secret))); String codes = String.join(", ", this.codesGenerator.generateCodes(this.recoveryCodesAmount)); - - source.sendMessage( - LegacyComponentSerializer.legacyAmpersand().deserialize( - MessageFormat.format(this.recovery, codes) - ).clickEvent(ClickEvent.copyToClipboard(codes)) - ); + source.sendMessage(serializer.deserialize(MessageFormat.format(this.recovery, codes)).clickEvent(ClickEvent.copyToClipboard(codes))); } else { source.sendMessage(this.usage); } - break; - } - case "disable": { - if (args.length != 2) { - source.sendMessage(this.usage); - return; - } - - playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username); - - if (playerInfo == null) { - source.sendMessage(this.notRegistered); - return; - } + } else if (args[0].equalsIgnoreCase("disable")) { + if (args.length == 2) { + playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username); - if (AuthSessionHandler.getVerifier().isValidCode(playerInfo.getTotpToken(), args[1])) { - try { - updateBuilder = this.playerDao.updateBuilder(); - updateBuilder.where().eq("NICKNAME", username); - updateBuilder.updateColumnValue("TOTPTOKEN", ""); - updateBuilder.update(); + if (playerInfo == null) { + source.sendMessage(this.notRegistered); + return; + } - source.sendMessage(this.disabled); - } catch (SQLException e) { - source.sendMessage(this.errorOccurred); - e.printStackTrace(); + if (AuthSessionHandler.getTotpCodeVerifier().isValidCode(playerInfo.getTotpToken(), args[1])) { + try { + updateBuilder = this.playerDao.updateBuilder(); + updateBuilder.where().eq("NICKNAME", username); + updateBuilder.updateColumnValue("TOTPTOKEN", ""); + updateBuilder.update(); + + source.sendMessage(this.disabled); + } catch (SQLException e) { + source.sendMessage(this.errorOccurred); + e.printStackTrace(); + } + } else { + source.sendMessage(this.wrong); } } else { - source.sendMessage(this.wrong); + source.sendMessage(this.usage); } - break; - } - default: { + } else { source.sendMessage(this.usage); - break; } } + } else { + source.sendMessage(this.notPlayer); } } @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.totp") != Tristate.FALSE; + return invocation.source().getPermissionValue("limboauth.commands.totp") == Tristate.TRUE; } } diff --git a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java index 62edd0b..b6c11ff 100644 --- a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java @@ -24,19 +24,20 @@ 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.event.AuthUnregisterEvent; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.model.RegisteredPlayer; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public class UnregisterCommand implements SimpleCommand { private final LimboAuth plugin; private final Dao<RegisteredPlayer, String> playerDao; + private final String confirmKeyword; private final Component notPlayer; private final Component notRegistered; private final Component successful; @@ -49,13 +50,15 @@ public class UnregisterCommand implements SimpleCommand { this.plugin = plugin; this.playerDao = playerDao; - this.notPlayer = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); - this.notRegistered = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); - this.successful = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.UNREGISTER_SUCCESSFUL); - this.errorOccurred = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); - this.wrongPassword = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); - this.usage = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.UNREGISTER_USAGE); - this.crackedCommand = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); + Serializer serializer = LimboAuth.getSerializer(); + this.confirmKeyword = Settings.IMP.MAIN.CONFIRM_KEYWORD; + this.notPlayer = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_PLAYER); + this.notRegistered = serializer.deserialize(Settings.IMP.MAIN.STRINGS.NOT_REGISTERED); + this.successful = serializer.deserialize(Settings.IMP.MAIN.STRINGS.UNREGISTER_SUCCESSFUL); + this.errorOccurred = serializer.deserialize(Settings.IMP.MAIN.STRINGS.ERROR_OCCURRED); + this.wrongPassword = serializer.deserialize(Settings.IMP.MAIN.STRINGS.WRONG_PASSWORD); + this.usage = serializer.deserialize(Settings.IMP.MAIN.STRINGS.UNREGISTER_USAGE); + this.crackedCommand = serializer.deserialize(Settings.IMP.MAIN.STRINGS.CRACKED_COMMAND); } @Override @@ -63,42 +66,41 @@ public class UnregisterCommand implements SimpleCommand { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if (!(source instanceof Player)) { - source.sendMessage(this.notPlayer); - return; - } - - if (args.length == 2) { - if (args[1].equalsIgnoreCase("confirm")) { - String username = ((Player) source).getUsername(); - RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); - if (player == null) { - source.sendMessage(this.notRegistered); - } else if (player.getHash().isEmpty()) { - source.sendMessage(this.crackedCommand); - } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { - try { - this.plugin.getServer().getEventManager().fireAndForget(new AuthUnregisterEvent(username)); - this.playerDao.deleteById(username.toLowerCase(Locale.ROOT)); - this.plugin.removePlayerFromCache(username); - ((Player) source).disconnect(this.successful); - } catch (SQLException e) { - source.sendMessage(this.errorOccurred); - e.printStackTrace(); + if (source instanceof Player) { + if (args.length == 2) { + if (this.confirmKeyword.equalsIgnoreCase(args[1])) { + String username = ((Player) source).getUsername(); + RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username); + if (player == null) { + source.sendMessage(this.notRegistered); + } else if (player.getHash().isEmpty()) { + source.sendMessage(this.crackedCommand); + } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) { + try { + this.plugin.getServer().getEventManager().fireAndForget(new AuthUnregisterEvent(username)); + this.playerDao.deleteById(username.toLowerCase(Locale.ROOT)); + this.plugin.removePlayerFromCache(username); + ((Player) source).disconnect(this.successful); + } catch (SQLException e) { + source.sendMessage(this.errorOccurred); + e.printStackTrace(); + } + } else { + source.sendMessage(this.wrongPassword); } - } else { - source.sendMessage(this.wrongPassword); - } - return; + return; + } } - } - source.sendMessage(this.usage); + source.sendMessage(this.usage); + } else { + source.sendMessage(this.notPlayer); + } } @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.unregister") != Tristate.FALSE; + return invocation.source().getPermissionValue("limboauth.commands.unregister") == Tristate.TRUE; } } |
