GiveCommand.java 7.41 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
import emu.grasscutter.game.props.ActionReason;

import java.util.LinkedList;
import java.util.List;
15
16
import java.util.regex.Pattern;
import java.util.regex.Matcher;
KingRainbow44's avatar
KingRainbow44 committed
17

18
19
import static emu.grasscutter.utils.Language.translate;

AnimeGitB's avatar
AnimeGitB committed
20
@Command(label = "give", usage = "give <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
21
        "g", "item", "giveitem"}, permission = "player.give")
KingRainbow44's avatar
KingRainbow44 committed
22
public final class GiveCommand implements CommandHandler {
23
24
    Pattern lvlRegex = Pattern.compile("l(?:vl?)?(\\d+)");  // Java is a joke of a proglang that doesn't have raw string literals
    Pattern refineRegex = Pattern.compile("r(\\d+)");
25
    Pattern amountRegex = Pattern.compile("((?<=x)\\d+|\\d+(?=x)(?!x\\d))");
26
27
28
29
30
31
32
33

    private int matchIntOrNeg(Pattern pattern, String arg) {
        Matcher match = pattern.matcher(arg);
        if (match.find()) {
            return Integer.parseInt(match.group(1));  // This should be exception-safe as only \d+ can be passed to it (i.e. non-empty string of pure digits)
        }
        return -1;
    }
KingRainbow44's avatar
KingRainbow44 committed
34
35

    @Override
AnimeGitB's avatar
AnimeGitB committed
36
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
37
        if (targetPlayer == null) {
38
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
KingRainbow44's avatar
KingRainbow44 committed
39
40
            return;
        }
AnimeGitB's avatar
AnimeGitB committed
41
42
43
44
        int item;
        int lvl = 1;
        int amount = 1;
        int refinement = 0;
KingRainbow44's avatar
KingRainbow44 committed
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        for (int i = args.size()-1; i>=0; i--) {  // Reverse iteration as we are deleting elements
            String arg = args.get(i).toLowerCase();
            boolean deleteArg = false;
            int argNum;
            if ((argNum = matchIntOrNeg(lvlRegex, arg)) != -1) {
                lvl = argNum;
                deleteArg = true;
            }
            if ((argNum = matchIntOrNeg(refineRegex, arg)) != -1) {
                refinement = argNum;
                deleteArg = true;
            }
            if ((argNum = matchIntOrNeg(amountRegex, arg)) != -1) {
                amount = argNum;
                deleteArg = true;
            }
            if (deleteArg) {
                args.remove(i);
            }
        }

KingRainbow44's avatar
KingRainbow44 committed
67
        switch (args.size()) {
AnimeGitB's avatar
AnimeGitB committed
68
            case 4: // <itemId|itemName> [amount] [level] [refinement]
KingRainbow44's avatar
KingRainbow44 committed
69
                try {
AnimeGitB's avatar
AnimeGitB committed
70
                    refinement = Integer.parseInt(args.get(3));
KingRainbow44's avatar
KingRainbow44 committed
71
                } catch (NumberFormatException ignored) {
72
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemRefinement"));
KingRainbow44's avatar
KingRainbow44 committed
73
                    return;
AnimeGitB's avatar
AnimeGitB committed
74
75
                }  // Fallthrough
            case 3: // <itemId|itemName> [amount] [level]
memetrollsXD's avatar
memetrollsXD committed
76
                try {
AnimeGitB's avatar
AnimeGitB committed
77
                    lvl = Integer.parseInt(args.get(2));
memetrollsXD's avatar
memetrollsXD committed
78
                } catch (NumberFormatException ignored) {
79
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemLevel"));
memetrollsXD's avatar
memetrollsXD committed
80
                    return;
AnimeGitB's avatar
AnimeGitB committed
81
82
                }  // Fallthrough
            case 2: // <itemId|itemName> [amount]
83
                try {
AnimeGitB's avatar
AnimeGitB committed
84
                    amount = Integer.parseInt(args.get(1));
85
                } catch (NumberFormatException ignored) {
86
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.amount"));
87
                    return;
AnimeGitB's avatar
AnimeGitB committed
88
89
                }  // Fallthrough
            case 1: // <itemId|itemName>
KingRainbow44's avatar
KingRainbow44 committed
90
                try {
AnimeGitB's avatar
AnimeGitB committed
91
                    item = Integer.parseInt(args.get(0));
KingRainbow44's avatar
KingRainbow44 committed
92
93
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
94
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
KingRainbow44's avatar
KingRainbow44 committed
95
96
97
                    return;
                }
                break;
AnimeGitB's avatar
AnimeGitB committed
98
            default: // *No args*
99
                CommandHandler.sendMessage(sender, translate("commands.give.usage"));
AnimeGitB's avatar
AnimeGitB committed
100
                return;
KingRainbow44's avatar
KingRainbow44 committed
101
102
        }

103
        ItemData itemData = GameData.getItemDataMap().get(item);
KingRainbow44's avatar
KingRainbow44 committed
104
        if (itemData == null) {
105
            CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
KingRainbow44's avatar
KingRainbow44 committed
106
107
            return;
        }
108
109
110
        if (refinement != 0) {
            if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
                if (refinement < 1 || refinement > 5) {
111
                    CommandHandler.sendMessage(sender, translate("commands.give.refinement_must_between_1_and_5"));
112
113
114
                    return;
                }
            } else {
115
                CommandHandler.sendMessage(sender, translate("commands.give.refinement_only_applicable_weapons"));
116
117
118
                return;
            }
        }
