-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathClickActionTask.java
More file actions
478 lines (402 loc) · 21.2 KB
/
Copy pathClickActionTask.java
File metadata and controls
478 lines (402 loc) · 21.2 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package com.extendedclip.deluxemenus.action;
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.menu.MenuHolder;
import com.extendedclip.deluxemenus.persistentmeta.PersistentMetaHandler;
import com.extendedclip.deluxemenus.scheduler.UniversalRunnable;
import com.extendedclip.deluxemenus.utils.AdventureUtils;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.ExpUtils;
import com.extendedclip.deluxemenus.utils.SoundUtils;
import com.extendedclip.deluxemenus.utils.StringUtils;
import com.extendedclip.deluxemenus.utils.VersionHelper;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
public class ClickActionTask extends UniversalRunnable {
private final DeluxeMenus plugin;
private final UUID uuid;
private final ActionType actionType;
private final String exec;
// Ugly hack to get around the fact that arguments are not available at task execution time
private final Map<String, String> arguments;
private final boolean parsePlaceholdersInArguments;
private final boolean parsePlaceholdersAfterArguments;
public ClickActionTask(
@NotNull final DeluxeMenus plugin,
@NotNull final UUID uuid,
@NotNull final ActionType actionType,
@NotNull final String exec,
@NotNull final Map<String, String> arguments,
final boolean parsePlaceholdersInArguments,
final boolean parsePlaceholdersAfterArguments
) {
this.plugin = plugin;
this.uuid = uuid;
this.actionType = actionType;
this.exec = exec;
this.arguments = arguments;
this.parsePlaceholdersInArguments = parsePlaceholdersInArguments;
this.parsePlaceholdersAfterArguments = parsePlaceholdersAfterArguments;
}
@Override
public void run() {
final Player player = Bukkit.getPlayer(this.uuid);
if (player == null) {
return;
}
plugin.getScheduler().runTask(player, () -> {
final Optional<MenuHolder> holder = Menu.getMenuHolder(player);
final Player target = holder.isPresent() && holder.get().getPlaceholderPlayer() != null
? holder.get().getPlaceholderPlayer()
: player;
final String executable = StringUtils.replacePlaceholdersAndArguments(
this.exec,
this.arguments,
target,
this.parsePlaceholdersInArguments,
this.parsePlaceholdersAfterArguments);
switch (actionType) {
case META:
if (!VersionHelper.IS_PDC_VERSION || plugin.getPersistentMetaHandler() == null) {
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Meta action not supported on this server version.");
break;
}
final PersistentMetaHandler.OperationResult result = plugin.getPersistentMetaHandler().parseAndExecuteMetaActionFromString(player, executable);
switch (result) {
case INVALID_SYNTAX:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Make sure you have the right syntax.");
break;
case NEW_VALUE_IS_DIFFERENT_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! New value is a different type than the old value!");
break;
case INVALID_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! The specified type is not supported for the specified action!");
break;
case EXISTENT_VALUE_IS_DIFFERENT_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Existent value is a different type than the new value!");
break;
case VALUE_NOT_FOUND:
case SUCCESS:
default:
break;
}
break;
case PLAYER:
case PLAYER_COMMAND_EVENT:
player.chat("/" + executable);
break;
case PLACEHOLDER:
holder.ifPresent(it -> it.setPlaceholders(executable));
break;
case CHAT:
player.chat(executable);
break;
case CONSOLE:
plugin.getScheduler().runTask(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), executable));
break;
case MINI_MESSAGE:
plugin.audiences().player(player).sendMessage(MiniMessage.miniMessage().deserialize(executable));
break;
case MINI_BROADCAST:
plugin.audiences().all().sendMessage(MiniMessage.miniMessage().deserialize(executable));
break;
case MESSAGE:
player.sendMessage(StringUtils.color(executable));
break;
case LOG:
final String[] logParts = executable.split(" ", 2);
if (logParts.length == 0 || logParts[0].isBlank()) {
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "LOG command requires at least a message");
break;
}
Level logLevel;
String message;
if (logParts.length == 1) {
logLevel = Level.INFO;
message = logParts[0];
} else {
message = logParts[1];
try {
logLevel = Level.parse(logParts[0].toUpperCase());
} catch (IllegalArgumentException e) {
logLevel = Level.INFO;
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "Log level " + logParts[0] + " is not a valid log level! Using INFO instead.");
}
}
plugin.getLogger().log(logLevel, String.format("[%s]: %s", holder.map(MenuHolder::getMenuName).orElse("Unknown Menu"), message));
break;
case BROADCAST:
Bukkit.broadcastMessage(StringUtils.color(executable));
break;
case CLOSE:
Menu.closeMenu(plugin, player, true, true);
break;
case OPEN_GUI_MENU:
case OPEN_MENU:
final String temporaryExecutable = executable.replaceAll("\\s+", " ").replace(" ", " ");
final String[] executableParts = temporaryExecutable.split(" ", 2);
if (executableParts.length == 0) {
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "Could not find and open menu " + executable);
break;
}
final String menuName = executableParts[0];
final Optional<Menu> optionalMenuToOpen = Menu.getMenuByName(menuName);
if (optionalMenuToOpen.isEmpty()) {
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "Could not find and open menu " + executable);
break;
}
final Menu menuToOpen = optionalMenuToOpen.get();
final List<String> menuArgumentNames = menuToOpen.options().arguments();
String[] passedArgumentValues = null;
if (executableParts.length > 1) {
passedArgumentValues = executableParts[1].split(" ");
}
if (menuArgumentNames.isEmpty()) {
if (passedArgumentValues != null && passedArgumentValues.length > 0) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Arguments were given for menu " + menuName + " in action [openguimenu] or [openmenu], but the menu does not support arguments!"
);
}
if (holder.isEmpty()) {
menuToOpen.openMenu(player);
break;
}
menuToOpen.openMenu(player, holder.get().getTypedArgs(), holder.get().getPlaceholderPlayer());
break;
}
if (passedArgumentValues == null || passedArgumentValues.length == 0) {
// Replicate old behavior: If no arguments are given, open the menu with the arguments from the current menu
if (holder.isEmpty()) {
menuToOpen.openMenu(player);
break;
}
menuToOpen.openMenu(player, holder.get().getTypedArgs(), holder.get().getPlaceholderPlayer());
break;
}
if (passedArgumentValues.length < menuArgumentNames.size()) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Not enough arguments given for menu " + menuName + " when opening using the [openguimenu] or [openmenu] action!"
);
break;
}
final Map<String, String> argumentsMap = new HashMap<>();
if (holder.isPresent() && holder.get().getTypedArgs() != null) {
// Pass the arguments from the current menu to the new menu. If the new menu has arguments with the
// same name, they will be overwritten
argumentsMap.putAll(holder.get().getTypedArgs());
}
for (int index = 0; index < menuArgumentNames.size(); index++) {
final String argumentName = menuArgumentNames.get(index);
if (passedArgumentValues.length <= index) {
// This should never be the case!
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Not enough arguments given for menu " + menuName + " when opening using the [openguimenu] or [openmenu] action!"
);
break;
}
if (menuArgumentNames.size() == index + 1) {
// If this is the last argument, get all remaining values and join them
final String lastArgumentValue = String.join(" ", Arrays.asList(passedArgumentValues).subList(index, passedArgumentValues.length));
argumentsMap.put(argumentName, lastArgumentValue);
break;
}
argumentsMap.put(argumentName, passedArgumentValues[index]);
}
if (holder.isEmpty()) {
menuToOpen.openMenu(player, argumentsMap, null);
break;
}
menuToOpen.openMenu(player, argumentsMap, holder.get().getPlaceholderPlayer());
break;
case CONNECT:
plugin.connect(player, executable);
break;
case JSON_MESSAGE:
AdventureUtils.sendJson(plugin, player, executable);
break;
case JSON_BROADCAST:
case BROADCAST_JSON:
plugin.audiences().all().sendMessage(AdventureUtils.fromJson(executable));
break;
case REFRESH:
if (holder.isEmpty()) {
plugin.debug(
DebugLevel.MEDIUM,
Level.WARNING,
player.getName() + " does not have menu open! Nothing to refresh!"
);
break;
}
holder.get().refreshMenu();
break;
case TAKE_MONEY:
if (plugin.getVault() == null || !plugin.getVault().hooked()) {
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "Vault not hooked! Cannot take money!");
break;
}
try {
plugin.getVault().takeMoney(player, Double.parseDouble(executable));
} catch (final NumberFormatException exception) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Amount for take money action: " + executable + ", is not a valid number!"
);
}
break;
case GIVE_MONEY:
if (plugin.getVault() == null || !plugin.getVault().hooked()) {
plugin.debug(DebugLevel.HIGHEST, Level.WARNING, "Vault not hooked! Cannot give money!");
break;
}
try {
plugin.getVault().giveMoney(player, Double.parseDouble(executable));
} catch (final NumberFormatException exception) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Amount for give money action: " + executable + ", is not a valid number!"
);
}
break;
case TAKE_EXP:
case GIVE_EXP:
final String lowerCaseExecutable = executable.toLowerCase();
try {
if (Integer.parseInt(lowerCaseExecutable.replaceAll("l", "")) <= 0) break;
if (actionType == ActionType.TAKE_EXP) {
ExpUtils.setExp(player, "-" + lowerCaseExecutable);
break;
}
ExpUtils.setExp(player, lowerCaseExecutable);
break;
} catch (final NumberFormatException exception) {
if (actionType == ActionType.TAKE_EXP) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Amount for take exp action: " + executable + ", is not a valid number!"
);
break;
}
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Amount for give exp action: " + executable + ", is not a valid number!"
);
break;
}
case GIVE_PERM:
if (plugin.getVault() == null || !plugin.getVault().hooked()) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Vault not hooked! Cannot give permission: " + executable + "!");
break;
}
plugin.getVault().givePermission(player, executable);
break;
case TAKE_PERM:
if (plugin.getVault() == null || !plugin.getVault().hooked()) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Vault not hooked! Cannot take permission: " + executable + "!");
break;
}
plugin.getVault().takePermission(player, executable);
break;
case BROADCAST_SOUND:
case BROADCAST_WORLD_SOUND:
case PLAY_SOUND:
final Sound sound;
float volume = 1;
float pitch = 1;
if (!executable.contains(" ")) {
try {
sound = SoundUtils.getSound(executable.toUpperCase());
} catch (final IllegalArgumentException exception) {
plugin.printStacktrace(
"Sound name given for sound action: " + executable + ", is not a valid sound!",
exception
);
break;
}
} else {
String[] parts = executable.split(" ", 3);
try {
sound = SoundUtils.getSound(parts[0].toUpperCase());
} catch (final IllegalArgumentException exception) {
plugin.printStacktrace(
"Sound name given for sound action: " + parts[0] + ", is not a valid sound!",
exception
);
break;
}
if (parts.length == 3) {
try {
pitch = Float.parseFloat(parts[2]);
} catch (final NumberFormatException exception) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Pitch given for sound action: " + parts[2] + ", is not a valid number!"
);
plugin.printStacktrace(
"Pitch given for sound action: " + parts[2] + ", is not a valid number!",
exception
);
}
}
try {
volume = Float.parseFloat(parts[1]);
} catch (final NumberFormatException exception) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Volume given for sound action: " + parts[1] + ", is not a valid number!"
);
plugin.printStacktrace(
"Volume given for sound action: " + parts[1] + ", is not a valid number!",
exception
);
}
}
switch (actionType) {
case BROADCAST_SOUND:
for (final Player broadcastTarget : Bukkit.getOnlinePlayers()) {
broadcastTarget.playSound(broadcastTarget.getLocation(), sound, volume, pitch);
}
break;
case BROADCAST_WORLD_SOUND:
for (final Player broadcastTarget : player.getWorld().getPlayers()) {
broadcastTarget.playSound(broadcastTarget.getLocation(), sound, volume, pitch);
}
break;
case PLAY_SOUND:
player.playSound(player.getLocation(), sound, volume, pitch);
break;
}
break;
default:
break;
}
});
}
}