-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
DenizenCoreImplementation.java
598 lines (531 loc) · 22.9 KB
/
DenizenCoreImplementation.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package com.denizenscript.denizen.utilities.implementation;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.scripts.containers.core.*;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.debugging.DebugSubmit;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.maps.DenizenMapManager;
import com.denizenscript.denizencore.DenizenImplementation;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.debugging.StrongWarning;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.function.Consumer;
public class DenizenCoreImplementation implements DenizenImplementation {
@Override
public File getScriptFolder() {
File file;
// Get the script directory
if (Settings.useDefaultScriptPath()) {
file = new File(Denizen.getInstance().getDataFolder() + File.separator + "scripts");
}
else {
file = new File(Settings.getAlternateScriptPath().replace("/", File.separator));
}
return file;
}
@Override
public String getImplementationVersion() {
return Denizen.versionTag;
}
@Override
public void debugMessage(String message) {
Debug.log(message);
}
@Override
public void debugException(Throwable ex) {
Debug.echoError(ex);
}
@Override
public void debugError(String error) {
Debug.echoError(error);
}
@Override
public void debugError(ScriptQueue scriptQueue, String s) {
Debug.echoError(scriptQueue, s);
}
@Override
public void debugError(ScriptQueue scriptQueue, Throwable throwable) {
Debug.echoError(scriptQueue, throwable);
}
@Override
public void debugReport(Debuggable debuggable, String s, String s1) {
Debug.report(debuggable, s, s1);
}
@Override
public void debugReport(Debuggable debuggable, String s, Object... values) {
Debug.report(debuggable, s, values);
}
@Override
public void debugApproval(String message) {
Debug.echoApproval(message);
}
@Override
public void debugEntry(Debuggable debuggable, String s) {
Debug.echoDebug(debuggable, s);
}
@Override
public void debugEntry(Debuggable debuggable, com.denizenscript.denizencore.utilities.debugging.Debug.DebugElement debugElement, String s) {
Debug.echoDebug(debuggable, debugElement, s);
}
@Override
public void debugEntry(Debuggable debuggable, com.denizenscript.denizencore.utilities.debugging.Debug.DebugElement debugElement) {
Debug.echoDebug(debuggable, debugElement);
}
@Override
public String getImplementationName() {
return "Spigot";
}
@Override
public void preScriptReload() {
// Remove all recipes added by Denizen item scripts
ItemScriptHelper.removeDenizenRecipes();
// Remove all registered commands added by Denizen command scripts
CommandScriptHelper.removeDenizenCommands();
// Remove all registered economy scripts if needed
if (Depends.vault != null) {
EconomyScriptContainer.cleanup();
}
// Give map image downloads a new chance
DenizenMapManager.failedUrls.clear();
}
@Override
public void onScriptReload() {
Depends.setupEconomy();
Bukkit.getServer().getPluginManager().callEvent(new ScriptReloadEvent());
Denizen.getInstance().lastReloadTime = System.currentTimeMillis();
}
@Override
public boolean shouldDebug(Debuggable debug) {
return Debug.shouldDebug(debug);
}
@Override
public void debugQueueExecute(ScriptEntry entry, String queue, String execute) {
Consumer<String> altDebug = entry.getResidingQueue().debugOutput;
entry.getResidingQueue().debugOutput = null;
Debug.echoDebug(entry, com.denizenscript.denizencore.utilities.debugging.Debug.DebugElement.Header,
ChatColor.LIGHT_PURPLE + "Queue '" + queue + ChatColor.LIGHT_PURPLE + "' Executing: " + ChatColor.WHITE + execute);
entry.getResidingQueue().debugOutput = altDebug;
}
@Override
public void debugTagFill(Debuggable entry, String tag, String result) {
Debug.echoDebug(entry, ChatColor.DARK_GRAY + "Filled tag <" + ChatColor.WHITE + tag
+ ChatColor.DARK_GRAY + "> with '" + ChatColor.WHITE + result + ChatColor.DARK_GRAY + "'.");
}
@Override
public String queueHeaderInfo(ScriptEntry scriptEntry) {
BukkitScriptEntryData data = ((BukkitScriptEntryData) scriptEntry.entryData);
if (data.hasPlayer() && data.hasNPC()) {
return " with player '" + data.getPlayer().getName() + "' and NPC '" + data.getNPC().getId() + "/" + data.getNPC().getName() + "'";
}
else if (data.hasPlayer()) {
return " with player '" + data.getPlayer().getName() + "'";
}
else if (data.hasNPC()) {
return " with NPC '" + data.getNPC().getId() + "/" + data.getNPC().getName() + "'";
}
return "";
}
@Override
public boolean needsHandleArgPrefix(String prefix) {
return prefix.equals("player") || prefix.equals("npc");
}
// <--[language]
// @name The Player and NPC Arguments
// @group Script Command System
// @description
// The "player:<player>" and "npc:<npc>" arguments are special meta-arguments that are available for all commands, but are only useful for some.
// They are written like:
// - give stick player:<server.flag[some_player]>
// or:
// - assign set script:MyScript npc:<entry[save].created_npc>
//
// Denizen tracks a "linked player" and a "linked NPC" in queues and the commands within.
// Many commands automatically operate on the linked player/NPC default or exclusively
// (for example, "give" defaults to giving items to the linked player but that can be changed with the "to" argument,
// "assignment" exclusively changes the assignment of the linked NPC, and that cannot be changed except by the global NPC argument).
//
// When the player argument is used, it sets the linked player for the specific command it's on.
// This is only useful for commands that default to operating on the linked player.
// This can also be useful with the "run" command to link a specific player to the new queue.
//
// The NPC argument is essentially equivalent to the player argument, but for the linked NPC instead of the linked player.
//
// These arguments will also affect tags (mainly "<player>" and "<npc>") in the same command line (regardless of argument order).
// If you need to use the original player/NPC in a tag on the same line, use the define command to track it.
// -->
public static StrongWarning invalidPlayerArg = new StrongWarning("The 'player:' arg should not be used in commands like define/flag/yaml/... just input the player directly instead.");
public static StrongWarning invalidNpcArg = new StrongWarning("The 'npc:' arg should not be used in commands like define/flag/yaml/... just input the npc directly instead.");
public static HashSet<String> invalidPlayerArgCommands = new HashSet<>(Arrays.asList("DEFINE", "FLAG", "YAML"));
@Override
public boolean handleCustomArgs(ScriptEntry scriptEntry, Argument arg, boolean if_ignore) {
// Fill player/off-line player
if (arg.matchesPrefix("player") && !if_ignore) {
if (invalidPlayerArgCommands.contains(scriptEntry.getCommandName())) {
invalidPlayerArg.warn(scriptEntry);
}
Debug.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), scriptEntry.getContext());
PlayerTag player = PlayerTag.valueOf(value, scriptEntry.context);
if (player == null || !player.isValid()) {
Debug.echoError(scriptEntry.getResidingQueue(), value + " is an invalid player!");
}
((BukkitScriptEntryData) scriptEntry.entryData).setPlayer(player);
((BukkitTagContext) scriptEntry.context).player = player;
return true;
}
// Fill NPC argument
else if (arg.matchesPrefix("npc") && !if_ignore) {
if (invalidPlayerArgCommands.contains(scriptEntry.getCommandName())) {
invalidNpcArg.warn(scriptEntry);
}
Debug.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), scriptEntry.getContext());
NPCTag npc = NPCTag.valueOf(value, scriptEntry.context);
if (npc == null || !npc.isValid()) {
Debug.echoError(scriptEntry.getResidingQueue(), value + " is an invalid NPC!");
return false;
}
((BukkitScriptEntryData) scriptEntry.entryData).setNPC(npc);
((BukkitTagContext) scriptEntry.context).npc = npc;
return true;
}
return false;
}
@Override
public void refreshScriptContainers() {
CommandScriptHelper.commandScripts.clear();
InventoryScriptHelper.inventoryScripts.clear();
EntityScriptHelper.scripts.clear();
ItemScriptHelper.item_scripts.clear();
ItemScriptHelper.item_scripts_by_hash_id.clear();
}
@Override
public String scriptQueueSpeed() {
return Settings.scriptQueueSpeed();
}
@Override
public AbstractFlagTracker getServerFlags() {
return Denizen.getInstance().serverFlagMap;
}
@Override
public TagContext getTagContext(ScriptContainer container) {
return new BukkitTagContext(container);
}
@Override
public TagContext getTagContext(ScriptEntry scriptEntry) {
return new BukkitTagContext(scriptEntry);
}
@Override
public ScriptEntryData getEmptyScriptEntryData() {
return new BukkitScriptEntryData(null, null);
}
@Override
public int getTagTimeout() {
if (!Settings.tagTimeoutUnsafe()) {
return 0;
}
return Settings.tagTimeout();
}
@Override
public boolean allowConsoleRedirection() {
return Settings.allowConsoleRedirection();
}
@Override
public String cleanseLogString(String input) {
return cleanseLog(input);
}
public static String cleanseLog(String input) {
char esc_char = (char) 0x1b;
String esc = String.valueOf(esc_char);
String repc = String.valueOf(ChatColor.COLOR_CHAR);
if (input.indexOf(esc_char) != -1) {
input = StringUtils.replace(input, esc + "[0;30;22m", repc + "0");
input = StringUtils.replace(input, esc + "[0;34;22m", repc + "1");
input = StringUtils.replace(input, esc + "[0;32;22m", repc + "2");
input = StringUtils.replace(input, esc + "[0;36;22m", repc + "3");
input = StringUtils.replace(input, esc + "[0;31;22m", repc + "4");
input = StringUtils.replace(input, esc + "[0;35;22m", repc + "5");
input = StringUtils.replace(input, esc + "[0;33;22m", repc + "6");
input = StringUtils.replace(input, esc + "[0;37;22m", repc + "7");
input = StringUtils.replace(input, esc + "[0;30;1m", repc + "8");
input = StringUtils.replace(input, esc + "[0;34;1m", repc + "9");
input = StringUtils.replace(input, esc + "[0;32;1m", repc + "a");
input = StringUtils.replace(input, esc + "[0;36;1m", repc + "b");
input = StringUtils.replace(input, esc + "[0;31;1m", repc + "c");
input = StringUtils.replace(input, esc + "[0;35;1m", repc + "d");
input = StringUtils.replace(input, esc + "[0;33;1m", repc + "e");
input = StringUtils.replace(input, esc + "[0;37;1m", repc + "f");
input = StringUtils.replace(input, esc + "[5m", repc + "k");
input = StringUtils.replace(input, esc + "[21m", repc + "l");
input = StringUtils.replace(input, esc + "[9m", repc + "m");
input = StringUtils.replace(input, esc + "[4m", repc + "n");
input = StringUtils.replace(input, esc + "[3m", repc + "o");
input = StringUtils.replace(input, esc + "[m", repc + "r");
}
return input;
}
@Override
public boolean matchesType(String comparable, String comparedto) {
// Part of Deprecations.oldMatchesOperator
boolean outcome = false;
if (comparedto.equalsIgnoreCase("location")) {
outcome = LocationTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("material")) {
outcome = MaterialTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("materiallist")) {
outcome = ListTag.valueOf(comparable, CoreUtilities.basicContext).containsObjectsFrom(MaterialTag.class);
}
else if (comparedto.equalsIgnoreCase("entity")) {
outcome = EntityTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("spawnedentity")) {
outcome = (EntityTag.matches(comparable) && EntityTag.valueOf(comparable, CoreUtilities.basicContext).isSpawned());
}
else if (comparedto.equalsIgnoreCase("npc")) {
outcome = NPCTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("player")) {
outcome = PlayerTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("offlineplayer")) {
outcome = (PlayerTag.valueOf(comparable, CoreUtilities.basicContext) != null && !PlayerTag.valueOf(comparable, CoreUtilities.basicContext).isOnline());
}
else if (comparedto.equalsIgnoreCase("onlineplayer")) {
outcome = (PlayerTag.valueOf(comparable, CoreUtilities.basicContext) != null && PlayerTag.valueOf(comparable, CoreUtilities.basicContext).isOnline());
}
else if (comparedto.equalsIgnoreCase("item")) {
outcome = ItemTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("cuboid")) {
outcome = CuboidTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("trade")) {
outcome = TradeTag.matches(comparable);
}
else {
Debug.echoError("Invalid 'matches' type '" + comparedto + "'!");
}
return outcome;
}
@Override
public boolean allowedToWebget() {
return Settings.allowWebget();
}
public static Thread tagThread = null;
public static boolean isSafeThread() {
return Bukkit.isPrimaryThread() || Thread.currentThread().equals(tagThread);
}
@Override
public void preTagExecute() {
try {
NMSHandler.getInstance().disableAsyncCatcher();
tagThread = Thread.currentThread();
}
catch (Throwable e) {
Debug.echoError("Running not-Spigot?!");
}
}
@Override
public void postTagExecute() {
try {
NMSHandler.getInstance().undisableAsyncCatcher();
tagThread = null;
}
catch (Throwable e) {
Debug.echoError("Running not-Spigot?!");
}
}
Boolean tTimeoutSil = null;
@Override
public boolean tagTimeoutWhenSilent() {
if (tTimeoutSil == null) {
tTimeoutSil = Settings.tagTimeoutSilent();
}
return tTimeoutSil;
}
@Override
public boolean getDefaultDebugMode() {
return Settings.defaultDebugMode();
}
@Override
public boolean canWriteToFile(File f) {
return Utilities.canWriteToFile(f);
}
public static ChatColor[] DEBUG_FRIENDLY_COLORS = new ChatColor[] {
ChatColor.AQUA, ChatColor.BLUE, ChatColor.DARK_AQUA, ChatColor.DARK_BLUE, ChatColor.DARK_GREEN,
ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.GRAY, ChatColor.GREEN,
ChatColor.LIGHT_PURPLE, ChatColor.WHITE, ChatColor.YELLOW
};
@Override
public String getRandomColor() {
return DEBUG_FRIENDLY_COLORS[CoreUtilities.getRandom().nextInt(DEBUG_FRIENDLY_COLORS.length)].toString();
}
@Override
public int whileMaxLoops() {
return Settings.whileMaxLoops();
}
@Override
public boolean allowLogging() {
return Settings.allowLogging();
}
@Override
public boolean canReadFile(File f) {
return Utilities.canReadFile(f);
}
@Override
public boolean allowFileCopy() {
return Settings.allowFilecopy();
}
@Override
public File getDataFolder() {
return Denizen.getInstance().getDataFolder();
}
@Override
public boolean allowStrangeYAMLSaves() {
return Settings.allowStrangeYAMLSaves();
}
@Override
public void startRecording() {
Debug.record = true;
Debug.recording = new StringBuilder();
}
@Override
public void stopRecording() {
Debug.record = false;
Debug.recording = new StringBuilder();
}
@Override
public void submitRecording(Consumer<String> processResult) {
if (!Debug.record) {
processResult.accept("disabled");
return;
}
Debug.record = false;
final DebugSubmit submit = new DebugSubmit();
submit.recording = Debug.recording.toString();
Debug.recording = new StringBuilder();
submit.start();
BukkitRunnable task = new BukkitRunnable() {
public void run() {
if (!submit.isAlive()) {
this.cancel();
if (submit.result == null) {
processResult.accept(null);
}
else {
processResult.accept(submit.result);
}
}
}
};
task.runTaskTimer(Denizen.getInstance(), 0, 5);
}
@Override
public FlaggableObject simpleWordToFlaggable(String word, ScriptEntry entry) {
if (CoreUtilities.equalsIgnoreCase(word, "player")) {
return Utilities.getEntryPlayer(entry);
}
if (CoreUtilities.equalsIgnoreCase(word, "npc")) {
return Utilities.getEntryNPC(entry);
}
if (!word.contains("@")) {
Notable noted = NotableManager.getSavedObject(word);
if (noted instanceof LocationTag) {
return (LocationTag) noted;
}
}
ObjectTag obj = ObjectFetcher.pickObjectFor(word, entry.context);
if (obj instanceof FlaggableObject) {
return (FlaggableObject) obj;
}
return null;
}
public static BukkitScriptEntryData getScriptEntryData(ScriptQueue queue) {
if (queue.getLastEntryExecuted() != null) {
return (BukkitScriptEntryData) queue.getLastEntryExecuted().entryData;
}
else if (queue.getEntries().size() > 0) {
return (BukkitScriptEntryData) queue.getEntries().get(0).entryData;
}
return null;
}
@Override
public ObjectTag getSpecialDef(String def, ScriptQueue queue) {
if (def.equals("__player")) {
BukkitScriptEntryData data = getScriptEntryData(queue);
if (data == null) {
return null;
}
return data.getPlayer();
}
else if (def.equals("__npc")) {
BukkitScriptEntryData data = getScriptEntryData(queue);
if (data == null) {
return null;
}
return data.getNPC();
}
return null;
}
@Override
public boolean setSpecialDef(String def, ScriptQueue queue, ObjectTag value) {
if (def.equals("__player")) {
BukkitScriptEntryData baseData = getScriptEntryData(queue);
if (baseData == null) {
return true;
}
PlayerTag player = value == null ? null : value.asType(PlayerTag.class, baseData.getTagContext());
if (queue.getLastEntryExecuted() != null) {
((BukkitScriptEntryData) queue.getLastEntryExecuted().entryData).setPlayer(player);
}
for (ScriptEntry entry : queue.getEntries()) {
((BukkitScriptEntryData) entry.entryData).setPlayer(player);
}
return true;
}
else if (def.equals("__npc")) {
BukkitScriptEntryData baseData = getScriptEntryData(queue);
if (baseData == null) {
return true;
}
NPCTag npc = value == null ? null : value.asType(NPCTag.class, baseData.getTagContext());
if (queue.getLastEntryExecuted() != null) {
((BukkitScriptEntryData) queue.getLastEntryExecuted().entryData).setNPC(npc);
}
for (ScriptEntry entry : queue.getEntries()) {
((BukkitScriptEntryData) entry.entryData).setNPC(npc);
}
return true;
}
return false;
}
}