GiveCommand.java 7.57 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

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

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

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

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



103
        ItemData itemData = GameData.getItemDataMap().get(item);
KingRainbow44's avatar
KingRainbow44 committed
104
        if (itemData == null) {
方块君's avatar
方块君 committed
105
            CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
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) {
方块君's avatar
方块君 committed
111
                    CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_refinement_must_between_1_and_5);
112
113
114
                    return;
                }
            } else {
方块君's avatar
方块君 committed
115
                CommandHandler.sendMessage(sender, Grasscutter.getLanguage().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()) {
AnimeGitB's avatar
AnimeGitB committed
123
            CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given.replace("{amount}", Integer.toString(amount)).replace("{item}", Integer.toString(item)).replace("{target}", Integer.toString(targetPlayer.getUid())));
124
        } else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
AnimeGitB's avatar
AnimeGitB committed
125
            CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given_with_level_and_refinement.replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)).replace("{refinement}", Integer.toString(refinement)).replace("{amount}", Integer.toString(amount)).replace("{target}", Integer.toString(targetPlayer.getUid())));
126
        } else {
127
            CommandHandler.sendMessage(sender,Grasscutter.getLanguage().Give_given_level.replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)).replace("{amount}", 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
        }
    }
}