DropCommand.java 2.83 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.command.commands;

方块君's avatar
方块君 committed
3
import emu.grasscutter.Grasscutter;
KingRainbow44's avatar
KingRainbow44 committed
4
5
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
6
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.data.excels.ItemData;
KingRainbow44's avatar
KingRainbow44 committed
8
import emu.grasscutter.game.entity.EntityItem;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
import emu.grasscutter.utils.Position;

import java.util.List;

14
15
import static emu.grasscutter.utils.Language.translate;

16
@Command(label = "drop", usage = "drop <itemId|itemName> [amount]", aliases = {"d", "dropitem"}, permission = "server.drop", permissionTargeted = "server.drop.others", description = "commands.drop.description")
KingRainbow44's avatar
KingRainbow44 committed
17
18
19
public final class DropCommand implements CommandHandler {

    @Override
AnimeGitB's avatar
AnimeGitB committed
20
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
21
22
        int item = 0;
        int amount = 1;
KingRainbow44's avatar
KingRainbow44 committed
23

AnimeGitB's avatar
AnimeGitB committed
24
25
26
27
28
        switch (args.size()) {
            case 2:
                try {
                    amount = Integer.parseInt(args.get(1));
                } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
29
                    CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.amount"));
AnimeGitB's avatar
AnimeGitB committed
30
                    return;
31
                }  // Slightly cheeky here: no break, so it falls through to initialize the first argument too
AnimeGitB's avatar
AnimeGitB committed
32
33
34
35
            case 1:
                try {
                    item = Integer.parseInt(args.get(0));
                } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
36
                    CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.itemId"));
AnimeGitB's avatar
AnimeGitB committed
37
38
39
40
                    return;
                }
                break;
            default:
Secretboy's avatar
Secretboy committed
41
                CommandHandler.sendMessage(sender, translate(sender, "commands.drop.command_usage"));
KingRainbow44's avatar
KingRainbow44 committed
42
                return;
AnimeGitB's avatar
AnimeGitB committed
43
        }
KingRainbow44's avatar
KingRainbow44 committed
44

AnimeGitB's avatar
AnimeGitB committed
45
46
        ItemData itemData = GameData.getItemDataMap().get(item);
        if (itemData == null) {
Secretboy's avatar
Secretboy committed
47
            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.itemId"));
AnimeGitB's avatar
AnimeGitB committed
48
49
50
51
52
53
54
55
            return;
        }
        if (itemData.isEquip()) {
            float range = (5f + (.1f * amount));
            for (int i = 0; i < amount; i++) {
                Position pos = targetPlayer.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
                EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, pos, 1);
                targetPlayer.getScene().addEntity(entity);
KingRainbow44's avatar
KingRainbow44 committed
56
            }
AnimeGitB's avatar
AnimeGitB committed
57
58
59
        } else {
            EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, targetPlayer.getPos().clone().addY(3f), amount);
            targetPlayer.getScene().addEntity(entity);
KingRainbow44's avatar
KingRainbow44 committed
60
        }
Secretboy's avatar
Secretboy committed
61
        CommandHandler.sendMessage(sender, translate(sender, "commands.drop.success", Integer.toString(amount), Integer.toString(item)));
KingRainbow44's avatar
KingRainbow44 committed
62
    }
Secretboy's avatar
Secretboy committed
63
}