DropCommand.java 2.24 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.ItemData;
import emu.grasscutter.game.entity.EntityItem;
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 = "drop", usage = "drop <itemId|itemName> [amount]",
        description = "Drops an item near you", aliases = {"d", "dropitem"}, permission = "server.drop")
public final class DropCommand 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: 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));

34
            ItemData itemData = GameData.getItemDataMap().get(item);
KingRainbow44's avatar
KingRainbow44 committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
            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.");
        }
    }
Jaida Wu's avatar
Jaida Wu committed
56
}