KillAllCommand.java 2.72 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.GenshinScene;
import emu.grasscutter.game.entity.EntityMonster;

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
    public void execute(GenshinPlayer sender, List<String> args) {
        GenshinScene scene;
        GenshinPlayer genshinPlayer;

        try {
            switch (args.size()) {
                case 0: // *No args*
                    if (sender == null) {
                        CommandHandler.sendMessage(null, "Usage: killall [playerUid] [sceneId]");
                        return;
                    }
                    scene = sender.getScene();
                    break;
                case 1: // [playerUid]
                    genshinPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
                    if (genshinPlayer == null) {
                        CommandHandler.sendMessage(sender, "Player not found or offline.");
                        return;
                    }
                    scene = genshinPlayer.getScene();
                    break;
                case 2: // [playerUid] [sceneId]
                    genshinPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
                    if (genshinPlayer == null) {
                        CommandHandler.sendMessage(sender, "Player not found or offline.");
                        return;
                    }
                    GenshinScene genshinScene = sender.getWorld().getSceneById(Integer.parseInt(args.get(1)));
                    if (genshinScene == null) {
                        CommandHandler.sendMessage(sender, "Scene not found in player world");
                        return;
                    }
                    scene = genshinScene;
                    break;
                default:
                    CommandHandler.sendMessage(sender, "Usage: killall [playerUid] [sceneId]");
                    return;
            }

            scene.getEntities().values().stream()
                    .filter(entity -> entity instanceof EntityMonster)
                    .forEach(entity -> scene.killEntity(entity, 0));
            CommandHandler.sendMessage(sender, "Killing all monsters in scene " + scene.getId());
        } catch (NumberFormatException ignored) {
            CommandHandler.sendMessage(sender, "Invalid arguments.");
        }
    }
}