ServerCommands.java 14.2 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
192
                    } else {
                        CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + ".");
                        account.addPermission("*"); // Grant the player superuser permissions.
                    }
KingRainbow44's avatar
KingRainbow44 committed
193
194
195
196
197
198
199
200
201
                    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
202
    
203
    @Command(label = "permission", 
204
205
            usage = "permission <add|remove> <username> <permission>",
            description = "Grants or removes a permission for a user", permission = "*")
206
207
208
209
210
211
212
213
214
215
216
217
    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
218
            Account account = Grasscutter.getGameServer().getAccountByName(username);
219
220
221
222
223
224
225
            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
226
                    break;
227
228
                case "add":
                    if(account.addPermission(permission)) {
Melledy's avatar
Melledy committed
229
                        CommandHandler.sendMessage(null, "Permission added.");
230
                    } else CommandHandler.sendMessage(null, "They already have this permission!");
Melledy's avatar
Melledy committed
231
                    break;
232
233
                case "remove":
                    if(account.removePermission(permission)) {
Melledy's avatar
Melledy committed
234
                        CommandHandler.sendMessage(null, "Permission removed.");
235
                    } else CommandHandler.sendMessage(null, "They don't have this permission!");
Melledy's avatar
Melledy committed
236
                    break;
237
            }
Melledy's avatar
Melledy committed
238
239
            
            account.save();
240
241
242
        }
    }
    
KingRainbow44's avatar
KingRainbow44 committed
243
    @Command(label = "help", 
244
            usage = "help [command]", description = "Sends the help message or shows information about a specified command")
KingRainbow44's avatar
KingRainbow44 committed
245
246
247
248
    public static class HelpCommand implements CommandHandler {

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

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

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


                SendAllHelpMessage(player, annotations);
KingRainbow44's avatar
KingRainbow44 committed
268
269
270
            } else {
                String command = args.get(0);
                CommandHandler handler = CommandMap.getInstance().getHandler(command);
271
                StringBuilder builder = new StringBuilder(player == null ? "\nHelp - " : "Help - ").append(command).append(": \n");
KingRainbow44's avatar
KingRainbow44 committed
272
                if(handler == null) {
273
274
275
276
277
278
279
280
281
282
283
284
                    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(" ");
                        }
                    }
285
                    if(player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) {
286
287
                        builder.append("\n Warning: You do not have permission to run this command.");
                    }
KingRainbow44's avatar
KingRainbow44 committed
288
                }
289
290

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

294
295
296
297
        void SendAllHelpMessage(GenshinPlayer player, List<Command> annotations) {
            if(player == null) {
                StringBuilder builder = new StringBuilder("\nAvailable commands:\n");
                annotations.forEach(annotation -> {
298
                    if (annotation.execution() != Command.Execution.PLAYER) {
299
300
301
302
303
304
305
306
307
                        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
308

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

                CommandHandler.sendMessage(null, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
314
            } else {
315
316
                CommandHandler.sendMessage(player, "Available commands:");
                annotations.forEach(annotation -> {
317
                    if (annotation.execution() != Command.Execution.CONSOLE) {
318
319
320
321
322
323
324
325
326
                        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
327

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