GiveAllCommand.java 6.64 KB
Newer Older
BaiSugar's avatar
BaiSugar committed
1
2
3
4
5
6
7
8
9
10
11
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.inventory.GenshinItem;
BaiSugar's avatar
BaiSugar committed
12
import emu.grasscutter.game.inventory.ItemType;
BaiSugar's avatar
BaiSugar committed
13

BaiSugar's avatar
BaiSugar committed
14
import java.util.*;
BaiSugar's avatar
BaiSugar committed
15
16

@Command(label = "giveall", usage = "giveall [player] <amount>",
BaiSugar's avatar
BaiSugar committed
17
        description = "Gives all items", aliases = {"givea"}, permission = "player.giveall", threading = true)
BaiSugar's avatar
BaiSugar committed
18
public class GiveAllCommand implements CommandHandler {
BaiSugar's avatar
BaiSugar committed
19

BaiSugar's avatar
BaiSugar committed
20
21
    @Override
    public void execute(GenshinPlayer sender, List<String> args) {
BaiSugar's avatar
BaiSugar committed
22
        int target, amount = 99999;
BaiSugar's avatar
BaiSugar committed
23
24

        switch (args.size()) {
BaiSugar's avatar
BaiSugar committed
25
26
27
28
29
            case 0: // *no args*
                if (sender == null) {
                    CommandHandler.sendMessage(null, "This usage can only be run in-game");
                    return;
                }
BaiSugar's avatar
BaiSugar committed
30
                target = sender.getUid();
BaiSugar's avatar
BaiSugar committed
31
                break;
BaiSugar's avatar
BaiSugar committed
32

BaiSugar's avatar
fix    
BaiSugar committed
33
            case 1: // [player]
BaiSugar's avatar
BaiSugar committed
34
35
36
37
38
39
40
                try {
                    target = Integer.parseInt(args.get(0));
                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
                        CommandHandler.sendMessage(sender, "Invalid player ID.");
                        return;
                    }
                }catch (NumberFormatException ignored){
BaiSugar's avatar
fix    
BaiSugar committed
41
                    CommandHandler.sendMessage(sender, "Invalid player ID.");
BaiSugar's avatar
BaiSugar committed
42
43
44
                    return;
                }
                break;
BaiSugar's avatar
BaiSugar committed
45

BaiSugar's avatar
fix    
BaiSugar committed
46
            case 2: // [player] [amount]
BaiSugar's avatar
BaiSugar committed
47
48
                try {
                    target = Integer.parseInt(args.get(0));
BaiSugar's avatar
BaiSugar committed
49
                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
BaiSugar's avatar
BaiSugar committed
50
51
52
53
54
                        target = sender.getUid();
                        amount = Integer.parseInt(args.get(0));
                    } else {
                        amount = Integer.parseInt(args.get(1));
                    }
BaiSugar's avatar
BaiSugar committed
55
                } catch (NumberFormatException ignored) {
BaiSugar's avatar
BaiSugar committed
56
57
58
59
                    CommandHandler.sendMessage(sender, "Invalid amount or player ID.");
                    return;
                }
                break;
BaiSugar's avatar
BaiSugar committed
60
61
62
63

            default: // invalid
                CommandHandler.sendMessage(null, "Usage: giveall [player] <amount>");
                return;
BaiSugar's avatar
BaiSugar committed
64
65
        }

BaiSugar's avatar
BaiSugar committed
66
        GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
BaiSugar's avatar
BaiSugar committed
67
68
69
70
71
        if (targetPlayer == null) {
            CommandHandler.sendMessage(sender, "Player not found.");
            return;
        }

BaiSugar's avatar
BaiSugar committed
72
73
        this.giveAllItems(targetPlayer, amount);
        CommandHandler.sendMessage(sender, "Giving all items done");
BaiSugar's avatar
BaiSugar committed
74
75
    }

