TeamCommand.java 9.21 KB
Newer Older
1
2
3
4
5
6
7
8
package emu.grasscutter.command.commands;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.packet.send.PacketChangeMpTeamAvatarRsp;

import java.util.List;
9
10
11

import static emu.grasscutter.config.Configuration.*;

12
13
14
import java.util.ArrayList;
import java.util.HashSet;

RDsatan's avatar
fixed    
RDsatan committed
15
16
@Command(label = "team", usage = "team <add|remove|set> [avatarId,...] [index|first|last|index-index,...]",
permission = "player.team", permissionTargeted = "player.team.others", description = "commands.team.description")
17
18
19
20
21
22
public final class TeamCommand implements CommandHandler {
    private static final int BASE_AVATARID = 10000000;

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        if (args.isEmpty()) {
Luke H-W's avatar
Luke H-W committed
23
            CommandHandler.sendTranslatedMessage(sender, "commands.team.usage");
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
            return;
        }

        switch (args.get(0)) {
            case "add":
                if (!addCommand(sender, targetPlayer, args)) return;
                break;

            case "remove":
                if (!removeCommand(sender, targetPlayer, args)) return;
                break;

            case "set":
                if (!setCommand(sender, targetPlayer, args)) return;
                break;

            default:
Luke H-W's avatar
Luke H-W committed
41
42
                CommandHandler.sendTranslatedMessage(sender, "commands.team.invalid_usage");
                CommandHandler.sendTranslatedMessage(sender, "commands.team.usage");
43
44
45
                return;
        }

RDsatan's avatar
fixed    
RDsatan committed
46
47
        targetPlayer.getTeamManager().updateTeamEntities(
                new PacketChangeMpTeamAvatarRsp(targetPlayer, targetPlayer.getTeamManager().getCurrentTeamInfo()));
48
49
50
51
    }

    private boolean addCommand(Player sender, Player targetPlayer, List<String> args) {
        if (args.size() < 2) {
Luke H-W's avatar
Luke H-W committed
52
53
            CommandHandler.sendTranslatedMessage(sender, "commands.team.invalid_usage");
            CommandHandler.sendTranslatedMessage(sender, "commands.team.add_usage");
54
55
56
57
58
59
60
61
62
            return false;
        }

        int index = -1;
        if (args.size() > 2) {
            try {
                index = Integer.parseInt(args.get(2)) - 1;
				if (index < 0) index = 0;
            } catch (Exception e) {
Luke H-W's avatar
Luke H-W committed
63
                CommandHandler.sendTranslatedMessage(sender, "commands.team.invalid_index");
64
65
66
67
68
69
70
71
                return false;
            }
        }

        var avatarIds = args.get(1).split(",");
        var currentTeamAvatars = targetPlayer.getTeamManager().getCurrentTeamInfo().getAvatars();

        if (currentTeamAvatars.size() + avatarIds.length > GAME_OPTIONS.avatarLimits.singlePlayerTeam) {
Luke H-W's avatar
Luke H-W committed
72
            CommandHandler.sendTranslatedMessage(sender, "commands.team.add_too_much", GAME_OPTIONS.avatarLimits.singlePlayerTeam);
73
74
75
76
            return false;
        }

        for (var avatarId: avatarIds) {
Luke H-W's avatar
Luke H-W committed
77
78
79
            int id = Integer.parseInt(avatarId);
            var success = addAvatar(sender, targetPlayer, id, index);
            if (index > 0) ++index;
80
81
82
83
84
85
        }
        return true;
    }

    private boolean removeCommand(Player sender, Player targetPlayer, List<String> args) {
        if (args.size() < 2) {
Luke H-W's avatar
Luke H-W committed
86
87
            CommandHandler.sendTranslatedMessage(sender, "commands.team.invalid_usage");
            CommandHandler.sendTranslatedMessage(sender, "commands.team.remove_usage");
88
89
90
91
92
93
94
95
96
97
98
99
100
            return false;
        }

        var currentTeamAvatars = targetPlayer.getTeamManager().getCurrentTeamInfo().getAvatars();
        var avatarCount = currentTeamAvatars.size();

        var metaIndexList = args.get(1).split(",");
        var indexes = new HashSet<Integer>();
        var ignoreList = new ArrayList<Integer>();
        for (var metaIndex: metaIndexList) {
            // step 1: parse metaIndex to indexes
            var subIndexes = transformToIndexes(metaIndex, avatarCount);
            if (subIndexes == null) {
Luke H-W's avatar
Luke H-W committed
101
                CommandHandler.sendTranslatedMessage(sender, "commands.team.failed_to_parse_index", metaIndex);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
                continue;
            }

            // step 2: get all of the avatar id through indexes
            for (var avatarIndex: subIndexes) {
                try {
                    indexes.add(currentTeamAvatars.get(avatarIndex - 1));
                } catch (Exception e) {
                    ignoreList.add(avatarIndex);
                    continue;
                }
            }
        }

        // step 3: check if user remove all of the avatar
        if (indexes.size() >= avatarCount) {
Luke H-W's avatar
Luke H-W committed
118
            CommandHandler.sendTranslatedMessage(sender, "commands.team.remove_too_much");
119
120
121
122
123
            return false;
        }

        // step 4: hint user for ignore index
        if (!ignoreList.isEmpty()) {
Luke H-W's avatar
Luke H-W committed
124
            CommandHandler.sendTranslatedMessage(sender, "commands.team.ignore_index", ignoreList);
125
126
127
128
129
130
131
132
133
        }

        // step 5: remove
        currentTeamAvatars.removeAll(indexes);
        return true;
    }

    private boolean setCommand(Player sender, Player targetPlayer, List<String> args) {
        if (args.size() < 3) {
Luke H-W's avatar
Luke H-W committed
134
135
            CommandHandler.sendTranslatedMessage(sender, "commands.team.invalid_usage");
            CommandHandler.sendTranslatedMessage(sender, "commands.team.set_usage");
136
137
138
139
140
141
142
143
144
145
            return false;
        }

        var currentTeamAvatars = targetPlayer.getTeamManager().getCurrentTeamInfo().getAvatars();

        int index;
        try {
            index = Integer.parseInt(args.get(1)) - 1;
            if (index < 0) index = 0;
        } catch(Exception e) {
Luke H-W's avatar
Luke H-W committed
146
            CommandHandler.sendTranslatedMessage(sender, "commands.team.failed_to_parse_index", args.get(1));
147
148
149
150
            return false;
        }

        if (index + 1 > currentTeamAvatars.size()) {
Luke H-W's avatar
Luke H-W committed
151
            CommandHandler.sendTranslatedMessage(sender, "commands.team.index_out_of_range");
152
153
154
155
156
157
158
            return false;
        }

        int avatarId;
        try {
            avatarId = Integer.parseInt(args.get(2));
        } catch(Exception e) {
Luke H-W's avatar
Luke H-W committed
159
            CommandHandler.sendTranslatedMessage(sender, "commands.team.failed_parse_avatar_id", args.get(2));
160
161
162
163
164
165
166
            return false;
        }
        if (avatarId < BASE_AVATARID) {
            avatarId += BASE_AVATARID;
        }

		if (currentTeamAvatars.contains(avatarId)) {
Luke H-W's avatar
Luke H-W committed
167
            CommandHandler.sendTranslatedMessage(sender, "commands.team.avatar_already_in_team", avatarId);
168
169
170
171
            return false;
		}

        if (!targetPlayer.getAvatars().hasAvatar(avatarId)) {
Luke H-W's avatar
Luke H-W committed
172
            CommandHandler.sendTranslatedMessage(sender, "commands.team.avatar_not_found", avatarId);
173
174
175
176
177
178
179
180
181
182
183
184
185
            return false;
        }

        currentTeamAvatars.set(index, avatarId);
        return true;
    }

    private boolean addAvatar(Player sender, Player targetPlayer, int avatarId, int index) {
        if (avatarId < BASE_AVATARID) {
            avatarId += BASE_AVATARID;
        }
        var currentTeamAvatars = targetPlayer.getTeamManager().getCurrentTeamInfo().getAvatars();
		if (currentTeamAvatars.contains(avatarId)) {
Luke H-W's avatar
Luke H-W committed
186
            CommandHandler.sendTranslatedMessage(sender, "commands.team.avatar_already_in_team", avatarId);
187
188
            return false;
		}
Luke H-W's avatar
Luke H-W committed
189
190
        if (!targetPlayer.getAvatars().hasAvatar(avatarId)) {
            CommandHandler.sendTranslatedMessage(sender, "commands.team.avatar_not_found", avatarId);
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
            return false;
        }
        if (index < 0) {
            currentTeamAvatars.add(avatarId);
        } else {
            currentTeamAvatars.add(index, avatarId);
        }
        return true;
    }

    private List<Integer> transformToIndexes(String metaIndexes, int listLength) {
        // step 1: check if metaIndexes is a special constants
        if (metaIndexes.equals("first")) {
            return List.of(1);
        } else if (metaIndexes.equals("last")) {
            return List.of(listLength);
        }

        // step 2: check if metaIndexes is a range
        if (metaIndexes.contains("-")) {
            var range = metaIndexes.split("-");
            if (range.length < 2) {
                return null;
            }

            int min, max;
            try {
                min = switch (range[0]) {
                    case "first" -> 1;
                    case "last" -> listLength;
                    default -> Integer.parseInt(range[0]);
                };

                max = switch (range[1]) {
                    case "first" -> 1;
                    case "last" -> listLength;
                    default -> Integer.parseInt(range[1]);
                };
            } catch (Exception e) {
                return null;
            }

            if (min > max) {
                min ^= max;
                max ^= min;
                min ^= max;
            }

            var indexes = new ArrayList<Integer>();
            for (int i = min; i <= max; ++i) {
                indexes.add(i);
            }
            return indexes;
        }

        // step 3: index is a value, simply return
        try {
            int index = Integer.parseInt(metaIndexes);
            return List.of(index);
        } catch (Exception e) {
            return null;
        }
    }

}