KillAllCommand.java 2.87 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
6
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.entity.EntityMonster;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.entity.GameEntity;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.world.Scene;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
14
15
16
17

import java.util.List;

@Command(label = "killall", usage = "killall [playerUid] [sceneId]",
        description = "Kill all entities", permission = "server.killall")
public final class KillAllCommand implements CommandHandler {

    @Override
18
19
20
    public void execute(Player sender, List<String> args) {
        Scene mainScene;
        Player targetPlayer;
KingRainbow44's avatar
KingRainbow44 committed
21
22
23
24
25
26
27
28

        try {
            switch (args.size()) {
                case 0: // *No args*
                    if (sender == null) {
                        CommandHandler.sendMessage(null, "Usage: killall [playerUid] [sceneId]");
                        return;
                    }
29
                    mainScene = sender.getScene();
KingRainbow44's avatar
KingRainbow44 committed
30
31
                    break;
                case 1: // [playerUid]
32
33
                    targetPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
                    if (targetPlayer == null) {
KingRainbow44's avatar
KingRainbow44 committed
34
35
36
                        CommandHandler.sendMessage(sender, "Player not found or offline.");
                        return;
                    }
37
                    mainScene = targetPlayer.getScene();
KingRainbow44's avatar
KingRainbow44 committed
38
39
                    break;
                case 2: // [playerUid] [sceneId]
40
41
                    targetPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
                    if (targetPlayer == null) {
KingRainbow44's avatar
KingRainbow44 committed
42
43
44
                        CommandHandler.sendMessage(sender, "Player not found or offline.");
                        return;
                    }
45
46
                    Scene scene = sender.getWorld().getSceneById(Integer.parseInt(args.get(1)));
                    if (scene == null) {
KingRainbow44's avatar
KingRainbow44 committed
47
48
49
                        CommandHandler.sendMessage(sender, "Scene not found in player world");
                        return;
                    }
50
                    mainScene = scene;
KingRainbow44's avatar
KingRainbow44 committed
51
52
53
54
55
56
                    break;
                default:
                    CommandHandler.sendMessage(sender, "Usage: killall [playerUid] [sceneId]");
                    return;
            }

Melledy's avatar
Melledy committed
57
58
            // Separate into list to avoid concurrency issue
            List<GameEntity> toKill = mainScene.getEntities().values().stream()
KingRainbow44's avatar
KingRainbow44 committed
59
                    .filter(entity -> entity instanceof EntityMonster)
Melledy's avatar
Melledy committed
60
61
62
                    .toList();
            toKill.stream().forEach(entity -> mainScene.killEntity(entity, 0));
            CommandHandler.sendMessage(sender, "Killing " + toKill.size() + " monsters in scene " + mainScene.getId());
KingRainbow44's avatar
KingRainbow44 committed
63
64
65
66
67
        } catch (NumberFormatException ignored) {
            CommandHandler.sendMessage(sender, "Invalid arguments.");
        }
    }
}