BaiSugar's avatar
BaiSugar committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    public void giveAllItems(GenshinPlayer player, int amount) {
        CommandHandler.sendMessage(player, "Giving all items...");

        for (AvatarData avatarData: GenshinData.getAvatarDataMap().values()) {
            //Exclude test avatar
            if (isTestAvatar(avatarData.getId())) continue;

            GenshinAvatar avatar = new GenshinAvatar(avatarData);
            avatar.setLevel(90);
            avatar.setPromoteLevel(6);
            for (int i = 1; i <= 6; ++i) {
                avatar.getTalentIdList().add((avatar.getAvatarId() - 10000000) * 10 + i);
            }
            // This will handle stats and talents
            avatar.recalcStats();
            player.addAvatar(avatar);
        }
BaiSugar's avatar
BaiSugar committed
93

BaiSugar's avatar
BaiSugar committed
94
95
        //some test items
        List<GenshinItem> genshinItemList = new ArrayList<>();
BaiSugar's avatar
BaiSugar committed
96
        for (ItemData itemdata: GenshinData.getItemDataMap().values()) {
BaiSugar's avatar
BaiSugar committed
97
98
99
            //Exclude test item
            if (isTestItem(itemdata.getId())) continue;

BaiSugar's avatar
BaiSugar committed
100
            if (itemdata.isEquip()) {
BaiSugar's avatar
BaiSugar committed
101
                for (int i = 0; i < 10; ++i) {
BaiSugar's avatar
BaiSugar committed
102
                    GenshinItem genshinItem = new GenshinItem(itemdata);
BaiSugar's avatar
BaiSugar committed
103
                    if (itemdata.getItemType() == ItemType.ITEM_WEAPON) {
BaiSugar's avatar
BaiSugar committed
104
105
                        genshinItem.setLevel(90);
                        genshinItem.setPromoteLevel(6);
BaiSugar's avatar
BaiSugar committed
106
                        genshinItem.setRefinement(4);
BaiSugar's avatar
BaiSugar committed
107
108
                    }
                    genshinItemList.add(genshinItem);
BaiSugar's avatar
BaiSugar committed
109
                }
BaiSugar's avatar
BaiSugar committed
110
111
            }
            else {
BaiSugar's avatar
BaiSugar committed
112
113
                GenshinItem genshinItem = new GenshinItem(itemdata);
                genshinItem.setCount(amount);
BaiSugar's avatar
BaiSugar committed
114
                genshinItemList.add(genshinItem);
BaiSugar's avatar
BaiSugar committed
115
116
            }
        }
BaiSugar's avatar
BaiSugar committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        int packetNum = 20;
        int itemLength = genshinItemList.size();
        int number = itemLength / packetNum;
        int remainder = itemLength % packetNum;
        int offset = 0;
        for (int i = 0; i < packetNum; ++i) {
            if (remainder > 0) {
                player.getInventory().addItems(genshinItemList.subList(i * number + offset, (i + 1) * number + offset + 1));
                --remainder;
                ++offset;
            }
            else {
                player.getInventory().addItems(genshinItemList.subList(i * number + offset, (i + 1) * number + offset));
            }
        }
    }
BaiSugar's avatar
BaiSugar committed
133

BaiSugar's avatar
BaiSugar committed
134
135
136
137
138
139
140
141
    public boolean isTestAvatar(int avatarId) {
        return avatarId < 10000002 || avatarId >= 11000000;
    }

    public boolean isTestItem(int itemId) {
        for (Range range: testItemRanges) {
            if (range.check(itemId)) {
                return true;
BaiSugar's avatar
BaiSugar committed
142
            }
BaiSugar's avatar
BaiSugar committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        }

        if (testItemsList.contains(itemId)) {
            return true;
        }

        return false;
    }

    static class Range {
        private int min;
        private int max;

        public Range(int min, int max) {
            if(min > max){
                min ^= max;
                max ^= min;
                min ^= max;
            }
            this.min = min;
            this.max = max;
        }

        public boolean check(int value) {
            return value >= this.min && value <= this.max;
BaiSugar's avatar
BaiSugar committed
168
169
170
        }
    }

BaiSugar's avatar
BaiSugar committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    private static final Range[] testItemRanges = new Range[] {
            new Range(106, 139),
            new Range(1000, 1099),
            new Range(2001, 2008),
            new Range(2017, 2029),
          //  new Range(108001, 108387) //food
    };

    private static final Integer[] testItemsIds = new Integer[] {
            210, 211, 314, 315, 317, 1005, 1007, 1105, 1107, 1201, 1202, 2800,
            100001, 100002, 100244, 100305, 100312, 100313, 101212, 11411, 11506, 11507, 11508, 12505,
            12506, 12508, 12509, 13503, 13506, 14411, 14503, 14505, 14508, 15411, 15504, 15505,
            15506, 15508, 20001, 10002, 10003, 10004, 10005, 10006, 10008 //9
    };

    private static final Collection<Integer> testItemsList = Arrays.asList(testItemsIds);

BaiSugar's avatar
BaiSugar committed
188
}
BaiSugar's avatar
BaiSugar committed
189