GiveCommand.java 8.59 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;
9
import emu.grasscutter.game.inventory.ItemType;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
11
12
13
14
15
import emu.grasscutter.game.props.ActionReason;

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

memetrollsXD's avatar
memetrollsXD committed
16
@Command(label = "give", usage = "give [player] <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
17
        "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) {
22
        int target, item, lvl, amount = 1, refinement = 0;
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
            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;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
            case 4: // [player] <itemId|itemName> [amount] [level] | <itemId|itemName> [amount] [level] [refinement]
                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));
                        refinement = Integer.parseInt(args.get(3));
                    } else {
                        item = Integer.parseInt(args.get(1));
                        amount = Integer.parseInt(args.get(2));
                        lvl = Integer.parseInt(args.get(3));
                    }
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item or player ID.");
                    return;
                }
                break;
            case 5: // [player] <itemId|itemName> [amount] [level] [refinement]
KingRainbow44's avatar
KingRainbow44 committed
104
105
106
107
108
109
                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
110
111
112
113
                    } else {
                        item = Integer.parseInt(args.get(1));
                        amount = Integer.parseInt(args.get(2));
                        lvl = Integer.parseInt(args.get(3));
114
                        refinement = Integer.parseInt(args.get(4));
KingRainbow44's avatar
KingRainbow44 committed
115
116
117
118
119
120
121
122
123
                    }
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid item or player ID.");
                    return;
                }
                break;
        }

124
        Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
KingRainbow44's avatar
KingRainbow44 committed
125
126
127
128
129
130

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

131
        ItemData itemData = GameData.getItemDataMap().get(item);
KingRainbow44's avatar
KingRainbow44 committed
132
133
134
135
        if (itemData == null) {
            CommandHandler.sendMessage(sender, "Invalid item id.");
            return;
        }
136
137
138
139
140
141
142
143
144
145
146
        if (refinement != 0) {
            if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
                if (refinement < 1 || refinement > 5) {
                    CommandHandler.sendMessage(sender, "Refinement must be between 1 and 5.");
                    return;
                }
            } else {
                CommandHandler.sendMessage(sender, "Refinement is only applicable to weapons.");
                return;
            }
        }
KingRainbow44's avatar
KingRainbow44 committed
147

148
        this.item(targetPlayer, itemData, amount, lvl, refinement);
KingRainbow44's avatar
KingRainbow44 committed
149

150
        if (!itemData.isEquip()) {
memetrollsXD's avatar
memetrollsXD committed
151
            CommandHandler.sendMessage(sender, String.format("Given %s of %s to %s.", amount, item, target));
152
153
154
155
        } else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
            CommandHandler.sendMessage(sender,
                    String.format("Given %s with level %s, refinement %s %s times to %s", item, lvl, refinement, amount, target));
        } else {
memetrollsXD's avatar
memetrollsXD committed
156
157
            CommandHandler.sendMessage(sender,
                    String.format("Given %s with level %s %s times to %s", item, lvl, amount, target));
158
        }
KingRainbow44's avatar
KingRainbow44 committed
159
160
    }

161
    private void item(Player player, ItemData itemData, int amount, int lvl, int refinement) {
KingRainbow44's avatar
KingRainbow44 committed
162
        if (itemData.isEquip()) {
163
            List<GameItem> items = new LinkedList<>();
memetrollsXD's avatar
memetrollsXD committed
164
            for (int i = 0; i < amount; i++) {
165
                GameItem item = new GameItem(itemData);
memetrollsXD's avatar
memetrollsXD committed
166
167
                item.setCount(amount);
                item.setLevel(lvl);
168
                if (lvl > 20 && lvl < 40) {
memetrollsXD's avatar
memetrollsXD committed
169
                    item.setPromoteLevel(1);
170
                } else if (lvl > 40 && lvl <= 50) {
memetrollsXD's avatar
memetrollsXD committed
171
                    item.setPromoteLevel(2);
172
                } else if (lvl > 50 && lvl <= 60) {
memetrollsXD's avatar
memetrollsXD committed
173
                    item.setPromoteLevel(3);
174
                } else if (lvl > 60 && lvl <= 70) {
memetrollsXD's avatar
memetrollsXD committed
175
                    item.setPromoteLevel(4);
176
                } else if (lvl > 70 && lvl <= 80) {
memetrollsXD's avatar
memetrollsXD committed
177
                    item.setPromoteLevel(5);
178
                } else if (lvl > 80 && lvl <= 90) {
memetrollsXD's avatar
memetrollsXD committed
179
180
                    item.setPromoteLevel(6);
                }
181
182
183
184
185
186
187
                if (item.getItemType() == ItemType.ITEM_WEAPON) {
                    if (refinement > 0) {
                        item.setRefinement(refinement - 1);
                    } else {
                        item.setRefinement(0);
                    }
                }
memetrollsXD's avatar
memetrollsXD committed
188
189
                items.add(item);
            }
190
            player.getInventory().addItems(items, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
191
        } else {
192
193
194
            GameItem item = new GameItem(itemData);
            item.setCount(amount);
            player.getInventory().addItem(item, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
195
196
197
        }
    }
}