KillAllCommand.java 2.12 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;

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

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

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