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

memetrollsXD's avatar
memetrollsXD committed
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;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
8
9
10

import java.util.List;

memetrollsXD's avatar
memetrollsXD committed
11
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "Modify user accounts")
KingRainbow44's avatar
KingRainbow44 committed
12
13
14
public final class AccountCommand implements CommandHandler {

    @Override
15
    public void execute(Player sender, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        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 {
49
                    account.addPermission("*");
KingRainbow44's avatar
KingRainbow44 committed
50
                    account.save(); // Save account to database.
51
52

                    CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerUid() + ".");
KingRainbow44's avatar
KingRainbow44 committed
53
54
55
56
57
58
59
60
61
62
63
                }
                return;
            case "delete":
                if (DatabaseHelper.deleteAccount(username)) {
                    CommandHandler.sendMessage(null, "Account deleted.");
                } else {
                    CommandHandler.sendMessage(null, "Account not found.");
                }
        }
    }
}