-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CoreUtilities.java
487 lines (432 loc) · 16.9 KB
/
CoreUtilities.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
package com.denizenscript.denizencore.utilities;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
public class CoreUtilities {
public static TagContext noDebugContext;
public static String join(String delim, List objects) {
StringBuilder output = new StringBuilder(objects.size() * 5);
for (int i = 0; i < objects.size(); i++) {
output.append(objects.get(i));
if (i + 1 < objects.size()) {
output.append(delim);
}
}
return output.toString();
}
public static String stringifyNullPass(Object obj) {
return obj == null ? null : obj.toString();
}
public static ObjectTag fixType(ObjectTag input, TagContext context) {
if (input instanceof ElementTag) {
return ObjectFetcher.pickObjectFor(input.toString(), context);
}
return input;
}
public static void autoPropertyMechanism(ObjectTag object, Mechanism mechanism) {
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getObjectTagClass());
if (properties == null) {
return;
}
PropertyParser.PropertyGetter specificGetter = properties.propertiesByMechanism.get(mechanism.getName());
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
return;
}
prop.adjust(mechanism);
return;
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyMechs) {
Property prop = listGetter.get(object);
if (prop != null) {
prop.adjust(mechanism);
if (mechanism.fulfilled()) {
return;
}
}
}
}
public static ObjectTag autoPropertyTagObject(ObjectTag object, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getObjectTagClass());
if (properties == null) {
return null;
}
String tagName = attribute.getAttributeWithoutContext(1);
PropertyParser.PropertyGetter specificGetter = properties.propertiesByTag.get(tagName);
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
String propName = properties.propertyNamesByTag.get(tagName);
attribute.seemingSuccesses.add(attribute.getAttributeWithoutContext(1) + " - property " + propName + " matched, but is not valid for the object.");
return null;
}
return prop.getObjectAttribute(attribute);
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyTags) {
Property prop = listGetter.get(object);
if (prop != null) {
ObjectTag returned = prop.getObjectAttribute(attribute);
if (returned != null) {
return returned;
}
}
}
return null;
}
public static String autoPropertyTag(ObjectTag object, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
PropertyParser.ClassPropertiesInfo properties = PropertyParser.propertiesByClass.get(object.getObjectTagClass());
if (properties == null) {
return null;
}
String tagName = attribute.getAttributeWithoutContext(1);
PropertyParser.PropertyGetter specificGetter = properties.propertiesByTag.get(tagName);
if (specificGetter != null) {
Property prop = specificGetter.get(object);
if (prop == null) {
String propName = properties.propertyNamesByTag.get(tagName);
attribute.seemingSuccesses.add(attribute.getAttributeWithoutContext(1) + " - property " + propName + " matched, but is not valid for the object.");
return null;
}
return prop.getAttribute(attribute);
}
for (PropertyParser.PropertyGetter listGetter : properties.propertiesAnyTags) {
Property prop = listGetter.get(object);
if (prop != null) {
String returned = prop.getAttribute(attribute);
if (returned != null) {
return returned;
}
}
}
return null;
}
public static ObjectTag autoAttrib(Property inp, Attribute attribute) {
if (attribute.isComplete()) {
return null;
}
return inp.getObjectAttribute(attribute);
}
public static ObjectTag autoAttribTyped(ObjectTag inp, Attribute attribute) {
return autoAttrib(fixType(inp, attribute.context), attribute);
}
public static ObjectTag autoAttrib(ObjectTag inp, Attribute attribute) {
if (inp == null) {
Debug.echoError("Tag parse failed (null return) for tag <" + attribute.toString() + ">!");
return null;
}
if (attribute.isComplete()) {
return inp;
}
return inp.getObjectAttribute(attribute);
}
public static <T extends ObjectTag> T asType(ObjectTag inp, Class<T> type, TagContext context) {
if (inp.getObjectTagClass() == type) {
return (T) inp;
}
if (type == ElementTag.class) {
return (T) new ElementTag(inp.toString());
}
return ObjectFetcher.getObjectFrom(type, inp.toString(), context);
}
public static abstract class TypeComparisonRunnable {
public abstract boolean canBecome(ObjectTag inp);
}
public final static Map<Class<? extends ObjectTag>, TypeComparisonRunnable> typeCheckers = new HashMap<>();
static {
registerTypeAsTrueAlways(ElementTag.class);
registerTypeAsTrueAlways(ListTag.class);
registerTypeAsNoOtherTypeCode(ScriptTag.class, "s");
registerTypeAsNoOtherTypeCode(DurationTag.class, "d");
registerTypeAsNoOtherTypeCode(CustomObjectTag.class, "custom");
registerTypeAsNoOtherTypeCode(QueueTag.class, "q");
}
public static void registerTypeAsNoOtherTypeCode(Class<? extends ObjectTag> type, final String knownCode) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(ObjectTag inp) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals(knownCode)) {
return false;
}
}
return true;
}
});
}
public static void registerTypeAsTrueAlways(Class<? extends ObjectTag> type) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(ObjectTag inp) {
return true;
}
});
}
public static boolean canPossiblyBeType(ObjectTag inp, Class<? extends ObjectTag> type) {
if (inp.getObjectTagClass() == type) {
return true;
}
TypeComparisonRunnable comp = typeCheckers.get(type);
if (comp != null && !comp.canBecome(inp)) {
return false;
}
return ObjectFetcher.checkMatch(type, inp.toString());
}
public static void deleteDirectory(File directory) throws IOException {
Files.walkFileTree(directory.toPath(),
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
});
}
public static void copyDirectory(File source, File destination) throws IOException {
copyDirectory(source.toPath(), destination.toPath());
}
public static void copyDirectory(Path source, Path destination) throws IOException {
Files.walk(source).forEach(file -> {
try {
Files.copy(file, destination.resolve(source.relativize(file)));
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
});
}
static Random random = new Random();
public static Random getRandom() {
return random;
}
static FilenameFilter scriptsFilter;
static {
scriptsFilter = new FilenameFilter() {
public boolean accept(File file, String fileName) {
if (fileName.startsWith(".")) {
return false;
}
String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if (ext.equalsIgnoreCase("DSCRIPT")) {
Debug.echoError("'.dscript' extension has never been officially supported. Please use '.dsc'. Regarding file " + fileName);
return true;
}
return ext.equalsIgnoreCase("YML") || ext.equalsIgnoreCase("DSC");
}
};
}
public static String bigDecToString(BigDecimal input) {
String temp = input.toString();
if (temp.contains(".")) {
for (int i = temp.length() - 1; i >= 0; i--) {
if (temp.charAt(i) != '0') {
if (temp.charAt(i) == '.') {
return temp.substring(0, i);
}
return temp.substring(0, i + 1);
}
}
}
return temp;
}
public static String doubleToString(double input) {
String temp = String.valueOf(input);
if (temp.endsWith(".0")) {
return temp.substring(0, temp.length() - 2);
}
return temp;
}
/**
* Lists all files in the given directory.
*
* @param dir The directory to search in
* @return A {@link java.io.File} collection
*/
public static List<File> listDScriptFiles(File dir) {
List<File> files = new ArrayList<>();
File[] entries = dir.listFiles();
for (File file : entries) {
// Add file
if (scriptsFilter == null || scriptsFilter.accept(dir, file.getName())) {
files.add(file);
}
// Add subdirectories
if (file.isDirectory()) {
files.addAll(listDScriptFiles(file));
}
}
return files;
}
public static boolean contains(String str, char c) {
return str.indexOf(c) >= 0;
}
public static String concat(List<String> str, String split) {
StringBuilder sb = new StringBuilder();
if (str.size() > 0) {
sb.append(str.get(0));
}
for (int i = 1; i < str.size(); i++) {
sb.append(split).append(str.get(i));
}
return sb.toString();
}
public static List<String> split(String str, char c) {
List<String> strings = new ArrayList<>();
int start = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
strings.add(str.substring(start, i));
start = i + 1;
}
}
strings.add(str.substring(start));
return strings;
}
public static List<String> split(String str, char c, int max) {
List<String> strings = new ArrayList<>();
int start = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
strings.add(str.substring(start, i));
start = i + 1;
if (strings.size() + 1 == max) {
break;
}
}
}
strings.add(str.substring(start));
if (Debug.verbose) {
Debug.log("Splitting " + str + " around " + c + " limited to " + max + " returns " + concat(strings, ":::"));
}
return strings;
}
public static String toLowerCase(String input) {
char[] data = input.toCharArray();
for (int i = 0; i < data.length; i++) {
if (data[i] >= 'A' && data[i] <= 'Z') {
data[i] -= 'A' - 'a';
}
}
return new String(data);
}
public static String getXthArg(int argc, String args) {
char[] data = args.toCharArray();
StringBuilder nArg = new StringBuilder();
int arg = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] == ' ') {
arg++;
if (arg > argc) {
return nArg.toString();
}
}
else if (arg == argc) {
nArg.append(data[i]);
}
}
return nArg.toString();
}
public static boolean xthArgEquals(int argc, String args, String input) {
char[] data = args.toCharArray();
char[] data2 = input.toCharArray();
int arg = 0;
int x = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] == ' ') {
arg++;
}
else if (arg == argc) {
if (x == data2.length) {
return false;
}
if (data2[x++] != data[i]) {
return false;
}
}
}
return x == data2.length;
}
public static String getClosestOption(List<String> strs, String opt) {
int minDist = Integer.MAX_VALUE;
opt = CoreUtilities.toLowerCase(opt);
String closest = "";
for (String cmd : strs) {
String comp = CoreUtilities.toLowerCase(cmd);
int distance = getLevenshteinDistance(opt, comp);
if (minDist > distance) {
minDist = distance;
closest = cmd;
}
}
return closest;
}
public static int getLevenshteinDistance(String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
}
else if (m == 0) {
return n;
}
int[] p = new int[n + 1]; // 'previous' cost array, horizontally
int[] d = new int[n + 1]; // cost array, horizontally
int[] _d; // placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i <= n; i++) {
p[i] = i;
}
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
d[0] = j;
for (i = 1; i <= n; i++) {
cost = s.charAt(i - 1) == t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left
// and up +cost
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
}