From 510e6bfa78d110b4ecbf8f74ab2d60ce614b8d55 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 10 May 2023 20:53:20 -0400 Subject: [PATCH] Add account list command --- .../command/commands/AccountCommand.java | 308 +++++++++--------- 1 file changed, 163 insertions(+), 145 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java index 9fb70024a..5591e2189 100644 --- a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java @@ -1,145 +1,163 @@ -package emu.grasscutter.command.commands; - -import static emu.grasscutter.utils.Language.translate; - -import at.favre.lib.crypto.bcrypt.BCrypt; -import emu.grasscutter.Grasscutter; -import emu.grasscutter.command.Command; -import emu.grasscutter.command.CommandHandler; -import emu.grasscutter.config.Configuration; -import emu.grasscutter.database.DatabaseHelper; -import emu.grasscutter.game.Account; -import emu.grasscutter.game.player.Player; -import java.util.List; - -@Command( - label = "account", - usage = { - "create []", // Only with EXPERIMENTAL_RealPassword == false - "delete ", - "create []", // Only with EXPERIMENTAL_RealPassword == true - "resetpass " - }, // Only with EXPERIMENTAL_RealPassword == true - targetRequirement = Command.TargetRequirement.NONE) -public final class AccountCommand implements CommandHandler { - @Override - public void execute(Player sender, Player targetPlayer, List args) { - if (sender != null) { - CommandHandler.sendTranslatedMessage(sender, "commands.generic.console_execute_error"); - return; - } - - if (args.size() < 2) { - sendUsageMessage(sender); - return; - } - - String action = args.get(0); - String username = args.get(1); - - switch (action) { - default: - sendUsageMessage(sender); - return; - case "create": - int uid = 0; - String password = ""; - - if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { - if (args.size() < 3) { - CommandHandler.sendMessage( - sender, "EXPERIMENTAL_RealPassword requires a password argument"); - CommandHandler.sendMessage(sender, "Usage: account create [uid]"); - return; - } - password = args.get(2); - - if (args.size() == 4) { - try { - uid = Integer.parseInt(args.get(3)); - } catch (NumberFormatException ignored) { - CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid")); - if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { - CommandHandler.sendMessage( - sender, - "EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid"); - CommandHandler.sendMessage( - sender, "Usage: account create [uid]"); - } - return; - } - } - } else { - if (args.size() > 2) { - try { - uid = Integer.parseInt(args.get(2)); - } catch (NumberFormatException ignored) { - CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid")); - return; - } - } - } - - emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid); - if (account == null) { - CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists")); - return; - } else { - if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { - account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray())); - } - account.addPermission("*"); - account.save(); // Save account to database. - - CommandHandler.sendMessage( - sender, translate(sender, "commands.account.create", account.getReservedPlayerUid())); - } - return; - case "delete": - // Get the account we want to delete. - Account toDelete = DatabaseHelper.getAccountByName(username); - - if (toDelete == null) { - CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account")); - return; - } - - DatabaseHelper.deleteAccount(toDelete); - CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete")); - return; - case "resetpass": - if (!Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { - CommandHandler.sendMessage( - sender, "resetpass requires EXPERIMENTAL_RealPassword to be true."); - return; - } - - if (args.size() != 3) { - CommandHandler.sendMessage(sender, "Invalid Args"); - CommandHandler.sendMessage(sender, "Usage: account resetpass "); - return; - } - - Account toUpdate = DatabaseHelper.getAccountByName(username); - - if (toUpdate == null) { - CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account")); - return; - } - - // Make sure player can't stay logged in with old password. - kickAccount(toUpdate); - - toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray())); - toUpdate.save(); - CommandHandler.sendMessage(sender, "Password Updated."); - } - } - - private void kickAccount(Account account) { - Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId()); - if (player != null) { - player.getSession().close(); - } - } -} +package emu.grasscutter.command.commands; + +import static emu.grasscutter.utils.Language.translate; + +import at.favre.lib.crypto.bcrypt.BCrypt; +import emu.grasscutter.Grasscutter; +import emu.grasscutter.command.Command; +import emu.grasscutter.command.CommandHandler; +import emu.grasscutter.config.Configuration; +import emu.grasscutter.database.DatabaseHelper; +import emu.grasscutter.database.DatabaseManager; +import emu.grasscutter.game.Account; +import emu.grasscutter.game.player.Player; +import java.util.List; +import java.util.stream.Collectors; + +@Command( + label = "account", + usage = { + "create []", // Only with EXPERIMENTAL_RealPassword == false + "delete ", + "create []", // Only with EXPERIMENTAL_RealPassword == true + "resetpass " + }, // Only with EXPERIMENTAL_RealPassword == true + targetRequirement = Command.TargetRequirement.NONE) +public final class AccountCommand implements CommandHandler { + @Override + public void execute(Player sender, Player targetPlayer, List args) { + if (sender != null) { + CommandHandler.sendTranslatedMessage(sender, "commands.generic.console_execute_error"); + return; + } + + if (args.size() < 2) { + sendUsageMessage(sender); + return; + } + + String action = args.get(0); + String username = args.get(1); + + switch (action) { + default -> this.sendUsageMessage(sender); + case "create" -> { + int uid = 0; + String password = ""; + if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { + if (args.size() < 3) { + CommandHandler.sendMessage( + sender, "EXPERIMENTAL_RealPassword requires a password argument"); + CommandHandler.sendMessage(sender, "Usage: account create [uid]"); + return; + } + password = args.get(2); + + if (args.size() == 4) { + try { + uid = Integer.parseInt(args.get(3)); + } catch (NumberFormatException ignored) { + CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid")); + if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { + CommandHandler.sendMessage( + sender, + "EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid"); + CommandHandler.sendMessage( + sender, "Usage: account create [uid]"); + } + return; + } + } + } else { + if (args.size() > 2) { + try { + uid = Integer.parseInt(args.get(2)); + } catch (NumberFormatException ignored) { + CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid")); + return; + } + } + } + Account account = DatabaseHelper.createAccountWithUid(username, uid); + if (account == null) { + CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists")); + return; + } else { + if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { + account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray())); + } + account.addPermission("*"); + account.save(); // Save account to database. + + CommandHandler.sendMessage( + sender, translate(sender, "commands.account.create", account.getReservedPlayerUid())); + } + } + case "delete" -> { + // Get the account we want to delete. + Account toDelete = DatabaseHelper.getAccountByName(username); + if (toDelete == null) { + CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account")); + return; + } + DatabaseHelper.deleteAccount(toDelete); + CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete")); + } + case "resetpass" -> { + if (!Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) { + CommandHandler.sendMessage( + sender, "resetpass requires EXPERIMENTAL_RealPassword to be true."); + return; + } + if (args.size() != 3) { + CommandHandler.sendMessage(sender, "Invalid Args"); + CommandHandler.sendMessage(sender, "Usage: account resetpass "); + return; + } + Account toUpdate = DatabaseHelper.getAccountByName(username); + if (toUpdate == null) { + CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account")); + return; + } + + // Make sure player can't stay logged in with old password. + kickAccount(toUpdate); + toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray())); + toUpdate.save(); + CommandHandler.sendMessage(sender, "Password Updated."); + } + case "list" -> { + CommandHandler.sendMessage(sender, "Note: This command might take a while to complete."); + CommandHandler.sendMessage(sender, + "Accounts: \n" + DatabaseManager.getAccountDatastore() + .find(Account.class).stream() + .map(acc -> "%s: %s (%s)".formatted( + acc.getId(), acc.getUsername(), + acc.getReservedPlayerUid() == 0 ? + this.getPlayerUid(acc) : + acc.getReservedPlayerUid())) + .collect(Collectors.joining("\n")) + ); + } + } + } + + /** + * Returns the UID of the player associated with the given account. + * If the player is not found, returns "no UID". + * + * @param account The account to get the UID of. + * @return The UID of the player associated with the given account. + */ + private String getPlayerUid(Account account) { + var player = DatabaseHelper.getPlayerByAccount(account, Player.class); + return player == null ? "no UID" : String.valueOf(player.getUid()); + } + + private void kickAccount(Account account) { + Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId()); + if (player != null) { + player.getSession().close(); + } + } +}