Drop.java 2.3 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.ItemData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.utils.Position;

import java.util.List;

@Command(label = "drop", usage = "drop <itemId|itemName> [amount]",
        description = "Drops an item near you", aliases = {"d", "dropitem"}, permission = "server.drop")
public class Drop 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
52
53
54
55
56
        if (sender == null) {
            CommandHandler.sendMessage(null, "Run this command in-game.");
            return;
        }

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

        try {
            int item = Integer.parseInt(args.get(0));
            int amount = 1;
            if (args.size() > 1) amount = Integer.parseInt(args.get(1));

            ItemData itemData = GenshinData.getItemDataMap().get(item);
            if (itemData == null) {
                CommandHandler.sendMessage(sender, "Invalid item id.");
                return;
            }

            if (itemData.isEquip()) {
                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));
                    EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, pos, 1);
                    sender.getScene().addEntity(entity);
                }
            } else {
                EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, sender.getPos().clone().addY(3f), amount);
                sender.getScene().addEntity(entity);
            }
            CommandHandler.sendMessage(sender, String.format("Dropped %s of %s.", amount, item));
        } catch (NumberFormatException ignored) {
            CommandHandler.sendMessage(sender, "Invalid item or player ID.");
        }
    }
}