KillAllCommand.java 2.69 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
8
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.world.Scene;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11
12
13
14
15
16

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
17
18
19
    public void execute(Player sender, List<String> args) {
        Scene mainScene;
        Player targetPlayer;
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
23
24
25
26
27

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

56
            mainScene.getEntities().values().stream()
KingRainbow44's avatar
KingRainbow44 committed
57
                    .filter(entity -> entity instanceof EntityMonster)
58
59
                    .forEach(entity -> mainScene.killEntity(entity, 0));
            CommandHandler.sendMessage(sender, "Killing all monsters in scene " + mainScene.getId());
KingRainbow44's avatar
KingRainbow44 committed
60
61
62
63
64
        } catch (NumberFormatException ignored) {
            CommandHandler.sendMessage(sender, "Invalid arguments.");
        }
    }
}