ServerCommands.java 8.71 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
6
7
8
package emu.grasscutter.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;

import java.util.List;
KingRainbow44's avatar
KingRainbow44 committed
9
import java.util.stream.Collectors;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
14
15
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

/**
 * A container for server-related commands.
 */
public final class ServerCommands {
    @Command(label = "reload", usage = "Usage: reload")
    public static class ReloadCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            Grasscutter.getLogger().info("Reloading config.");
            Grasscutter.loadConfig();
            Grasscutter.getDispatchServer().loadQueries();
            Grasscutter.getLogger().info("Reload complete.");
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            this.execute(args);
        }
    }
    
    @Command(label = "sendmessage", aliases = {"sendmsg", "msg"}, 
            usage = "Usage: sendmessage <player> <message>")
    public static class SendMessageCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            if(args.size() < 2) {
                CommandHandler.sendMessage(null, "Usage: sendmessage <player> <message>"); return;
            }
            
            try {
                int target = Integer.parseInt(args.get(0));
                String message = String.join(" ", args.subList(1, args.size()));

46
                GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
Melledy's avatar
Melledy committed
47

KingRainbow44's avatar
KingRainbow44 committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
                if(targetPlayer == null) {
                    CommandHandler.sendMessage(null, "Player not found."); return;
                }
                
                targetPlayer.dropMessage(message);
                CommandHandler.sendMessage(null, "Message sent.");
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid player ID.");
            }
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 2) {
KingRainbow44's avatar
KingRainbow44 committed
62
                CommandHandler.sendMessage(player, "Usage: sendmessage <player> <message>"); return;
KingRainbow44's avatar
KingRainbow44 committed
63
64
65
66
67
68
            }

            try {
                int target = Integer.parseInt(args.get(0));
                String message = String.join(" ", args.subList(1, args.size()));

69
                GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
Melledy's avatar
Melledy committed
70

KingRainbow44's avatar
KingRainbow44 committed
71
                if(targetPlayer == null) {
KingRainbow44's avatar
KingRainbow44 committed
72
                    CommandHandler.sendMessage(player, "Player not found."); return;
KingRainbow44's avatar
KingRainbow44 committed
73
74
75
                }

                targetPlayer.sendMessage(player, message);
KingRainbow44's avatar
KingRainbow44 committed
76
                CommandHandler.sendMessage(player, "Message sent.");
KingRainbow44's avatar
KingRainbow44 committed
77
            } catch (NumberFormatException ignored) {
KingRainbow44's avatar
KingRainbow44 committed
78
                CommandHandler.sendMessage(player, "Invalid player ID.");
KingRainbow44's avatar
KingRainbow44 committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
            }
        }
    }
    
    @Command(label = "account", 
            usage = "Usage: account <create|delete> <username> [uid]", 
            execution = Command.Execution.CONSOLE)
    public static class AccountCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            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;
                        }
                    }

                    Account account = DatabaseHelper.createAccountWithId(username, uid);
                    if(account == null) {
                        CommandHandler.sendMessage(null, "Account already exists."); return;
114
115
116
117
                    } else {
                        CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + ".");
                        account.addPermission("*"); // Grant the player superuser permissions.
                    }
KingRainbow44's avatar
KingRainbow44 committed
118
119
120
121
122
123
124
125
126
                    return;
                case "delete":
                    if(DatabaseHelper.deleteAccount(username)) {
                        CommandHandler.sendMessage(null, "Account deleted."); return;
                    } else CommandHandler.sendMessage(null, "Account not found.");
                    return;
            }
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
127
    
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    @Command(label = "permission", 
            usage = "Usage: permission <add|remove> <username> <permission>", 
            execution = Command.Execution.CONSOLE)
    public static class PermissionCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            if(args.size() < 3) {
                CommandHandler.sendMessage(null, "Usage: permission <add|remove> <username> <permission>"); return;
            }
            
            String action = args.get(0);
            String username = args.get(1);
            String permission = args.get(2);
            
Melledy's avatar
Melledy committed
143
            Account account = Grasscutter.getGameServer().getAccountByName(username);
144
145
146
147
148
149
150
            if(account == null) {
                CommandHandler.sendMessage(null, "Account not found."); return;
            }
            
            switch(action) {
                default:
                    CommandHandler.sendMessage(null, "Usage: permission <add|remove> <username> <permission>");
Melledy's avatar
Melledy committed
151
                    break;
152
153
                case "add":
                    if(account.addPermission(permission)) {
Melledy's avatar
Melledy committed
154
                        CommandHandler.sendMessage(null, "Permission added.");
155
                    } else CommandHandler.sendMessage(null, "They already have this permission!");
Melledy's avatar
Melledy committed
156
                    break;
157
158
                case "remove":
                    if(account.removePermission(permission)) {
Melledy's avatar
Melledy committed
159
                        CommandHandler.sendMessage(null, "Permission removed.");
160
                    } else CommandHandler.sendMessage(null, "They don't have this permission!");
Melledy's avatar
Melledy committed
161
                    break;
162
            }
Melledy's avatar
Melledy committed
163
164
            
            account.save();
165
166
167
        }
    }
    
KingRainbow44's avatar
KingRainbow44 committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    @Command(label = "help", 
            usage = "Usage: help [command]")
    public static class HelpCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
            List<Command> annotations = handlers.stream()
                    .map(handler -> handler.getClass().getAnnotation(Command.class))
                    .collect(Collectors.toList());
            
            if(args.size() < 1) {
                StringBuilder builder = new StringBuilder("Available commands:\n");
                annotations.forEach(annotation -> builder.append(annotation.usage()).append("\n"));
                CommandHandler.sendMessage(null, builder.toString());
            } else {
                String command = args.get(0);
                CommandHandler handler = CommandMap.getInstance().getHandler(command);
                if(handler == null) {
                    CommandHandler.sendMessage(null, "Command not found."); return;
                }
                
                Command annotation = handler.getClass().getAnnotation(Command.class);
                CommandHandler.sendMessage(null, annotation.usage());
            }
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
            List<Command> annotations = handlers.stream()
                    .map(handler -> handler.getClass().getAnnotation(Command.class))
                    .collect(Collectors.toList());

            if(args.size() < 1) {
                annotations.forEach(annotation -> player.dropMessage(annotation.usage()));
            } else {
                String command = args.get(0);
                CommandHandler handler = CommandMap.getInstance().getHandler(command);
                if(handler == null) {
                    CommandHandler.sendMessage(player, "Command not found."); return;
                }

                Command annotation = handler.getClass().getAnnotation(Command.class);
                CommandHandler.sendMessage(player, annotation.usage());
            }
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
216
}