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

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
5
import emu.grasscutter.data.GameData;
KingRainbow44's avatar
KingRainbow44 committed
6
7
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.entity.EntityMonster;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11
12
13
14
15
16
17
import emu.grasscutter.utils.Position;

import java.util.List;

@Command(label = "spawn", usage = "spawn <entityId|entityName> [level] [amount]",
        description = "Spawns an entity near you", permission = "server.spawn")
public final class SpawnCommand implements CommandHandler {

    @Override
18
    public void execute(Player sender, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        if (sender == null) {
            CommandHandler.sendMessage(null, "Run this command in-game.");
            return;
        }

        if (args.size() < 1) {
            CommandHandler.sendMessage(sender, "Usage: spawn <entityId|entityName> [amount]");
            return;
        }

        try {
            int entity = Integer.parseInt(args.get(0));
            int level = args.size() > 1 ? Integer.parseInt(args.get(1)) : 1;
            int amount = args.size() > 2 ? Integer.parseInt(args.get(2)) : 1;

34
            MonsterData entityData = GameData.getMonsterDataMap().get(entity);
KingRainbow44's avatar
KingRainbow44 committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            if (entityData == null) {
                CommandHandler.sendMessage(sender, "Invalid entity id.");
                return;
            }

            float range = (5f + (.1f * amount));
            for (int i = 0; i < amount; i++) {
                Position pos = sender.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
                EntityMonster monster = new EntityMonster(sender.getScene(), entityData, pos, level);
                sender.getScene().addEntity(monster);
            }
            CommandHandler.sendMessage(sender, String.format("Spawned %s of %s.", amount, entity));
        } catch (NumberFormatException ignored) {
            CommandHandler.sendMessage(sender, "Invalid item or player ID.");
        }
    }
}