Spawn.java 2.1 KB
Newer Older
Jaida Wu's avatar
Jaida Wu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package emu.grasscutter.command.commands;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityMonster;
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 class Spawn implements CommandHandler {

    @Override
KingRainbow44's avatar
KingRainbow44 committed
18
    public void execute(GenshinPlayer sender, List<String> args) {
Jaida Wu's avatar
Jaida Wu committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        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;

            MonsterData entityData = GenshinData.getMonsterDataMap().get(entity);
            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.");
        }
    }
}