PlayerCommands.java 22.5 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.commands;

KingRainbow44's avatar
KingRainbow44 committed
3
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
4
5
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.ItemData;
6
import emu.grasscutter.data.def.AvatarData;
7
import emu.grasscutter.data.def.AvatarSkillDepotData;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.GenshinPlayer;
KingRainbow44's avatar
KingRainbow44 committed
10
import emu.grasscutter.game.World;
Melledy's avatar
Melledy committed
11
12
13
14
15
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.inventory.GenshinItem;
KingRainbow44's avatar
KingRainbow44 committed
16
import emu.grasscutter.game.inventory.Inventory;
Melledy's avatar
Melledy committed
17
18
19
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.FightProperty;
SpikeHD's avatar
SpikeHD committed
20
import emu.grasscutter.game.props.PlayerProperty;
Melledy's avatar
Melledy committed
21
22
23
24
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
import emu.grasscutter.server.packet.send.PacketItemAddHintNotify;
import emu.grasscutter.utils.Position;

KingRainbow44's avatar
KingRainbow44 committed
25
26
27
28
29
30
31
32
import java.util.LinkedList;
import java.util.List;

/**
 * A container for player-related commands.
 */
public final class PlayerCommands {
    @Command(label = "give", aliases = {"g", "item", "giveitem"}, 
33
            usage = "give [player] <itemId|itemName> [amount]", description = "Gives an item to you or the specified player", permission = "player.give")
KingRainbow44's avatar
KingRainbow44 committed
34
35
36
37
38
39
40
41
42
43
44
45
46
    public static class GiveCommand implements CommandHandler {

        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            int target, item, amount = 1;

            switch(args.size()) {
                default:
                    CommandHandler.sendMessage(player, "Usage: give <player> <itemId|itemName> [amount]");
                    return;
                case 1:
                    try {
                        item = Integer.parseInt(args.get(0));
KingRainbow44's avatar
KingRainbow44 committed
47
                        target = player.getAccount().getPlayerId();
KingRainbow44's avatar
KingRainbow44 committed
48
49
50
51
52
53
54
55
56
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from item name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid item id.");
                        return;
                    }
                    break;
                case 2:
                    try {
                        target = Integer.parseInt(args.get(0));
57
                        if(Grasscutter.getGameServer().getPlayerById(target) == null) {
KingRainbow44's avatar
KingRainbow44 committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                            target = player.getId(); amount = Integer.parseInt(args.get(1));
                            item = Integer.parseInt(args.get(0));
                        } else {
                            item = Integer.parseInt(args.get(1));
                        }
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from item name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid item or player ID.");
                        return;
                    }
                    break;
                case 3:
                    try {
                        target = Integer.parseInt(args.get(0));
72
                        if(Grasscutter.getGameServer().getPlayerById(target) == null) {
KingRainbow44's avatar
KingRainbow44 committed
73
74
75
76
77
78
79
80
81
82
83
84
85
                            CommandHandler.sendMessage(player, "Invalid player ID."); return;
                        }

                        item = Integer.parseInt(args.get(1));
                        amount = Integer.parseInt(args.get(2));
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from item name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid item or player ID.");
                        return;
                    }
                    break;
            }

86
            GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
KingRainbow44's avatar
KingRainbow44 committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
            if(targetPlayer == null) {
                CommandHandler.sendMessage(player, "Player not found."); return;
            }

            ItemData itemData = GenshinData.getItemDataMap().get(item);
            if(itemData == null) {
                CommandHandler.sendMessage(player, "Invalid item id."); return;
            }
            
            this.item(targetPlayer, itemData, amount);
        }

