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

80 lines
3.1 KiB
Java
Raw Normal View History

2022-04-21 06:48:24 +08:00
package emu.grasscutter.command.commands;
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;
import emu.grasscutter.game.Account;
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;
import static emu.grasscutter.utils.Language.translate;
2022-05-08 14:44:14 +08:00
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "commands.account.description")
2022-04-21 06:48:24 +08:00
public final class AccountCommand implements CommandHandler {
@Override
2022-05-04 14:32:09 +08:00
public void execute(Player sender, Player targetPlayer, List<String> args) {
2022-04-21 06:48:24 +08:00
if (sender != null) {
CommandHandler.sendMessage(sender, translate("commands.generic.console_execute_error"));
2022-04-21 06:48:24 +08:00
return;
}
if (args.size() < 2) {
CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
2022-04-21 06:48:24 +08:00
return;
}
String action = args.get(0);
String username = args.get(1);
switch (action) {
default:
CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
2022-04-21 06:48:24 +08:00
return;
case "create":
int uid = 0;
if (args.size() > 2) {
try {
uid = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, translate("commands.account.invalid"));
2022-04-21 06:48:24 +08:00
return;
}
}
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
if (account == null) {
CommandHandler.sendMessage(null, translate("commands.account.exists"));
2022-04-21 06:48:24 +08:00
return;
} else {
account.addPermission("*");
2022-04-21 06:48:24 +08:00
account.save(); // Save account to database.
CommandHandler.sendMessage(null, translate("commands.account.create", Integer.toString(account.getPlayerUid())));
2022-04-21 06:48:24 +08:00
}
return;
case "delete":
// Get the account we want to delete.
Account toDelete = DatabaseHelper.getAccountByName(username);
if (toDelete == null) {
CommandHandler.sendMessage(null, translate("commands.account.no_account"));
return;
2022-04-21 06:48:24 +08:00
}
// Get the player for the account.
// If that player is currently online, we kick them before proceeding with the deletion.
Player player = Grasscutter.getGameServer().getPlayerByUid(toDelete.getPlayerUid());
if (player != null) {
player.getSession().close();
}
// Finally, we do the actual deletion.
DatabaseHelper.deleteAccount(toDelete);
CommandHandler.sendMessage(null, translate("commands.account.delete"));
2022-04-21 06:48:24 +08:00
}
}
}