GiveAllCommand.java 5.87 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;

方块君's avatar
方块君 committed
18
@Command(label = "giveall", usage = "giveall [amount]", aliases = {"givea"}, permission = "player.giveall", threading = true, description = "commands.giveAll.description")
KingRainbow44's avatar
KingRainbow44 committed
19
public final class GiveAllCommand implements CommandHandler {
BaiSugar's avatar
BaiSugar committed
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

BaiSugar's avatar
BaiSugar committed
168
}
BaiSugar's avatar
BaiSugar committed
169