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

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

8
import java.util.*;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11
12
13

/**
 * A container for server-related commands.
 */
public final class ServerCommands {
14
    @Command(label = "reload", usage = "reload", description = "Reload server config", permission = "server.reload")
KingRainbow44's avatar
KingRainbow44 committed
15
16
17
18
19
20
    public static class ReloadCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
            Grasscutter.getLogger().info("Reloading config.");
            Grasscutter.loadConfig();
21
            Grasscutter.getGameServer().getGachaManager().load();
KingRainbow44's avatar
KingRainbow44 committed
22
23
24
25
26
27
            Grasscutter.getDispatchServer().loadQueries();
            Grasscutter.getLogger().info("Reload complete.");
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
28
            CommandHandler.sendMessage(player, "Reloading config.");
KingRainbow44's avatar
KingRainbow44 committed
29
            this.execute(args);
30
            CommandHandler.sendMessage(player, "Reload complete.");
KingRainbow44's avatar
KingRainbow44 committed
31
32
        }
    }
33
34
35
36
37

    @Command(label = "kick", usage = "kick <player>", description = "Kicks the specified player from the server (WIP)", permission = "server.kick")
    public static class KickCommand implements CommandHandler {
        @Override
        public void execute(List<String> args) {
38
39
40
41
42
            this.execute(null, args);
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
43
44
            int target = Integer.parseInt(args.get(0));

45
            GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
46
            if(targetPlayer == null) {
47
48
49
                CommandHandler.sendMessage(player, "Player not found.");
                return;
            }
50
            
51
52
            if(player != null) {
                CommandHandler.sendMessage(null, String.format("Player [%s:%s] has kicked player [%s:%s]", player.getAccount().getPlayerId(), player.getAccount().getUsername(), target, targetPlayer.getAccount().getUsername()));
53
            }
54
            CommandHandler.sendMessage(player, String.format("Kicking player [%s:%s]", target, targetPlayer.getAccount().getUsername()));
55
56

            targetPlayer.getSession().close();
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
        }
    }

    @Command(label = "stop", usage = "stop", description = "Stops the server", permission = "server.stop")
    public static class StopCommand implements CommandHandler {
        @Override
        public void execute(List<String> args) {
            Grasscutter.getLogger().info("Server shutting down...");
            for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
                p.dropMessage("Server shutting down...");
            }

            System.exit(1);
        }
    }

    @Command(label = "broadcast", aliases = {"b"},
            usage = "broadcast <message>", description = "Sends a message to all the players", permission = "server.broadcast")
    public static class BroadcastCommand implements CommandHandler {
        @Override
        public void execute(List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(null, "Usage: broadcast <message>"); return;
            }

            String message = String.join(" ", args.subList(0, args.size()));

            for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
                p.dropMessage(message);
            }

            CommandHandler.sendMessage(null, "Message sent.");
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(player, "Usage: broadcast <message>"); return;
            }

            String message = String.join(" ", args.subList(0, args.size()));

            for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
                p.dropMessage(message);
            }

            CommandHandler.sendMessage(player, "Message sent.");
        }
    }

KingRainbow44's avatar
KingRainbow44 committed
107
    @Command(label = "sendmessage", aliases = {"sendmsg", "msg"}, 
108
            usage = "sendmessage <player> <message>", description = "Sends a message to a player")
KingRainbow44's avatar
KingRainbow44 committed
109
110
111
112
113
114
115
116
117
118
119
120
    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()));

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

KingRainbow44's avatar
KingRainbow44 committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
                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
137
                CommandHandler.sendMessage(player, "Usage: sendmessage <player> <message>"); return;
KingRainbow44's avatar
KingRainbow44 committed
138
139
140
141
142
143
            }

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

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

KingRainbow44's avatar
KingRainbow44 committed
146
                if(targetPlayer == null) {
KingRainbow44's avatar
KingRainbow44 committed
147
                    CommandHandler.sendMessage(player, "Player not found."); return;
KingRainbow44's avatar
KingRainbow44 committed
148
149
150
                }

                targetPlayer.sendMessage(player, message);
KingRainbow44's avatar
KingRainbow44 committed
151
                CommandHandler.sendMessage(player, "Message sent.");
KingRainbow44's avatar
KingRainbow44 committed
152
            } catch (NumberFormatException ignored) {
KingRainbow44's avatar
KingRainbow44 committed
153
                CommandHandler.sendMessage(player, "Invalid player ID.");
KingRainbow44's avatar
KingRainbow44 committed
154
155
156
157
158
            }
        }
    }
    
    @Command(label = "account", 
159
160
            usage = "account <create|delete> <username> [uid]",
            description = "Modify user accounts", execution = Command.Execution.CONSOLE)
KingRainbow44's avatar
KingRainbow44 committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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;
189
190
191
                    } else {
                        CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + ".");
                        account.addPermission("*"); // Grant the player superuser permissions.
192
                        account.save(); // Save account to database.
193
                    }
KingRainbow44's avatar
KingRainbow44 committed
194
195
196
197
198
199
200
201
202
                    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
