AccountCommand.java 6.14 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
            CommandHandler.sendTranslatedMessage(sender, "commands.account.command_usage");
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
                CommandHandler.sendTranslatedMessage(sender, "commands.account.command_usage");
KingRainbow44's avatar
KingRainbow44 committed
43
44
45
                return;
            case "create":
                int uid = 0;
46
47
                String password = "";

48
49
50
51
                if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
                    if(args.size() < 3) {
                        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
61
62
63
                            CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
                            if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
                                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

90
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.create", Integer.toString(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
103
                // Make sure player isn't online as we delete their account.
                kickAccount(toDelete);
104
105
106

                // Finally, we do the actual deletion.
                DatabaseHelper.deleteAccount(toDelete);
107
                CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete"));
108
109
                return;
            case "resetpass":
110
111
                if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword != true) {
                    CommandHandler.sendMessage(sender, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
112
113
114
                    return;
                }

115
116
117
                if(args.size() != 3) {
                    CommandHandler.sendMessage(sender, "Invalid Args");
                    CommandHandler.sendMessage(sender, "Usage: account resetpass <username> <password>");
118
119
120
121
122
123
                    return;
                }

                Account toUpdate = DatabaseHelper.getAccountByName(username);

                if (toUpdate == null) {
124
                    CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
125
126
127
                    return;
                }

128
129
                // Make sure player can't stay logged in with old password.
                kickAccount(toUpdate);
130
131
132

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

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