DropCommand.java 2.91 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;

KingRainbow44's avatar
KingRainbow44 committed
16
17
18
19
20
@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
AnimeGitB's avatar
AnimeGitB committed
21
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
22
        if (targetPlayer == null) {
23
            CommandHandler.sendMessage(null, translate("commands.execution.need_target"));
KingRainbow44's avatar
KingRainbow44 committed
24
25
            return;
        }
AnimeGitB's avatar
AnimeGitB committed
26
27
28
        
        int item = 0;
        int amount = 1;
KingRainbow44's avatar
KingRainbow44 committed
29

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

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