        /**
         * give [player] [itemId|itemName] [amount]
         */
        @Override public void execute(List<String> args) {
            if(args.size() < 2) {
                CommandHandler.sendMessage(null, "Usage: give <player> <itemId|itemName> [amount]");
                return;
            }
107

KingRainbow44's avatar
KingRainbow44 committed
108
109
110
111
112
            try {
                int target = Integer.parseInt(args.get(0));
                int item = Integer.parseInt(args.get(1));
                int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2));
                
113
                GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
KingRainbow44's avatar
KingRainbow44 committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                if(targetPlayer == null) {
                    CommandHandler.sendMessage(null, "Player not found."); return;
                }
                
                ItemData itemData = GenshinData.getItemDataMap().get(item);
                if(itemData == null) {
                    CommandHandler.sendMessage(null, "Invalid item id."); return;
                }
                
                this.item(targetPlayer, itemData, amount);
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid item or player ID.");
            }
        }
        
        private void item(GenshinPlayer player, ItemData itemData, int amount) {
            GenshinItem genshinItem = new GenshinItem(itemData);
            if(itemData.isEquip()) {
                List<GenshinItem> items = new LinkedList<>();
                for(int i = 0; i < amount; i++) {
                    items.add(genshinItem);
                } player.getInventory().addItems(items);
                player.sendPacket(new PacketItemAddHintNotify(items, ActionReason.SubfieldDrop));
            } else {
                genshinItem.setCount(amount);
                player.getInventory().addItem(genshinItem);
                player.sendPacket(new PacketItemAddHintNotify(genshinItem, ActionReason.SubfieldDrop));
            }
        }
    }
    
    @Command(label = "drop", aliases = {"d", "dropitem"}, 
146
147
            usage = "drop <itemId|itemName> [amount]",
            execution = Command.Execution.PLAYER, description = "Drops an item near you", permission = "server.drop")
KingRainbow44's avatar
KingRainbow44 committed
148
    public static class DropCommand implements CommandHandler {
149

KingRainbow44's avatar
KingRainbow44 committed
150
151
152
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 1) {
KingRainbow44's avatar
KingRainbow44 committed
153
                CommandHandler.sendMessage(player, "Usage: drop <itemId|itemName> [amount]");
KingRainbow44's avatar
KingRainbow44 committed
154
155
                return;
            }
156

KingRainbow44's avatar
KingRainbow44 committed
157
158
159
            try {
                int item = Integer.parseInt(args.get(0));
                int amount = 1; if(args.size() > 1) amount = Integer.parseInt(args.get(1));
Melledy's avatar
Melledy committed
160

KingRainbow44's avatar
KingRainbow44 committed
161
162
                ItemData itemData = GenshinData.getItemDataMap().get(item);
                if(itemData == null) {
KingRainbow44's avatar
KingRainbow44 committed
163
                    CommandHandler.sendMessage(player, "Invalid item id."); return;
KingRainbow44's avatar
KingRainbow44 committed
164
                }
165

KingRainbow44's avatar
KingRainbow44 committed
166
167
168
169
170
171
172
173
174
175
176
177
                if (itemData.isEquip()) {
                    float range = (5f + (.1f * amount));
                    for (int i = 0; i < amount; i++) {
                        Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
                        EntityItem entity = new EntityItem(player.getWorld(), player, itemData, pos, 1);
                        player.getWorld().addEntity(entity);
                    }
                } else {
                    EntityItem entity = new EntityItem(player.getWorld(), player, itemData, player.getPos().clone().addY(3f), amount);
                    player.getWorld().addEntity(entity);
                }
            } catch (NumberFormatException ignored) {
KingRainbow44's avatar
KingRainbow44 committed
178
                CommandHandler.sendMessage(player, "Invalid item or player ID.");
KingRainbow44's avatar
KingRainbow44 committed
179
180
181
            }
        }
    }
SpikeHD's avatar
SpikeHD committed
182

183
    @Command(label = "givechar", aliases = { "givec" }, usage = "Usage: givechar <playerId> <avatarId> [level]")
