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

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
5
import emu.grasscutter.data.GameData;
KingRainbow44's avatar
KingRainbow44 committed
6
import emu.grasscutter.data.def.ItemData;
7
import emu.grasscutter.game.inventory.GameItem;
8
import emu.grasscutter.game.inventory.ItemType;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
import emu.grasscutter.game.props.ActionReason;

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

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

19
@Command(label = "give", usage = "give <itemId|itemName> [amount] [level]", aliases = {
方块君's avatar
方块君 committed
20
        "g", "item", "giveitem"}, permission = "player.give", description = "commands.give.description")
KingRainbow44's avatar
KingRainbow44 committed
21
public final class GiveCommand implements CommandHandler {
22
23
    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+)");
24
    Pattern amountRegex = Pattern.compile("((?<=x)\\d+|\\d+(?=x)(?!x\\d))");
25
26
27
28
29
30
31
32

    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
33
34

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

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        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
66
        switch (args.size()) {
AnimeGitB's avatar
AnimeGitB committed
67
            case 4: // <itemId|itemName> [amount] [level] [refinement]
KingRainbow44's avatar
KingRainbow44 committed
68
                try {
AnimeGitB's avatar
AnimeGitB committed
69
                    refinement = Integer.parseInt(args.get(3));
KingRainbow44's avatar
KingRainbow44 committed
70
                } catch (NumberFormatException ignored) {
71
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemRefinement"));
KingRainbow44's avatar
KingRainbow44 committed
72
                    return;
AnimeGitB's avatar
AnimeGitB committed
73
74
                }  // Fallthrough
            case 3: // <itemId|itemName> [amount] [level]
memetrollsXD's avatar
memetrollsXD committed
75
                try {
AnimeGitB's avatar
AnimeGitB committed
76
                    lvl = Integer.parseInt(args.get(2));
memetrollsXD's avatar
memetrollsXD committed
77
                } catch (NumberFormatException ignored) {
78
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemLevel"));
memetrollsXD's avatar
memetrollsXD committed
79
                    return;
AnimeGitB's avatar
AnimeGitB committed
80
81
                }  // Fallthrough
            case 2: // <itemId|itemName> [amount]
82
                try {
AnimeGitB's avatar
AnimeGitB committed
83
                    amount = Integer.parseInt(args.get(1));
84
                } catch (NumberFormatException ignored) {
85
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.amount"));
86
                    return;
AnimeGitB's avatar
AnimeGitB committed
87
88
                }  // Fallthrough
            case 1: // <itemId|itemName>
KingRainbow44's avatar
KingRainbow44 committed
89
                try {
AnimeGitB's avatar
AnimeGitB committed
90
                    item = Integer.parseInt(args.get(0));
KingRainbow44's avatar
KingRainbow44 committed
91
92
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from item name using GM Handbook.
93
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
KingRainbow44's avatar
KingRainbow44 committed
94
95
96
                    return;
                }
                break;
AnimeGitB's avatar
AnimeGitB committed
97
            default: // *No args*
98
                CommandHandler.sendMessage(sender, translate("commands.give.usage"));
AnimeGitB's avatar
AnimeGitB committed
99
                return;
KingRainbow44's avatar
KingRainbow44 committed
100
101
        }

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

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

121
        if (!itemData.isEquip()) {
122
            CommandHandler.sendMessage(sender, translate("commands.give.given", Integer.toString(amount), Integer.toString(item), Integer.toString(targetPlayer.getUid())));
123
        } else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
124
            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())));
125
        } else {
126
            CommandHandler.sendMessage(sender, translate("commands.give.given_level", Integer.toString(item), Integer.toString(lvl), Integer.toString(amount)));
127
        }
KingRainbow44's avatar
KingRainbow44 committed
128
129
    }

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