Grasscutter/src/main/java/emu/grasscutter/command/commands/AccountCommand.java

64 lines
2.3 KiB
Java
Raw Normal View History

2022-04-21 06:48:24 +08:00
package emu.grasscutter.command.commands;
2022-04-28 06:18:59 +08:00
import emu.grasscutter.Grasscutter;
2022-04-21 06:48:24 +08:00
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
2022-04-27 12:24:25 +08:00
import emu.grasscutter.game.player.Player;
2022-04-21 06:48:24 +08:00
import java.util.List;
2022-04-28 06:18:59 +08:00
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "Modify user accounts")
2022-04-21 06:48:24 +08:00
public final class AccountCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
2022-04-21 06:48:24 +08:00
if (sender != null) {
CommandHandler.sendMessage(sender, "This command can only be run from the console.");
return;
}
if (args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]");
return;
}
String action = args.get(0);
String username = args.get(1);
switch (action) {
default:
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]");
return;
case "create":
int uid = 0;
if (args.size() > 2) {
try {
uid = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid UID.");
return;
}
}
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
if (account == null) {
CommandHandler.sendMessage(null, "Account already exists.");
return;
} else {
account.addPermission("*");
2022-04-21 06:48:24 +08:00
account.save(); // Save account to database.
CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerUid() + ".");
2022-04-21 06:48:24 +08:00
}
return;
case "delete":
if (DatabaseHelper.deleteAccount(username)) {
CommandHandler.sendMessage(null, "Account deleted.");
} else {
CommandHandler.sendMessage(null, "Account not found.");
}
}
}
}