GiveCommand.java 6.42 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
6
import emu.grasscutter.data.GameData;
KingRainbow44's avatar
KingRainbow44 committed
7
import emu.grasscutter.data.def.ItemData;
8
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
14
15
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.server.packet.send.PacketItemAddHintNotify;

import java.util.LinkedList;
import java.util.List;

memetrollsXD's avatar
memetrollsXD committed
16
17
@Command(label = "give", usage = "give [player] <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
        "g", "item", "giveitem" }, permission = "player.give")
KingRainbow44's avatar
KingRainbow44 committed
18
19
20
public final class GiveCommand implements CommandHandler {

    @Override
21
    public void execute(Player sender, List<String> args) {
memetrollsXD's avatar
memetrollsXD committed
22
        int target, item, lvl, amount = 1;
KingRainbow44's avatar
KingRainbow44 committed
23
        if (sender == null && args.size() < 2) {
memetrollsXD's avatar
memetrollsXD committed
24
            CommandHandler.sendMessage(null, "Usage: give <player> <itemId|itemName> [amount] [level]");
KingRainbow44's avatar
KingRainbow44 committed
25
26
27
28
29
30
31
32
33
34
35
            return;
        }

        switch (args.size()) {
            default: // *No args*
                CommandHandler.sendMessage(sender, "Usage: give [player] <itemId|itemName> [amount]");
                return;
            case 1: // <itemId|itemName>
                try {
                    item = Integer.parseInt(args.get(0));
                    target = sender.getUid();
memetrollsXD's avatar
memetrollsXD committed
36
                    lvl = 1;
KingRainbow44's avatar
KingRainbow44 committed
37
38
39
40
41
42
43
44
45
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item id.");
                    return;
                }
                break;
            case 2: // <itemId|itemName> [amount] | [player] <itemId|itemName>
                try {
                    target = Integer.parseInt(args.get(0));
memetrollsXD's avatar
memetrollsXD committed
46
                    lvl = 1;
KingRainbow44's avatar
KingRainbow44 committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60

                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
                        target = sender.getUid();
                        item = Integer.parseInt(args.get(0));
                        amount = Integer.parseInt(args.get(1));
                    } else {
                        item = Integer.parseInt(args.get(1));
                    }
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item or player ID.");
                    return;
                }
                break;
memetrollsXD's avatar
memetrollsXD committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
            case 3: // [player] <itemId|itemName> [amount] | <itemId|itemName> [amount] [level]
                try {
                    target = Integer.parseInt(args.get(0));

                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
                        target = sender.getUid();
                        item = Integer.parseInt(args.get(0));
                        amount = Integer.parseInt(args.get(1));
                        lvl = Integer.parseInt(args.get(2));
                    } else {
                        item = Integer.parseInt(args.get(1));
                        amount = Integer.parseInt(args.get(2));
                        lvl = 1;
                    }

                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item or player ID.");
                    return;
                }
                break;
            case 4: // [player] <itemId|itemName> [amount] [level]
KingRainbow44's avatar
KingRainbow44 committed
83
84
85
86
87
88
                try {
                    target = Integer.parseInt(args.get(0));

                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
                        CommandHandler.sendMessage(sender, "Invalid player ID.");
                        return;
memetrollsXD's avatar
memetrollsXD committed
89
90
91
92
                    } else {
                        item = Integer.parseInt(args.get(1));
                        amount = Integer.parseInt(args.get(2));
                        lvl = Integer.parseInt(args.get(3));
KingRainbow44's avatar
KingRainbow44 committed
93
94
95
96
97
98
99
100
101
                    }
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item or player ID.");
                    return;
                }
                break;
        }

102
        Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
KingRainbow44's avatar
KingRainbow44 committed
103
104
105
106
107
108

        if (targetPlayer == null) {
            CommandHandler.sendMessage(sender, "Player not found.");
            return;
        }

109
        ItemData itemData = GameData.getItemDataMap().get(item);
KingRainbow44's avatar
KingRainbow44 committed
110
111
112
113
114
        if (itemData == null) {
            CommandHandler.sendMessage(sender, "Invalid item id.");
            return;
        }

memetrollsXD's avatar
memetrollsXD committed
115
        this.item(targetPlayer, itemData, amount, lvl);
KingRainbow44's avatar
KingRainbow44 committed
116

memetrollsXD's avatar
memetrollsXD committed
117
118
119
120
121
        if (!itemData.isEquip())
            CommandHandler.sendMessage(sender, String.format("Given %s of %s to %s.", amount, item, target));
        else
            CommandHandler.sendMessage(sender,
                    String.format("Given %s with level %s %s times to %s", item, lvl, amount, target));
KingRainbow44's avatar
KingRainbow44 committed
122
123
    }

124
    private void item(Player player, ItemData itemData, int amount, int lvl) {
KingRainbow44's avatar
KingRainbow44 committed
125
        if (itemData.isEquip()) {
126
            List<GameItem> items = new LinkedList<>();
memetrollsXD's avatar
memetrollsXD committed
127
            for (int i = 0; i < amount; i++) {
128
                GameItem item = new GameItem(itemData);
memetrollsXD's avatar
memetrollsXD committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                item.setCount(amount);
                item.setLevel(lvl);
                item.setPromoteLevel(0);
                if (lvl > 20) { // 20/40
                    item.setPromoteLevel(1);
                } else if (lvl > 40) { // 40/50
                    item.setPromoteLevel(2);
                } else if (lvl > 50) { // 50/60
                    item.setPromoteLevel(3);
                } else if (lvl > 60) { // 60/70
                    item.setPromoteLevel(4);
                } else if (lvl > 70) { // 70/80
                    item.setPromoteLevel(5);
                } else if (lvl > 80) { // 80/90
                    item.setPromoteLevel(6);
                }
                items.add(item);
            }
147
            player.getInventory().addItems(items, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
148
        } else {
149
150
151
            GameItem item = new GameItem(itemData);
            item.setCount(amount);
            player.getInventory().addItem(item, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
152
153
154
        }
    }
}