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

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.entity.EntityMonster;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.entity.GameEntity;
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

import java.util.List;

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

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

    @Override
AnimeGitB's avatar
AnimeGitB committed
18
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
19
        Scene scene = targetPlayer.getScene();
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
23
        try {
            switch (args.size()) {
                case 0: // *No args*
                    break;
AnimeGitB's avatar
AnimeGitB committed
24
25
                case 1: // [sceneId]
                    scene = targetPlayer.getWorld().getSceneById(Integer.parseInt(args.get(0)));
KingRainbow44's avatar
KingRainbow44 committed
26
27
                    break;
                default:
28
                    sendUsageMessage(sender);
KingRainbow44's avatar
KingRainbow44 committed
29
30
31
                    return;
            }
        } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
32
            CommandHandler.sendMessage(sender, translate(sender, "commands.execution.argument_error"));
KingRainbow44's avatar
KingRainbow44 committed
33
        }
AnimeGitB's avatar
AnimeGitB committed
34
        if (scene == null) {
tester233's avatar
tester233 committed
35
            CommandHandler.sendMessage(sender, translate(sender, "commands.killall.scene_not_found_in_player_world"));
AnimeGitB's avatar
AnimeGitB committed
36
37
38
39
40
41
42
43
            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();
44
        toKill.forEach(entity -> sceneF.killEntity(entity, 0));
tester233's avatar
tester233 committed
45
        CommandHandler.sendMessage(sender, translate(sender, "commands.killall.kill_monsters_in_scene", Integer.toString(toKill.size()), Integer.toString(scene.getId())));
KingRainbow44's avatar
KingRainbow44 committed
46
47
    }
}