Add account list command

This commit is contained in:
KingRainbow44 2023-05-10 20:53:20 -04:00
parent 7c065b33d6
commit 510e6bfa78
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE

View File

@ -8,9 +8,11 @@ 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",
@ -38,13 +40,10 @@ public final class AccountCommand implements CommandHandler {
String username = args.get(1);
switch (action) {
default:
sendUsageMessage(sender);
return;
case "create":
default -> this.sendUsageMessage(sender);
case "create" -> {
int uid = 0;
String password = "";
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
if (args.size() < 3) {
CommandHandler.sendMessage(
@ -79,8 +78,7 @@ public final class AccountCommand implements CommandHandler {
}
}
}
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
Account account = DatabaseHelper.createAccountWithUid(username, uid);
if (account == null) {
CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists"));
return;
@ -94,34 +92,29 @@ public final class AccountCommand implements CommandHandler {
CommandHandler.sendMessage(
sender, translate(sender, "commands.account.create", account.getReservedPlayerUid()));
}
return;
case "delete":
}
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":
}
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 <username> <password>");
return;
}
Account toUpdate = DatabaseHelper.getAccountByName(username);
if (toUpdate == null) {
CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
return;
@ -129,11 +122,36 @@ public final class AccountCommand implements CommandHandler {
// 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) {