-
Notifications
You must be signed in to change notification settings - Fork 48
/
GroupCommandModule.java
412 lines (352 loc) · 18 KB
/
GroupCommandModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package dev.esophose.playerparticles.command;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.api.PlayerParticlesAPI;
import dev.esophose.playerparticles.manager.LocaleManager;
import dev.esophose.playerparticles.manager.ParticleGroupPresetManager;
import dev.esophose.playerparticles.manager.PermissionManager;
import dev.esophose.playerparticles.particles.PPlayer;
import dev.esophose.playerparticles.particles.ParticleGroup;
import dev.esophose.playerparticles.particles.ParticlePair;
import dev.esophose.playerparticles.particles.preset.ParticleGroupPreset;
import dev.rosewood.rosegarden.utils.StringPlaceholders;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.util.StringUtil;
public class GroupCommandModule implements CommandModule {
private static final List<String> VALID_COMMANDS = Arrays.asList("save", "load", "unload", "remove", "info", "list");
@Override
public void onCommandExecute(PPlayer pplayer, String[] args) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
if (args.length == 0 || !VALID_COMMANDS.contains(args[0])) {
localeManager.sendMessage(pplayer, "command-description-group-save");
localeManager.sendMessage(pplayer, "command-description-group-load");
localeManager.sendMessage(pplayer, "command-description-group-unload");
localeManager.sendMessage(pplayer, "command-description-group-remove");
localeManager.sendMessage(pplayer, "command-description-group-info");
localeManager.sendMessage(pplayer, "command-description-group-list");
return;
}
if (args.length == 1 && !args[0].equalsIgnoreCase("list")) {
localeManager.sendMessage(pplayer, "group-no-name", StringPlaceholders.of("cmd", args[0].toLowerCase()));
return;
}
switch (args[0].toLowerCase()) {
case "save":
this.onSave(pplayer, args[1].toLowerCase());
break;
case "load":
this.onLoad(pplayer, args[1].toLowerCase());
break;
case "unload":
this.onUnload(pplayer, args[1].toLowerCase());
break;
case "remove":
this.onRemove(pplayer, args[1].toLowerCase());
break;
case "info":
this.onInfo(pplayer, args[1].toLowerCase());
break;
case "list":
this.onList(pplayer);
break;
default:
localeManager.sendMessage(pplayer, "command-description-group-save");
localeManager.sendMessage(pplayer, "command-description-group-load");
localeManager.sendMessage(pplayer, "command-description-group-unload");
localeManager.sendMessage(pplayer, "command-description-group-remove");
localeManager.sendMessage(pplayer, "command-description-group-info");
localeManager.sendMessage(pplayer, "command-description-group-list");
break;
}
}
/**
* Handles the command /pp group save
*
* @param pplayer The PPlayer
* @param groupName The target group name
*/
private void onSave(PPlayer pplayer, String groupName) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
// Check that the groupName isn't the reserved name
if (groupName.equalsIgnoreCase(ParticleGroup.DEFAULT_NAME)) {
localeManager.sendMessage(pplayer, "group-reserved");
return;
}
// Check if the player actually has any particles
if (pplayer.getActiveParticles().size() == 0) {
localeManager.sendMessage(pplayer, "group-save-no-particles");
return;
}
// The database column can only hold up to 100 characters, cut it off there
if (groupName.length() >= 100) {
groupName = groupName.substring(0, 100);
}
// Check if they are creating a new group, if they are, check that they haven't gone over their limit
boolean groupUpdated = pplayer.getParticleGroupByName(groupName) != null;
if (!groupUpdated && PlayerParticles.getInstance().getManager(PermissionManager.class).hasPlayerReachedMaxGroups(pplayer)) {
localeManager.sendMessage(pplayer, "group-save-reached-max");
return;
}
// Use the existing group if available, otherwise create a new one
Map<Integer, ParticlePair> particles = new ConcurrentHashMap<>();
for (ParticlePair particle : pplayer.getActiveParticles())
particles.put(particle.getId(), particle.clone()); // Make sure the ParticlePairs aren't the same references in both the active and saved group
ParticleGroup group = new ParticleGroup(groupName, particles);
// Apply changes and notify player
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), group);
if (groupUpdated) {
localeManager.sendMessage(pplayer, "group-save-success-overwrite", StringPlaceholders.of("name", groupName));
} else {
localeManager.sendMessage(pplayer, "group-save-success", StringPlaceholders.of("name", groupName));
}
}
/**
* Handles the command /pp group load
*
* @param pplayer The PPlayer
* @param groupName The target group name
*/
private void onLoad(PPlayer pplayer, String groupName) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
// Check that the groupName isn't the reserved name
if (groupName.equalsIgnoreCase(ParticleGroup.DEFAULT_NAME)) {
localeManager.sendMessage(pplayer, "group-reserved");
return;
}
// Get the group
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
if (group != null) {
if (!group.canPlayerUse(pplayer)) {
localeManager.sendMessage(pplayer, "group-no-permission", StringPlaceholders.of("group", groupName));
return;
}
// Empty out the active group and fill it with clones from the target group
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
activeGroup.getParticles().clear();
for (ParticlePair particle : group.getParticles().values()) {
ParticlePair clonedParticle = particle.clone();
clonedParticle.setOwner(pplayer);
activeGroup.getParticles().put(clonedParticle.getId(), clonedParticle);
}
// Update group and notify player
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), activeGroup);
localeManager.sendMessage(pplayer, "group-load-success", StringPlaceholders.builder("amount", activeGroup.getParticles().size()).add("name", groupName).build());
return;
}
// Didn't find a saved group, look at the presets
ParticleGroupPreset presetGroup = PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroup(groupName);
if (presetGroup == null) {
localeManager.sendMessage(pplayer, "group-invalid", StringPlaceholders.of("name", groupName));
return;
}
if (!presetGroup.canPlayerUse(pplayer)) {
localeManager.sendMessage(pplayer, "group-preset-no-permission", StringPlaceholders.of("group", groupName));
return;
}
pplayer.loadPresetGroup(presetGroup.getGroup().getParticles().values());
localeManager.sendMessage(pplayer, "group-load-preset-success", StringPlaceholders.builder("amount", presetGroup.getGroup().getParticles().size()).add("name", groupName).build());
}
/**
* Handles the command /pp group unload
*
* @param pplayer The PPlayer
* @param groupName The target group name
*/
private void onUnload(PPlayer pplayer, String groupName) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
// Check that the groupName isn't the reserved name
if (groupName.equalsIgnoreCase(ParticleGroup.DEFAULT_NAME)) {
localeManager.sendMessage(pplayer, "group-reserved");
return;
}
// Get the group
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
if (group != null) {
if (!group.canPlayerUse(pplayer)) {
localeManager.sendMessage(pplayer, "group-no-permission", StringPlaceholders.of("group", groupName));
return;
}
// Remove all particles that match the group
AtomicInteger matches = new AtomicInteger(0);
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles().values()) {
activeGroup.getParticles().values().removeIf(x -> {
boolean match = x.getEffect() == particle.getEffect() && x.getStyle() == particle.getStyle();
if (match) matches.incrementAndGet();
return match;
});
}
// Update group and notify player
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), activeGroup);
localeManager.sendMessage(pplayer, "group-unload-success", StringPlaceholders.builder("amount", matches.get()).add("name", groupName).build());
return;
}
// Didn't find a saved group, look at the presets
ParticleGroupPreset presetGroup = PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroup(groupName);
if (presetGroup == null) {
localeManager.sendMessage(pplayer, "group-invalid", StringPlaceholders.of("name", groupName));
return;
}
if (!presetGroup.canPlayerUse(pplayer)) {
localeManager.sendMessage(pplayer, "group-preset-no-permission", StringPlaceholders.of("group", groupName));
return;
}
// Remove all particles that match the group
AtomicInteger matches = new AtomicInteger(0);
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
for (ParticlePair particle : presetGroup.getGroup().getParticles().values()) {
activeGroup.getParticles().values().removeIf(x -> {
boolean match = x.getEffect() == particle.getEffect() && x.getStyle() == particle.getStyle();
if (match) matches.incrementAndGet();
return match;
});
}
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), activeGroup);
localeManager.sendMessage(pplayer, "group-unload-success", StringPlaceholders.builder("amount", matches.get()).add("name", groupName).build());
}
/**
* Handles the command /pp group remove
*
* @param pplayer The PPlayer
* @param groupName The target group name
*/
private void onRemove(PPlayer pplayer, String groupName) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
// Check that the groupName isn't the reserved name
if (groupName.equalsIgnoreCase(ParticleGroup.DEFAULT_NAME)) {
localeManager.sendMessage(pplayer, "group-reserved");
return;
}
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
if (group == null) {
// Didn't find a saved group, look at the presets
ParticleGroupPreset presetGroup = PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroup(groupName);
if (presetGroup == null) {
localeManager.sendMessage(pplayer, "group-invalid", StringPlaceholders.of("name", groupName));
} else {
localeManager.sendMessage(pplayer, "group-remove-preset");
}
return;
}
// Delete the group and notify player
PlayerParticlesAPI.getInstance().removePlayerParticleGroup(pplayer.getPlayer(), group.getName());
localeManager.sendMessage(pplayer, "group-remove-success", StringPlaceholders.of("name", groupName));
}
/**
* Handles the command /pp group info
*
* @param pplayer The PPlayer
* @param groupName The target group name
*/
private void onInfo(PPlayer pplayer, String groupName) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
// Check that the groupName isn't the reserved name
if (groupName.equalsIgnoreCase(ParticleGroup.DEFAULT_NAME)) {
localeManager.sendMessage(pplayer, "group-reserved");
return;
}
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
if (group == null) {
// Didn't find a saved group, look at the presets
ParticleGroupPreset presetGroup = PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroup(groupName);
if (presetGroup == null) {
localeManager.sendMessage(pplayer, "group-invalid", StringPlaceholders.of("name", groupName));
return;
}
if (!presetGroup.canPlayerUse(pplayer)) {
localeManager.sendMessage(pplayer, "group-preset-no-permission", StringPlaceholders.of("group", groupName));
return;
}
group = presetGroup.getGroup();
}
List<ParticlePair> particles = new ArrayList<>(group.getParticles().values());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
localeManager.sendMessage(pplayer, "group-info-header", StringPlaceholders.of("group", groupName));
for (ParticlePair particle : particles) {
StringPlaceholders stringPlaceholders = StringPlaceholders.builder("id", particle.getId())
.add("effect", particle.getEffect().getName())
.add("style", particle.getStyle().getName())
.add("data", particle.getDataString())
.build();
localeManager.sendMessage(pplayer, "list-output", stringPlaceholders);
}
}
/**
* Handles the command /pp group list
*
* @param pplayer The PPlayer
*/
private void onList(PPlayer pplayer) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
List<ParticleGroup> groups = new ArrayList<>(pplayer.getParticleGroups().values());
groups.sort(Comparator.comparing(ParticleGroup::getName));
StringBuilder groupsList = new StringBuilder();
for (ParticleGroup group : groups)
if (!group.getName().equals(ParticleGroup.DEFAULT_NAME))
groupsList.append(group.getName()).append(", ");
if (groupsList.toString().endsWith(", "))
groupsList = new StringBuilder(groupsList.substring(0, groupsList.length() - 2));
StringBuilder presetsList = new StringBuilder();
for (ParticleGroupPreset group : PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroupsForPlayer(pplayer))
presetsList.append(group.getGroup().getName()).append(", ");
if (presetsList.toString().endsWith(", "))
presetsList = new StringBuilder(presetsList.substring(0, presetsList.length() - 2));
if ((groupsList.length() == 0) && (presetsList.length() == 0)) {
localeManager.sendMessage(pplayer, "group-list-none");
return;
}
if (groupsList.length() > 0) {
localeManager.sendMessage(pplayer, "group-list-output", StringPlaceholders.of("info", groupsList.toString()));
}
if (presetsList.length() > 0) {
localeManager.sendMessage(pplayer, "group-list-presets", StringPlaceholders.of("info", presetsList.toString()));
}
}
@Override
public List<String> onTabComplete(PPlayer pplayer, String[] args) {
List<String> matches = new ArrayList<>();
if (args.length <= 1) {
if (args.length == 0) matches = VALID_COMMANDS;
else StringUtil.copyPartialMatches(args[0], VALID_COMMANDS, matches);
} else if (args.length == 2 && !args[0].equalsIgnoreCase("list")) {
if (args[0].equalsIgnoreCase("save")) {
matches.add("<groupName>");
} else {
List<String> groupNames = new ArrayList<>();
for (ParticleGroup group : pplayer.getParticleGroups().values())
if (!group.getName().equals(ParticleGroup.DEFAULT_NAME))
groupNames.add(group.getName());
if (!args[0].equals("remove"))
for (ParticleGroupPreset group : PlayerParticles.getInstance().getManager(ParticleGroupPresetManager.class).getPresetGroupsForPlayer(pplayer))
groupNames.add(group.getGroup().getName());
StringUtil.copyPartialMatches(args[1], groupNames, matches);
}
}
return matches;
}
@Override
public String getName() {
return "group";
}
@Override
public String getDescriptionKey() {
return "command-description-group";
}
@Override
public String getArguments() {
return "<sub-command>";
}
@Override
public boolean requiresEffectsAndStyles() {
return false;
}
@Override
public boolean canConsoleExecute() {
return false;
}
}