KillAllCommand.java 2.17 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", permissionTargeted = "server.killall.others", description = "commands.kill.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
        if (targetPlayer == null) {
21
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
22
23
            return;
        }
KingRainbow44's avatar
KingRainbow44 committed
24

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