KillAllCommand.java 2.19 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

import java.util.List;

13
14
import static emu.grasscutter.utils.Language.translate;

15
@Command(label = "killall", usage = "killall [sceneId]", permission = "server.killall")
KingRainbow44's avatar
KingRainbow44 committed
16
17
public final class KillAllCommand implements CommandHandler {

18
19
20
21
22
    @Override
    public String description() {
        return translate("commands.kill.description");
    }

KingRainbow44's avatar
KingRainbow44 committed
23
    @Override
AnimeGitB's avatar
AnimeGitB committed
24
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
25
        if (targetPlayer == null) {
26
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
27
28
            return;
        }
KingRainbow44's avatar
KingRainbow44 committed
29

AnimeGitB's avatar
AnimeGitB committed
30
        Scene scene = targetPlayer.getScene();
KingRainbow44's avatar
KingRainbow44 committed
31
32
33
34
        try {
            switch (args.size()) {
                case 0: // *No args*
                    break;
AnimeGitB's avatar
AnimeGitB committed
35
36
                case 1: // [sceneId]
                    scene = targetPlayer.getWorld().getSceneById(Integer.parseInt(args.get(0)));
KingRainbow44's avatar
KingRainbow44 committed
37
38
                    break;
                default:
39
                    CommandHandler.sendMessage(sender, translate("commands.kill.usage"));
KingRainbow44's avatar
KingRainbow44 committed
40
41
42
                    return;
            }
        } catch (NumberFormatException ignored) {
43
            CommandHandler.sendMessage(sender, translate("commands.execution.argument_error"));
KingRainbow44's avatar
KingRainbow44 committed
44
        }
AnimeGitB's avatar
AnimeGitB committed
45
        if (scene == null) {
46
            CommandHandler.sendMessage(sender, translate("commands.kill.scene_not_found_in_player_world"));
AnimeGitB's avatar
AnimeGitB committed
47
48
49
50
51
52
53
54
            return;
        }

        // Separate into list to avoid concurrency issue
        final Scene sceneF = scene;
        List<GameEntity> toKill = sceneF.getEntities().values().stream()
                .filter(entity -> entity instanceof EntityMonster)
                .toList();
55
56
        toKill.forEach(entity -> sceneF.killEntity(entity, 0));
        CommandHandler.sendMessage(sender, translate("commands.kill.kill_monsters_in_scene", Integer.toString(toKill.size()), Integer.toString(scene.getId())));
KingRainbow44's avatar
KingRainbow44 committed
57
58
    }
}