GiveAllCommand.java 5.86 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
17
import static emu.grasscutter.utils.Language.translate;

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

BaiSugar's avatar
BaiSugar committed
22
    @Override
AnimeGitB's avatar
AnimeGitB committed
23
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
24
        if (targetPlayer == null) {
25
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
26
27
28
            return;
        }
        int amount = 99999;
BaiSugar's avatar
BaiSugar committed
29
30

        switch (args.size()) {
AnimeGitB's avatar
AnimeGitB committed
31
            case 0:
BaiSugar's avatar
BaiSugar committed
32
                break;
AnimeGitB's avatar
AnimeGitB committed
33
            case 1: // [amount]
BaiSugar's avatar
BaiSugar committed
34
                try {
AnimeGitB's avatar
AnimeGitB committed
35
                    amount = Integer.parseInt(args.get(0));
BaiSugar's avatar
BaiSugar committed
36
                } catch (NumberFormatException ignored) {
37
                    CommandHandler.sendMessage(sender, translate("commands.generic.invalid.amount"));
BaiSugar's avatar
BaiSugar committed
38
39
40
                    return;
                }
                break;
BaiSugar's avatar
BaiSugar committed
41
            default: // invalid
42
                CommandHandler.sendMessage(sender, translate("commands.giveAll.usage"));
BaiSugar's avatar
BaiSugar committed
43
                return;
BaiSugar's avatar
BaiSugar committed
44
45
        }

BaiSugar's avatar
BaiSugar committed
46
        this.giveAllItems(targetPlayer, amount);
47
        CommandHandler.sendMessage(sender, translate("commands.giveAll.success", targetPlayer.getNickname()));
BaiSugar's avatar
BaiSugar committed
48
49
    }

50
    public void giveAllItems(Player player, int amount) {
51
        CommandHandler.sendMessage(player, translate("commands.giveAll.started"));
BaiSugar's avatar
BaiSugar committed
52

53
        for (AvatarData avatarData: GameData.getAvatarDataMap().values()) {
BaiSugar's avatar
BaiSugar committed
54
55
56
            //Exclude test avatar
            if (isTestAvatar(avatarData.getId())) continue;

57
            Avatar avatar = new Avatar(avatarData);
BaiSugar's avatar
BaiSugar committed
58
59
            avatar.setLevel(90);
            avatar.setPromoteLevel(6);
60
61
            for(int i = 1;i <= 6;++i){
                avatar.getTalentIdList().add((avatar.getAvatarId()-10000000)*10+i);
BaiSugar's avatar
BaiSugar committed
62
63
64
65
66
            }
            // This will handle stats and talents
            avatar.recalcStats();
            player.addAvatar(avatar);
        }
BaiSugar's avatar
BaiSugar committed
67

BaiSugar's avatar
BaiSugar committed
68
        //some test items
69
70
        List<GameItem> itemList = new ArrayList<>();
        for (ItemData itemdata: GameData.getItemDataMap().values()) {
BaiSugar's avatar
BaiSugar committed
71
72
73
            //Exclude test item
            if (isTestItem(itemdata.getId())) continue;

BaiSugar's avatar
BaiSugar committed
74
            if (itemdata.isEquip()) {
lhhxxxxx's avatar
lhhxxxxx committed
75
76
77
                if (itemdata.getItemType() == ItemType.ITEM_WEAPON) {
                    for (int i = 0; i < 5; ++i) {
                        GameItem item = new GameItem(itemdata);
78
79
80
                        item.setLevel(90);
                        item.setPromoteLevel(6);
                        item.setRefinement(4);
lhhxxxxx's avatar
lhhxxxxx committed
81
                        itemList.add(item);
BaiSugar's avatar
BaiSugar committed
82
                    }
BaiSugar's avatar
BaiSugar committed
83
                }
BaiSugar's avatar
BaiSugar committed
84
85
            }
            else {
86
87
88
                GameItem item = new GameItem(itemdata);
                item.setCount(amount);
                itemList.add(item);
BaiSugar's avatar
BaiSugar committed
89
90
            }
        }
BaiSugar's avatar
BaiSugar committed
91
        int packetNum = 10;
92
        int itemLength = itemList.size();
BaiSugar's avatar
BaiSugar committed
93
94
95
96
97
        int number = itemLength / packetNum;
        int remainder = itemLength % packetNum;
        int offset = 0;
        for (int i = 0; i < packetNum; ++i) {
            if (remainder > 0) {
98
                player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset + 1));
BaiSugar's avatar
BaiSugar committed
99
100
101
102
                --remainder;
                ++offset;
            }
            else {
103
                player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset));
BaiSugar's avatar
BaiSugar committed
104
105
106
            }
        }
    }
BaiSugar's avatar
BaiSugar committed
107

BaiSugar's avatar
BaiSugar committed
108
109
110
111
112
113
114
115
    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
116
            }
BaiSugar's avatar
BaiSugar committed
117
118
        }

KingRainbow44's avatar
KingRainbow44 committed
119
        return testItemsList.contains(itemId);
BaiSugar's avatar
BaiSugar committed
120
121
122
    }

    static class Range {
KingRainbow44's avatar
KingRainbow44 committed
123
        private final int min, max;
BaiSugar's avatar
BaiSugar committed
124
125
126
127
128
129
130

        public Range(int min, int max) {
            if(min > max){
                min ^= max;
                max ^= min;
                min ^= max;
            }
KingRainbow44's avatar
KingRainbow44 committed
131
            
BaiSugar's avatar
BaiSugar committed
132
133
134
135
136
137
            this.min = min;
            this.max = max;
        }

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

BaiSugar's avatar
BaiSugar committed
141
142
143
    private static final Range[] testItemRanges = new Range[] {
            new Range(106, 139),
            new Range(1000, 1099),
BaiSugar's avatar
BaiSugar committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            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
158
159
    };
    private static final Integer[] testItemsIds = new Integer[] {
BaiSugar's avatar
BaiSugar committed
160
161
162
163
164
            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
165
166
167
168
    };

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

BaiSugar's avatar
BaiSugar committed
169
}
BaiSugar's avatar
BaiSugar committed
170