203
    
204
    @Command(label = "permission", 
205
206
            usage = "permission <add|remove> <username> <permission>",
            description = "Grants or removes a permission for a user", permission = "*")
207
208
209
210
211
212
213
214
215
216
217
218
    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
219
            Account account = Grasscutter.getGameServer().getAccountByName(username);
220
221
222
223
224
225
226
            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
227
                    break;
228
229
                case "add":
                    if(account.addPermission(permission)) {
Melledy's avatar
Melledy committed
230
                        CommandHandler.sendMessage(null, "Permission added.");
231
                    } else CommandHandler.sendMessage(null, "They already have this permission!");
Melledy's avatar
Melledy committed
232
                    break;
233
234
                case "remove":
                    if(account.removePermission(permission)) {
Melledy's avatar
Melledy committed
235
                        CommandHandler.sendMessage(null, "Permission removed.");
236
                    } else CommandHandler.sendMessage(null, "They don't have this permission!");
Melledy's avatar
Melledy committed
237
                    break;
238
            }
Melledy's avatar
Melledy committed
239
240
            
            account.save();
241
242
243
        }
    }
    
KingRainbow44's avatar
KingRainbow44 committed
244
    @Command(label = "help", 
245
            usage = "help [command]", description = "Sends the help message or shows information about a specified command")
KingRainbow44's avatar
KingRainbow44 committed
246
247
248
249
    public static class HelpCommand implements CommandHandler {

        @Override
        public void execute(List<String> args) {
250
251
252
253
254
            this.execute(null, args);
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
255
            if(args.size() < 1) {
256
                HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
257
                List<Command> annotations = new ArrayList<>();
258
259
260
261
                for(String key : handlers.keySet()) {
                    Command annotation = handlers.get(key).getClass().getAnnotation(Command.class);

                    if(!Arrays.asList(annotation.aliases()).contains(key)) {
262
                        if(player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) continue;
263
264
265
266
267
268
                        annotations.add(annotation);
                    }
                }


                SendAllHelpMessage(player, annotations);
KingRainbow44's avatar
KingRainbow44 committed
269
270
271
            } else {
                String command = args.get(0);
                CommandHandler handler = CommandMap.getInstance().getHandler(command);
272
                StringBuilder builder = new StringBuilder(player == null ? "\nHelp - " : "Help - ").append(command).append(": \n");
KingRainbow44's avatar
KingRainbow44 committed
273
                if(handler == null) {
274
275
276
277
278
279
280
281
282
283
284
285
                    builder.append("No command found.");
                } else {
                    Command annotation = handler.getClass().getAnnotation(Command.class);

                    builder.append("   ").append(annotation.description()).append("\n");
                    builder.append("   Usage: ").append(annotation.usage());
                    if(annotation.aliases().length >= 1) {
                        builder.append("\n").append("   Aliases: ");
                        for (String alias : annotation.aliases()) {
                            builder.append(alias).append(" ");
                        }
                    }
286
                    if(player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) {
287
288
                        builder.append("\n Warning: You do not have permission to run this command.");
                    }
KingRainbow44's avatar
KingRainbow44 committed
289
                }
290
291

                CommandHandler.sendMessage(player, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
292
293
294
            }
        }

295
296
297
298
        void SendAllHelpMessage(GenshinPlayer player, List<Command> annotations) {
            if(player == null) {
                StringBuilder builder = new StringBuilder("\nAvailable commands:\n");
                annotations.forEach(annotation -> {
299
                    if (annotation.execution() != Command.Execution.PLAYER) {
300
301
302
303
304
305
306
307
308
                        builder.append(annotation.label()).append("\n");
                        builder.append("   ").append(annotation.description()).append("\n");
                        builder.append("   Usage: ").append(annotation.usage());
                        if (annotation.aliases().length >= 1) {
                            builder.append("\n").append("   Aliases: ");
                            for (String alias : annotation.aliases()) {
                                builder.append(alias).append(" ");
                            }
                        }
KingRainbow44's avatar
KingRainbow44 committed
309

310
311
312
313
314
                        builder.append("\n");
                    }
                });

                CommandHandler.sendMessage(null, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
315
            } else {
316
317
                CommandHandler.sendMessage(player, "Available commands:");
                annotations.forEach(annotation -> {
318
                    if (annotation.execution() != Command.Execution.CONSOLE) {
319
320
321
322
323
324
325
326
327
                        StringBuilder builder = new StringBuilder(annotation.label()).append("\n");
                        builder.append("   ").append(annotation.description()).append("\n");
                        builder.append("   Usage: ").append(annotation.usage());
                        if (annotation.aliases().length >= 1) {
                            builder.append("\n").append("   Aliases: ");
                            for (String alias : annotation.aliases()) {
                                builder.append(alias).append(" ");
                            }
                        }
KingRainbow44's avatar
KingRainbow44 committed
328

329
330
331
                        CommandHandler.sendMessage(player, builder.toString());
                    }
                });
KingRainbow44's avatar
KingRainbow44 committed
332
333
334
            }
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
335
}