KingRainbow44's avatar
KingRainbow44 committed
119

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

122
        if (!itemData.isEquip()) {
123
            CommandHandler.sendMessage(sender, translate("commands.give.given", Integer.toString(amount), Integer.toString(item), Integer.toString(targetPlayer.getUid())));
124
        } else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
125
            CommandHandler.sendMessage(sender, translate("commands.give.given_with_level_and_refinement", Integer.toString(item), Integer.toString(lvl), Integer.toString(refinement), Integer.toString(amount), Integer.toString(targetPlayer.getUid())));
126
        } else {
127
            CommandHandler.sendMessage(sender, translate("commands.give.given_level", Integer.toString(item), Integer.toString(lvl), Integer.toString(amount)));
128
        }
KingRainbow44's avatar
KingRainbow44 committed
129
130
    }

131
    private void item(Player player, ItemData itemData, int amount, int lvl, int refinement) {
KingRainbow44's avatar
KingRainbow44 committed
132
        if (itemData.isEquip()) {
133
            List<GameItem> items = new LinkedList<>();
memetrollsXD's avatar
memetrollsXD committed
134
            for (int i = 0; i < amount; i++) {
135
                GameItem item = new GameItem(itemData);
筱傑's avatar
筱傑 committed
136
137
138
139
140
141
142
143
                if (item.isEquipped()) {
                    // check item max level
                    if (item.getItemType() == ItemType.ITEM_WEAPON) {
                        if (lvl > 90) lvl = 90;
                    } else {
                        if (lvl > 21) lvl = 21;
                    }
                }
memetrollsXD's avatar
memetrollsXD committed
144
145
                item.setCount(amount);
                item.setLevel(lvl);
筱傑's avatar
筱傑 committed
146
                if (lvl > 80) {
memetrollsXD's avatar
memetrollsXD committed
147
                    item.setPromoteLevel(6);
筱傑's avatar
筱傑 committed
148
149
150
151
152
153
154
155
156
157
                } else if (lvl > 70) {
                    item.setPromoteLevel(5);
                } else if (lvl > 60) {
                    item.setPromoteLevel(4);
                } else if (lvl > 50) {
                    item.setPromoteLevel(3);
                } else if (lvl > 40) {
                    item.setPromoteLevel(2);
                } else if (lvl > 20) {
                    item.setPromoteLevel(1);
memetrollsXD's avatar
memetrollsXD committed
158
                }
159
160
161
162
163
164
165
                if (item.getItemType() == ItemType.ITEM_WEAPON) {
                    if (refinement > 0) {
                        item.setRefinement(refinement - 1);
                    } else {
                        item.setRefinement(0);
                    }
                }
memetrollsXD's avatar
memetrollsXD committed
166
167
                items.add(item);
            }
168
            player.getInventory().addItems(items, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
169
        } else {
170
171
172
            GameItem item = new GameItem(itemData);
            item.setCount(amount);
            player.getInventory().addItem(item, ActionReason.SubfieldDrop);
KingRainbow44's avatar
KingRainbow44 committed
173
174
175
        }
    }
}