GiveAllCommand.java 6.91 KB
Newer Older
BaiSugar's avatar
BaiSugar 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;
BaiSugar's avatar
BaiSugar committed
7
8
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.ItemData;
9
10
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.inventory.GameItem;
BaiSugar's avatar
BaiSugar committed
11
import emu.grasscutter.game.inventory.ItemType;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.player.Player;
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
    @Override
21
    public void execute(Player 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

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

66
        Player 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
    }

76
    public void giveAllItems(Player player, int amount) {
BaiSugar's avatar
BaiSugar committed
77
78
        CommandHandler.sendMessage(player, "Giving all items...");

79
        for (AvatarData avatarData: GameData.getAvatarDataMap().values()) {
BaiSugar's avatar
BaiSugar committed
80
81
82
            //Exclude test avatar
            if (isTestAvatar(avatarData.getId())) continue;

83
            Avatar avatar = new Avatar(avatarData);
BaiSugar's avatar
BaiSugar committed
84
85
            avatar.setLevel(90);
            avatar.setPromoteLevel(6);
86
87
88
            for(int i = 1;i <= 6;++i){
                avatar.getTalentIdList().add((avatar.getAvatarId()-10000000)*10+i);
            }
BaiSugar's avatar
BaiSugar committed
89
90
91
92
            // This will handle stats and talents
            avatar.recalcStats();
            player.addAvatar(avatar);
        }
BaiSugar's avatar
BaiSugar committed
93

BaiSugar's avatar
BaiSugar committed
94
        //some test items
95
96
        List<GameItem> itemList = new ArrayList<>();
        for (ItemData itemdata: GameData.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()) {
lhhxxxxx's avatar
lhhxxxxx committed
101
102
103
                if (itemdata.getItemType() == ItemType.ITEM_WEAPON) {
                    for (int i = 0; i < 5; ++i) {
                        GameItem item = new GameItem(itemdata);
104
105
106
                        item.setLevel(90);
                        item.setPromoteLevel(6);
                        item.setRefinement(4);
lhhxxxxx's avatar
lhhxxxxx committed
107
                        itemList.add(item);
BaiSugar's avatar
BaiSugar committed
108
                    }
BaiSugar's avatar
BaiSugar committed
109
                }
BaiSugar's avatar
BaiSugar committed
110
111
            }
            else {
112
113
114
                GameItem item = new GameItem(itemdata);
                item.setCount(amount);
                itemList.add(item);
BaiSugar's avatar
BaiSugar committed
115
116
            }
        }
BaiSugar's avatar
BaiSugar committed
117
        int packetNum = 10;
118
        int itemLength = itemList.size();
BaiSugar's avatar
BaiSugar committed
119
120
121
122
123
        int number = itemLength / packetNum;
        int remainder = itemLength % packetNum;
        int offset = 0;
        for (int i = 0; i < packetNum; ++i) {
            if (remainder > 0) {
124
                player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset + 1));
BaiSugar's avatar
BaiSugar committed
125
126
127
128
                --remainder;
                ++offset;
            }
            else {
129
                player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset));
BaiSugar's avatar
BaiSugar committed
130
131
132
            }
        }
    }
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
       if (testItemsList.contains(itemId)) {
BaiSugar's avatar
BaiSugar committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
            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
    private static final Range[] testItemRanges = new Range[] {
            new Range(106, 139),
            new Range(1000, 1099),
BaiSugar's avatar
BaiSugar committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
            new Range(2001, 3022),
            new Range(23300, 23340),
            new Range(23383, 23385),
            new Range(78310, 78554),
            new Range(99310, 99554),
            new Range(100001, 100187),
            new Range(100210, 100214),
            new Range(100303, 100398),
            new Range(100414, 100425),
            new Range(100454, 103008),
            new Range(109000, 109492),
            new Range(115001, 118004),
            new Range(141001, 141072),
            new Range(220050, 221016),
BaiSugar's avatar
BaiSugar committed
188
189
    };
    private static final Integer[] testItemsIds = new Integer[] {
BaiSugar's avatar
BaiSugar committed
190
191
192
193
194
            210, 211, 314, 315, 317, 1005, 1007, 1105, 1107, 1201, 1202,10366,
            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,100231,100232,100431,
            101689,105001,105004, 106000,106001,108000,110000
BaiSugar's avatar
BaiSugar committed
195
196
197
198
    };

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

BaiSugar's avatar
BaiSugar committed
199
}
BaiSugar's avatar
BaiSugar committed
200