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

3
import at.favre.lib.crypto.bcrypt.BCrypt;
4
import emu.grasscutter.Grasscutter;
KingRainbow44's avatar
KingRainbow44 committed
5
6
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
7
import emu.grasscutter.config.Configuration;
KingRainbow44's avatar
KingRainbow44 committed
8
import emu.grasscutter.database.DatabaseHelper;
9
import emu.grasscutter.game.Account;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
11
12
13

import java.util.List;

14
15
import static emu.grasscutter.utils.Language.translate;

16
17
18
19
20
21
22
23
@Command(
    label = "account",
    usage = {
        "create <username> [<UID>]",  // Only with EXPERIMENTAL_RealPassword == false
        "delete <username>",
        "create <username> <password> [<UID>]",  // Only with EXPERIMENTAL_RealPassword == true
        "resetpass <username> <password>"},  // Only with EXPERIMENTAL_RealPassword == true
    targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
24
25
public final class AccountCommand implements CommandHandler {
    @Override
AnimeGitB's avatar
AnimeGitB committed
26
    public void execute(Player sender, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
27
        if (sender != null) {
28
            CommandHandler.sendTranslatedMessage(sender, "commands.generic.console_execute_error");
KingRainbow44's avatar
KingRainbow44 committed
29
30
31
32
            return;
        }

        if (args.size() < 2) {
33
            sendUsageMessage(sender);
KingRainbow44's avatar
KingRainbow44 committed
34
35
36
37
38
39
40
41
            return;
        }

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

        switch (action) {
            default:
42
                sendUsageMessage(sender);
KingRainbow44's avatar
KingRainbow44 committed
43
44
45
                return;
            case "create":
                int uid = 0;
46
47
                String password = "";

github-actions's avatar
github-actions committed
48
49
                if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
                    if (args.size() < 3) {
50
51
                        CommandHandler.sendMessage(sender, "EXPERIMENTAL_RealPassword requires a password argument");
                        CommandHandler.sendMessage(sender, "Usage: account create <username> <password> [uid]");
KingRainbow44's avatar
KingRainbow44 committed
52
53
                        return;
                    }
54
55
56
57
58
59
                    password = args.get(2);

                    if (args.size() == 4) {
                        try {
                            uid = Integer.parseInt(args.get(3));
                        } catch (NumberFormatException ignored) {
60
                            CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
github-actions's avatar
github-actions committed
61
                            if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
62
63
                                CommandHandler.sendMessage(sender, "EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid");
                                CommandHandler.sendMessage(sender, "Usage: account create <username> <password> [uid]");
64
65
66
67
68
69
70
71
72
                            }
                            return;
                        }
                    }
                } else {
                    if (args.size() > 2) {
                        try {
                            uid = Integer.parseInt(args.get(2));
                        } catch (NumberFormatException ignored) {
73
                            CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
74
75
76
                            return;
                        }
                    }
KingRainbow44's avatar
KingRainbow44 committed
77
78
                }

79
                emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
KingRainbow44's avatar
KingRainbow44 committed
80
                if (account == null) {
81
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists"));
KingRainbow44's avatar
KingRainbow44 committed
82
83
                    return;
                } else {
github-actions's avatar
github-actions committed
84
                    if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
85
86
                        account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray()));
                    }
87
                    account.addPermission("*");
KingRainbow44's avatar
KingRainbow44 committed
88
                    account.save(); // Save account to database.
89

AnimeGitB's avatar
AnimeGitB committed
90
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.create", account.getReservedPlayerUid()));
KingRainbow44's avatar
KingRainbow44 committed
91
92
93
                }
                return;
            case "delete":
94
95
96
97
                // Get the account we want to delete.
                Account toDelete = DatabaseHelper.getAccountByName(username);

                if (toDelete == null) {
98
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
99
                    return;
KingRainbow44's avatar
KingRainbow44 committed
100
                }
github-actions's avatar
github-actions committed
101

102
                DatabaseHelper.deleteAccount(toDelete);
103
                CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete"));
104
105
                return;
            case "resetpass":
github-actions's avatar
github-actions committed
106
                if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword != true) {
107
                    CommandHandler.sendMessage(sender, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
108
109
110
                    return;
                }

github-actions's avatar
github-actions committed
111
                if (args.size() != 3) {
112
113
                    CommandHandler.sendMessage(sender, "Invalid Args");
                    CommandHandler.sendMessage(sender, "Usage: account resetpass <username> <password>");
114
115
116
117
118
119
                    return;
                }

                Account toUpdate = DatabaseHelper.getAccountByName(username);

                if (toUpdate == null) {
120
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
121
122
123
                    return;
                }

124
125
                // Make sure player can't stay logged in with old password.
                kickAccount(toUpdate);
126
127
128

                toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
                toUpdate.save();
129
                CommandHandler.sendMessage(sender, "Password Updated.");
130
                return;
KingRainbow44's avatar
KingRainbow44 committed
131
132
        }
    }
133
134
135
136
137
138
139

    private void kickAccount(Account account) {
        Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId());
        if (player != null) {
            player.getSession().close();
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
140
}