DropCommand.java 2.95 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;
KingRainbow44's avatar
KingRainbow44 committed
7
8
import emu.grasscutter.data.def.ItemData;
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
        if (targetPlayer == null) {
22
            CommandHandler.sendMessage(null, translate("commands.execution.need_target"));
KingRainbow44's avatar
KingRainbow44 committed
23
24
            return;
        }
AnimeGitB's avatar
AnimeGitB committed
25
26
27
        
        int item = 0;
        int amount = 1;
KingRainbow44's avatar
KingRainbow44 committed
28

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

AnimeGitB's avatar
AnimeGitB committed
50
51
        ItemData itemData = GameData.getItemDataMap().get(item);
        if (itemData == null) {
52
            CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
AnimeGitB's avatar
AnimeGitB committed
53
54
55
56
57
58
59
60
            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
61
            }
AnimeGitB's avatar
AnimeGitB committed
62
63
64
        } else {
            EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, targetPlayer.getPos().clone().addY(3f), amount);
            targetPlayer.getScene().addEntity(entity);
KingRainbow44's avatar
KingRainbow44 committed
65
        }
66
        CommandHandler.sendMessage(sender, translate("commands.drop.success", Integer.toString(amount), Integer.toString(item)));
KingRainbow44's avatar
KingRainbow44 committed
67
    }
Jaida Wu's avatar
Jaida Wu committed
68
}