AccountCommand.java 3.12 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.command.commands;

3
import emu.grasscutter.Grasscutter;
KingRainbow44's avatar
KingRainbow44 committed
4
5
6
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
7
import emu.grasscutter.game.Account;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11

import java.util.List;

12
13
import static emu.grasscutter.utils.Language.translate;

方块君's avatar
方块君 committed
14
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "commands.account.description")
KingRainbow44's avatar
KingRainbow44 committed
15
16
17
public final class AccountCommand implements CommandHandler {

    @Override
AnimeGitB's avatar
AnimeGitB committed
18
    public void execute(Player sender, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
19
        if (sender != null) {
Secretboy's avatar
Secretboy committed
20
            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.console_execute_error"));
KingRainbow44's avatar
KingRainbow44 committed
21
22
23
24
            return;
        }

        if (args.size() < 2) {
Secretboy's avatar
Secretboy committed
25
            CommandHandler.sendMessage(null, translate(sender, "commands.account.command_usage"));
KingRainbow44's avatar
KingRainbow44 committed
26
27
28
29
30
31
32
33
            return;
        }

        String action = args.get(0);
        String username = args.get(1);

        switch (action) {
            default:
Secretboy's avatar
Secretboy committed
34
                CommandHandler.sendMessage(null, translate(sender, "commands.account.command_usage"));
KingRainbow44's avatar
KingRainbow44 committed
35
36
37
38
39
40
41
                return;
            case "create":
                int uid = 0;
                if (args.size() > 2) {
                    try {
                        uid = Integer.parseInt(args.get(2));
                    } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
42
                        CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
KingRainbow44's avatar
KingRainbow44 committed
43
44
45
46
47
48
                        return;
                    }
                }

                emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
                if (account == null) {
Secretboy's avatar
Secretboy committed
49
                    CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
KingRainbow44's avatar
KingRainbow44 committed
50
51
                    return;
                } else {
52
                    account.addPermission("*");
KingRainbow44's avatar
KingRainbow44 committed
53
                    account.save(); // Save account to database.
54

Secretboy's avatar
Secretboy committed
55
                    CommandHandler.sendMessage(null, translate(sender, "commands.account.create", Integer.toString(account.getPlayerUid())));
KingRainbow44's avatar
KingRainbow44 committed
56
57
58
                }
                return;
            case "delete":
59
60
61
62
                // Get the account we want to delete.
                Account toDelete = DatabaseHelper.getAccountByName(username);

                if (toDelete == null) {
Secretboy's avatar
Secretboy committed
63
                    CommandHandler.sendMessage(null, translate(sender, "commands.account.no_account"));
64
                    return;
KingRainbow44's avatar
KingRainbow44 committed
65
                }
66
67
68
69
70
71
72
73
74
75

                // 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);
Secretboy's avatar
Secretboy committed
76
                CommandHandler.sendMessage(null, translate(sender, "commands.account.delete"));
KingRainbow44's avatar
KingRainbow44 committed
77
78
79
        }
    }
}