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
21
22
23
24
25
26
    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) {
27
            CommandHandler.sendMessage(player, "Reloading config.");
KingRainbow44's avatar
KingRainbow44 committed
28
            this.execute(args);
29
            CommandHandler.sendMessage(player, "Reload complete.");
KingRainbow44's avatar
KingRainbow44 committed
30
31
        }
    }
32
33
34
35
36

    @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) {
37
38
39
40
41
            this.execute(null, args);
        }

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

44
            GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
45
            if(targetPlayer == null) {
46
47
48
                CommandHandler.sendMessage(player, "Player not found.");
                return;
            }
49
            
50
51
            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()));
52
            }
53
            CommandHandler.sendMessage(player, String.format("Kicking player [%s:%s]", target, targetPlayer.getAccount().getUsername()));
54
55

            targetPlayer.getSession().close();
56
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
        }
    }

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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