AccountCommand.java 2.42 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
package emu.grasscutter.command.commands;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
7
8
9

import java.util.List;

10
11
import static emu.grasscutter.utils.Language.translate;

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

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

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

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

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

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

53
                    CommandHandler.sendMessage(null, translate("commands.account.create", Integer.toString(account.getPlayerUid())));
KingRainbow44's avatar
KingRainbow44 committed
54
55
56
57
                }
                return;
            case "delete":
                if (DatabaseHelper.deleteAccount(username)) {
58
                    CommandHandler.sendMessage(null, translate("commands.account.delete"));
KingRainbow44's avatar
KingRainbow44 committed
59
                } else {
60
                    CommandHandler.sendMessage(null, translate("commands.account.no_account"));
KingRainbow44's avatar
KingRainbow44 committed
61
62
63
64
                }
        }
    }
}