GiveCommand.java 7.37 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;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.data.excels.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;

tester233's avatar
tester233 committed
19
@Command(label = "give", usage = "give <itemId|itemName> [amount] [level]", aliases = {
20
        "g", "item", "giveitem"}, permission = "player.give", permissionTargeted = "player.give.others", 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
37
38
39
        int item;
        int lvl = 1;
        int amount = 1;
        int refinement = 0;
KingRainbow44's avatar
KingRainbow44 committed
40

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

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

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

117
        if (!itemData.isEquip()) {
Secretboy's avatar
Secretboy committed
118
            CommandHandler.sendMessage(sender, translate(sender, "commands.give.given", Integer.toString(amount), Integer.toString(item), Integer.toString(targetPlayer.getUid())));
119
        } else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
Secretboy's avatar
Secretboy committed
120
            CommandHandler.sendMessage(sender, translate(sender, "commands.give.given_with_level_and_refinement", Integer.toString(item), Integer.toString(lvl), Integer.toString(refinement), Integer.toString(amount), Integer.toString(targetPlayer.getUid())));
121
        } else {
Secretboy's avatar
Secretboy committed
122
            CommandHandler.sendMessage(sender, translate(sender, "commands.give.given_level", Integer.toString(item), Integer.toString(lvl), Integer.toString(amount), Integer.toString(targetPlayer.getUid())));
123
        }
KingRainbow44's avatar
KingRainbow44 committed
124
125
    }

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