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

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

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