SpikeHD's avatar
SpikeHD committed
184
    public static class GiveCharCommand implements CommandHandler {
185
        @Override public void execute(GenshinPlayer player, List<String> args) {
186
            int target, avatarID, level = 1, ascension = 1;
SpikeHD's avatar
SpikeHD committed
187

188
            if(args.size() < 2) {
SpikeHD's avatar
SpikeHD committed
189
                CommandHandler.sendMessage(null, "Usage: givechar <player> <avatarId> [level]");
190
191
192
193
194
                return;
            }
            
            switch(args.size()) {
                default:
SpikeHD's avatar
SpikeHD committed
195
                CommandHandler.sendMessage(null, "Usage: givechar <player> <avatarId> [level]");
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                    return;
                case 1:
                    try {
                        avatarID = Integer.parseInt(args.get(0));
                        target = player.getAccount().getPlayerId();
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from avatar name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid avatar id.");
                        return;
                    }
                    break;
                case 2:
                    try {
                        target = Integer.parseInt(args.get(0));
                        if(Grasscutter.getGameServer().getPlayerById(target) == null) {
                            target = player.getId(); level = Integer.parseInt(args.get(1));
                            avatarID = Integer.parseInt(args.get(0));
                        } else {
                            avatarID = Integer.parseInt(args.get(1));
                        }
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from avatar name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid avatar or player ID.");
                        return;
                    }
                    break;
                case 3:
                    try {
                        target = Integer.parseInt(args.get(0));
                        if(Grasscutter.getGameServer().getPlayerById(target) == null) {
                            CommandHandler.sendMessage(player, "Invalid player ID."); return;
                        }

                        avatarID = Integer.parseInt(args.get(1));
                        level = Integer.parseInt(args.get(2));
                    } catch (NumberFormatException ignored) {
                        // TODO: Parse from avatar name using GM Handbook.
                        CommandHandler.sendMessage(player, "Invalid avatar or player ID.");
                        return;
                    }
                    break;
            }
SpikeHD's avatar
SpikeHD committed
238

239
240
            GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
            if(targetPlayer == null) {
SpikeHD's avatar
SpikeHD committed
241
242
                CommandHandler.sendMessage(null, "Player not found."); return;
            }
243
                
SpikeHD's avatar
SpikeHD committed
244
245
246
247
248
            AvatarData avatarData = GenshinData.getAvatarDataMap().get(avatarID);
            if(avatarData == null) {
                CommandHandler.sendMessage(null, "Invalid avatar id."); return;
            }

249
250
251
252
253
254
255
            // Calculate ascension level.
            if (level <= 40) {
                ascension = (int)Math.ceil(level / 20);
            } else if (level > 20) {
                ascension = (int)Math.ceil(level / 10) - 3;
            }

SpikeHD's avatar
SpikeHD committed
256
257
            GenshinAvatar avatar = new GenshinAvatar(avatarID);
            avatar.setLevel(level);
258
            avatar.setPromoteLevel(ascension);
259
260
261
            
            // This will handle stats and talents
            avatar.recalcStats();
262
263
264
    
            targetPlayer.addAvatar(avatar);
        }
SpikeHD's avatar
SpikeHD committed
265

266
267
268
        @Override
        public void execute(List<String> args) {
            if(args.size() < 2) {
SpikeHD's avatar
SpikeHD committed
269
                CommandHandler.sendMessage(null, "Usage: givechar <player> <itemId|itemName> [amount]");
270
271
272
273
274
275
276
                return;
            }

            try {
                int target = Integer.parseInt(args.get(0));
                int avatarID = Integer.parseInt(args.get(1));
                int level = 1; if(args.size() > 2) level = Integer.parseInt(args.get(2));
277
                int ascension = 1;
278
279
280
281
282
283
284
285
286
287
288
                
                GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
                if(targetPlayer == null) {
                    CommandHandler.sendMessage(null, "Player not found."); return;
                }
                    
                AvatarData avatarData = GenshinData.getAvatarDataMap().get(avatarID);
                if(avatarData == null) {
                    CommandHandler.sendMessage(null, "Invalid avatar id."); return;
                }
                
289
290
291
292
293
294
295
                // Calculate ascension level.
                if (level <= 40) {
                    ascension = (int)Math.ceil(level / 20);
                } else if (level > 20) {
                    ascension = (int)Math.ceil(level / 10) - 3;
                }
                
296
297
                GenshinAvatar avatar = new GenshinAvatar(avatarID);
                avatar.setLevel(level);
298
                avatar.setPromoteLevel(ascension);
299
300
301

                // This will handle stats and talents
                avatar.recalcStats();
302
303
304
305
306
        
                targetPlayer.addAvatar(avatar);
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid item or player ID.");
            }
SpikeHD's avatar
SpikeHD committed
307
308
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
309
310
    
    @Command(label = "spawn", execution = Command.Execution.PLAYER, 
311
            usage = "spawn <entityId|entityName> [level] [amount]", description = "Spawns an entity near you", permission = "server.spawn")
KingRainbow44's avatar
KingRainbow44 committed
312
313
314
315
316
317
318
319
    public static class SpawnCommand implements CommandHandler {
        
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(null, "Usage: spawn <entityId|entityName> [amount]");
                return;
            }
320

KingRainbow44's avatar
KingRainbow44 committed
321
322
323
324
            try {
                int entity = Integer.parseInt(args.get(0));
                int level = 1; if(args.size() > 1) level = Integer.parseInt(args.get(1));
                int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2));
325

KingRainbow44's avatar
KingRainbow44 committed
326
327
328
329
                MonsterData entityData = GenshinData.getMonsterDataMap().get(entity);
                if(entityData == null) {
                    CommandHandler.sendMessage(null, "Invalid entity id."); return;
                }
330

KingRainbow44's avatar
KingRainbow44 committed
331
332
333
334
335
336
337
338
339
340
341
342
343
                float range = (5f + (.1f * amount));
                for (int i = 0; i < amount; i++) {
                    Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
                    EntityMonster monster = new EntityMonster(player.getWorld(), entityData, pos, level);
                    player.getWorld().addEntity(monster);
                }
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid item or player ID.");
            }
        }
    }
    
    @Command(label = "killall", 
344
            usage = "killall [sceneId]", description = "Kill all entities", permission = "server.killall")
