/
DenizenCoreImplementation.java
464 lines (405 loc) · 16.6 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
package com.denizenscript.denizen;
import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.scripts.containers.core.*;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizencore.DenizenImplementation;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.ScriptHelper;
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.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class DenizenCoreImplementation implements DenizenImplementation {
@Override
public File getScriptFolder() {
File file = null;
// Get the script directory
if (Settings.useDefaultScriptPath()) {
file = new File(DenizenAPI.getCurrentInstance()
.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 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();
}
}
@Override
public void onScriptReload() {
Depends.setupEconomy();
Bukkit.getServer().getPluginManager().callEvent(new ScriptReloadEvent());
}
@Override
public List<YamlConfiguration> getOutsideScripts() {
List<YamlConfiguration> files = new ArrayList<>();
try {
files.add(ScriptHelper.loadConfig("Denizen.jar/util.dsc", DenizenAPI.getCurrentInstance().getResource("util.dsc")));
}
catch (IOException e) {
Debug.echoError(e);
}
return files;
}
@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 TagContext getTagContextFor(ScriptEntry scriptEntry, boolean instant) {
return new BukkitTagContext(scriptEntry, instant);
}
@Override
public boolean needsHandleArgPrefix(String prefix) {
return prefix.equals("player") || prefix.equals("npc") || prefix.equals("npcid");
}
@Override
public boolean handleCustomArgs(ScriptEntry scriptEntry, Argument arg, boolean if_ignore) {
// Fill player/off-line player
if (arg.matchesPrefix("player") && !if_ignore) {
Debug.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
PlayerTag player = PlayerTag.valueOf(value);
if (player == null || !player.isValid()) {
Debug.echoError(scriptEntry.getResidingQueue(), value + " is an invalid player!");
}
((BukkitScriptEntryData) scriptEntry.entryData).setPlayer(player);
return true;
}
// Fill NPCID/NPC argument
else if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
Debug.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
NPCTag npc = NPCTag.valueOf(value);
if (npc == null || !npc.isValid()) {
Debug.echoError(scriptEntry.getResidingQueue(), value + " is an invalid NPC!");
return false;
}
((BukkitScriptEntryData) scriptEntry.entryData).setNPC(npc);
return true;
}
return false;
}
@Override
public void refreshScriptContainers() {
VersionScriptContainer.scripts.clear();
ItemScriptHelper.item_scripts.clear();
ItemScriptHelper.item_scripts_by_hash_id.clear();
InventoryScriptHelper.inventory_scripts.clear();
}
@Override
public String scriptQueueSpeed() {
return Settings.scriptQueueSpeed();
}
@Override
public ListTag valueOfFlagListTag(String string) {
FlagManager.Flag flag = DenizenAPI.getCurrentInstance().getFlag(string);
if (flag == null) {
return null;
}
return new ListTag(flag.toString(), true, flag.values());
}
@Override
public boolean matchesFlagListTag(String arg) {
boolean flag = false;
if (arg.startsWith("fl")) {
if (arg.indexOf('[') == 2) {
int cb = arg.indexOf(']');
if (cb > 4 && arg.indexOf('@') == (cb + 1)) {
String owner = arg.substring(3, cb);
flag = arg.substring(cb + 2).length() > 0 && (PlayerTag.matches(owner)
|| (Depends.citizens != null && NPCTag.matches(owner)));
}
}
else if (arg.indexOf('@') == 2) {
flag = arg.substring(3).length() > 0;
}
}
return flag;
}
@Override
public String getLastEntryFromFlag(String flag) {
FlagManager.Flag theflag = DenizenAPI.getCurrentInstance().getFlag(flag);
if (theflag == null || theflag.getLast() == null) {
return null;
}
return theflag.getLast().asString();
}
@Override
public TagContext getTagContext(ScriptEntry scriptEntry) {
return new BukkitTagContext(scriptEntry, false);
}
@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) {
String esc = String.valueOf((char) 0x1b);
String repc = String.valueOf(ChatColor.COLOR_CHAR);
if (input.contains(esc)) {
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) {
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).containsObjectsFrom(MaterialTag.class);
}
else if (comparedto.equalsIgnoreCase("entity")) {
outcome = EntityTag.matches(comparable);
}
else if (comparedto.equalsIgnoreCase("spawnedentity")) {
outcome = (EntityTag.matches(comparable) && EntityTag.valueOf(comparable).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) != null && !PlayerTag.valueOf(comparable).isOnline());
}
else if (comparedto.equalsIgnoreCase("onlineplayer")) {
outcome = (PlayerTag.valueOf(comparable) != null && PlayerTag.valueOf(comparable).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 DenizenAPI.getCurrentInstance().getDataFolder();
}
@Override
public boolean allowStrangeYAMLSaves() {
return Settings.allowStrangeYAMLSaves();
}
}