ServerCommands.java 14.5 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.commands;

3
import com.mongodb.internal.connection.CommandHelper;
KingRainbow44's avatar
KingRainbow44 committed
4
5
6
7
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
8
import emu.grasscutter.server.packet.send.PacketSceneKickPlayerRsp;
KingRainbow44's avatar
KingRainbow44 committed
9

10
11
12
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
KingRainbow44's avatar
KingRainbow44 committed
13
import java.util.List;
KingRainbow44's avatar
KingRainbow44 committed
14
import java.util.stream.Collectors;
KingRainbow44's avatar
KingRainbow44 committed
15
16
17
18
19

/**
 * A container for server-related commands.
 */
public final class ServerCommands {
20
    @Command(label = "reload", usage = "reload", description = "Reload server config", permission = "server.reload")
KingRainbow44's avatar
KingRainbow44 committed
21
22
23
24
25
26
27
28
29
30
31
32
    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) {
33
            CommandHandler.sendMessage(player, "Reloading config.");
KingRainbow44's avatar
KingRainbow44 committed
34
            this.execute(args);
35
            CommandHandler.sendMessage(player, "Reload complete.");
KingRainbow44's avatar
KingRainbow44 committed
36
37
        }
    }
38
39
40
41
42

    @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) {
43
44
45
46
47
            this.execute(null, args);
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
48
49
50
            int target = Integer.parseInt(args.get(0));
            String message = String.join(" ", args.subList(1, args.size()));

51
            GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
52
            if(targetPlayer == null) {
53
54
55
56
57
                CommandHandler.sendMessage(player, "Player not found.");
                return;
            }
            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()));
58
59
            }

60
61
62
                CommandHandler.sendMessage(player, String.format("Kicking player [%s:%s]", target, targetPlayer.getAccount().getUsername()));

            targetPlayer.getSession().close();
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
107
108
109
110
111
112
        }
    }

    @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
113
    @Command(label = "sendmessage", aliases = {"sendmsg", "msg"}, 
114
            usage = "sendmessage <player> <message>", description = "Sends a message to a player")
KingRainbow44's avatar
KingRainbow44 committed
115
116
117
118
119
120
121
122
123
124
125
126
    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()));

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

KingRainbow44's avatar
KingRainbow44 committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                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
143
                CommandHandler.sendMessage(player, "Usage: sendmessage <player> <message>"); return;
KingRainbow44's avatar
KingRainbow44 committed
144
145
146
147
148
149
            }

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

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

KingRainbow44's avatar
KingRainbow44 committed
152
                if(targetPlayer == null) {
KingRainbow44's avatar
KingRainbow44 committed
153
                    CommandHandler.sendMessage(player, "Player not found."); return;
KingRainbow44's avatar
KingRainbow44 committed
154
155
156
                }

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

        @Override
        public void execute(List<String> args) {
255
256
257
258
259
            this.execute(null, args);
        }

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
260
            if(args.size() < 1) {
261
262
263
264
265
266
267
268
269
270
271
272
273
                HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
                List<Command> annotations = new ArrayList<Command>();
                for(String key : handlers.keySet()) {
                    Command annotation = handlers.get(key).getClass().getAnnotation(Command.class);

                    if(!Arrays.asList(annotation.aliases()).contains(key)) {
                        if(player != null && annotation.permission() != "" && !player.getAccount().hasPermission(annotation.permission())) continue;
                        annotations.add(annotation);
                    }
                }


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

                CommandHandler.sendMessage(player, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
297
298
299
            }
        }

300
301
302
303
304
305
306
307
308
309
310
311
312
313
        void SendAllHelpMessage(GenshinPlayer player, List<Command> annotations) {
            if(player == null) {
                StringBuilder builder = new StringBuilder("\nAvailable commands:\n");
                annotations.forEach(annotation -> {
                    if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
                        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
314

315
316
317
318
319
                        builder.append("\n");
                    }
                });

                CommandHandler.sendMessage(null, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
320
            } else {
321
322
323
324
325
326
327
328
329
330
331
332
                CommandHandler.sendMessage(player, "Available commands:");
                annotations.forEach(annotation -> {
                    if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
                        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
333

334
335
336
                        CommandHandler.sendMessage(player, builder.toString());
                    }
                });
KingRainbow44's avatar
KingRainbow44 committed
337
338
339
            }
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
340
}