KingRainbow44's avatar
KingRainbow44 committed
345
    public static class KillAllCommand implements CommandHandler {
346

KingRainbow44's avatar
KingRainbow44 committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            World world = player.getWorld();
            world.getEntities().values().stream()
                    .filter(entity -> entity instanceof EntityMonster)
                    .forEach(entity -> world.killEntity(entity, 0));
        }
        
        @Override
        public void execute(List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(null, "Usage: killall [sceneId]"); return;
            }
            
            try {
                int sceneId = Integer.parseInt(args.get(0));
                CommandHandler.sendMessage(null, "Killing all monsters in scene " + sceneId);
                // TODO: Implement getting worlds by scene ID.
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid scene id.");
            }
        }
    }
    
    @Command(label = "resetconst", aliases = {"resetconstellation"}, 
372
373
            usage = "resetconst [all]", execution = Command.Execution.PLAYER, permission = "player.resetconstellation",
            description = "Resets the constellation level on your current active character, will need to relog after using the command to see any changes.")
KingRainbow44's avatar
KingRainbow44 committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    public static class ResetConstellationCommand implements CommandHandler {
        
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() > 0 && args.get(0).equalsIgnoreCase("all")) {
                player.getAvatars().forEach(this::resetConstellation);
                player.dropMessage("Reset all avatars' constellations.");
            } else {
                EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity(); 
                if(entity == null)
                    return;
                
                GenshinAvatar avatar = entity.getAvatar();
                this.resetConstellation(avatar);
388

KingRainbow44's avatar
KingRainbow44 committed
389
390
391
392
393
394
395
396
397
398
399
400
401
                player.dropMessage("Constellations for " + avatar.getAvatarData().getName() + " have been reset. Please relog to see changes.");
            }
        }
        
        private void resetConstellation(GenshinAvatar avatar) {
            avatar.getTalentIdList().clear();
            avatar.setCoreProudSkillLevel(0);
            avatar.recalcStats();
            avatar.save();
        }
    }
    
    @Command(label = "godmode",
402
            usage = "godmode", execution = Command.Execution.PLAYER, description = "Prevents you from taking damage", permission = "player.godmode")
