AccountCommand.java 6.2 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
@Command(label = "account", usage = "account <create|delete|resetpass> <username> [uid|password] [uid] ", description = "commands.account.description", targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
17
18
19
public final class AccountCommand implements CommandHandler {

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

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

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

        switch (action) {
            default:
Secretboy's avatar
Secretboy committed
36
                CommandHandler.sendMessage(null, translate(sender, "commands.account.command_usage"));
KingRainbow44's avatar
KingRainbow44 committed
37
38
39
                return;
            case "create":
                int uid = 0;
40
41
                String password = "";

github-actions's avatar
github-actions committed
42
43
                if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
                    if (args.size() < 3) {
44
45
46
                        CommandHandler.sendMessage(null, "EXPERIMENTAL_RealPassword requires a password argument");
                        CommandHandler.sendMessage(null, "Usage: account create <username> <password> [uid]");

KingRainbow44's avatar
KingRainbow44 committed
47
48
                        return;
                    }
49
50
51
52
53
54
55
                    password = args.get(2);

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

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

85
                    CommandHandler.sendMessage(null, translate(sender, "commands.account.create", Integer.toString(account.getReservedPlayerUid())));
KingRainbow44's avatar
KingRainbow44 committed
86
87
88
                }
                return;
            case "delete":
89
90
91
92
                // Get the account we want to delete.
                Account toDelete = DatabaseHelper.getAccountByName(username);

                if (toDelete == null) {
Secretboy's avatar
Secretboy committed
93
                    CommandHandler.sendMessage(null, translate(sender, "commands.account.no_account"));
94
                    return;
KingRainbow44's avatar
KingRainbow44 committed
95
                }
github-actions's avatar
github-actions committed
96

97
98
                // Get the player for the account.
                // If that player is currently online, we kick them before proceeding with the deletion.
99
                Player player = Grasscutter.getGameServer().getPlayerByAccountId(toDelete.getId());
100
101
102
103
104
105
                if (player != null) {
                    player.getSession().close();
                }

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

github-actions's avatar
github-actions committed
114
                if (args.size() != 3) {
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                    CommandHandler.sendMessage(null, "Invalid Args");
                    CommandHandler.sendMessage(null, "Usage: account resetpass <username> <password>");
                    return;
                }

                Account toUpdate = DatabaseHelper.getAccountByName(username);

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

                // Get the player for the account.
                // If that player is currently online, we kick them before proceeding with the deletion.
                Player uPlayer = Grasscutter.getGameServer().getPlayerByAccountId(toUpdate.getId());
                if (uPlayer != null) {
                    uPlayer.getSession().close();
                }

                toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
                toUpdate.save();
                CommandHandler.sendMessage(null, "Password Updated.");
                return;
KingRainbow44's avatar
KingRainbow44 committed
138
139
140
        }
    }
}