KingRainbow44's avatar
KingRainbow44 committed
403
404
405
406
407
408
409
410
411
412
    public static class GodModeCommand implements CommandHandler {
        
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            player.setGodmode(!player.inGodmode());
            player.dropMessage("Godmode is now " + (player.inGodmode() ? "enabled" : "disabled") + ".");
        }
    }
    
    @Command(label = "sethealth", aliases = {"sethp"}, 
413
414
            usage = "sethealth <hp>", execution = Command.Execution.PLAYER, description = "Sets your health to the specified value",
            permission = "player.sethealth")
KingRainbow44's avatar
KingRainbow44 committed
415
    public static class SetHealthCommand implements CommandHandler {
416

KingRainbow44's avatar
KingRainbow44 committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(null, "Usage: sethealth <hp>"); return;
            }
            
            try {
                int health = Integer.parseInt(args.get(0));
                EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity();
                if(entity == null)
                    return;
                
                entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, health);
                entity.getWorld().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, FightProperty.FIGHT_PROP_CUR_HP));
                player.dropMessage("Health set to " + health + ".");
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid health value.");
            }
        }
    }
SpikeHD's avatar
SpikeHD committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

    @Command(label = "setworldlevel", aliases = {"setworldlvl"}, 
            usage = "Usage: setworldlevel <level>", execution = Command.Execution.PLAYER)
    public static class SetWorldLevelCommand implements CommandHandler {
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            if(args.size() < 1) {
                CommandHandler.sendMessage(null, "Usage: setworldlevel <level>"); return;
            }
            
            try {
                int level = Integer.parseInt(args.get(0));

                // Set in both world and player props
                player.getWorld().setWorldLevel(level);
                player.setProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL, level);

                player.dropMessage("World level set to " + level + ".");
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(null, "Invalid world level.");
            }
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
460
461
    
    @Command(label = "clearartifacts", aliases = {"clearart"}, 
462
463
            usage = "clearartifacts", execution = Command.Execution.PLAYER, permission = "player.clearartifacts",
            description = "Deletes all unequipped and unlocked level 0 artifacts, including yellow rarity ones from your inventory")
KingRainbow44's avatar
KingRainbow44 committed
464
    public static class ClearArtifactsCommand implements CommandHandler {
465

KingRainbow44's avatar
KingRainbow44 committed
466
467
468
469
470
471
472
473
474
475
        @Override
        public void execute(GenshinPlayer player, List<String> args) {
            Inventory playerInventory = player.getInventory();
            playerInventory.getItems().values().stream()
                    .filter(item -> item.getItemType() == ItemType.ITEM_RELIQUARY)
                    .filter(item -> item.getLevel() == 1 && item.getExp() == 0)
                    .filter(item -> !item.isLocked() && !item.isEquipped())
                    .forEach(item -> playerInventory.removeItem(item, item.getCount()));
        }
    }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

    @Command(label = "sendservermessage", aliases = {"sendservmsg"},
            usage = "sendservermessage <player> <message>", description = "Sends a message to a player as the server",
            execution = Command.Execution.PLAYER, permission = "server.sendmessage")
    public static class SendServerMessageCommand implements CommandHandler {
        @Override
        public void execute(GenshinPlayer player, 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()));

                GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
                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.");
            }
        }
    }
Melledy's avatar
Melledy committed
503
}