-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ElementTag.java
2396 lines (2216 loc) · 93.6 KB
/
ElementTag.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.scripts.commands.Comparable;
import com.denizenscript.denizencore.tags.*;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.SQLEscaper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import javax.xml.bind.DatatypeConverter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ElementTag implements ObjectTag {
// <--[language]
// @name ElementTag Objects
// @group Object System
// @description
// ElementTags are simple objects that contain a simple bit of text.
// Their main usage is within the replaceable tag system,
// often times returned from the use of another tag that isn't returning a specific object type, such as a location or entity.
// For example, <player.name> or <list[item1|item2|item3].comma_separated> will both return ElementTags.
//
// Pluses to the ElementTag system is the ability to utilize its tag attributes,
// which can provide a range of functionality that should be familiar from any other programming language,
// such as 'to_uppercase', 'split', 'replace', 'contains', and many more.
// See 'ElementTag.*' tags for more information.
//
// While information fetched from other tags resulting in an ElementTag is often times automatically handled,
// it may be desirable to utilize element attributes from text/numbers/etc. that aren't already an element object.
// To accomplish this, the standard 'element' tag base can be used for the creation of a new element.
// For example: <element[This_is_a_test].to_uppercase>
// will result in the value 'THIS_IS_A_TEST'.
//
// Note that while other objects often return their object identifier (p@, li@, e@, etc.), elements usually do not (except special type-validation circumstances).
// They will, however, recognize the object notation "el@" if it is used.
//
// -->
// <--[language]
// @name ElementTag(Boolean)
// @group Object System
// @description
// When "ElementTag(Boolean)" appears in meta documentation, this means the input/output is an ElementTag
// (refer to <@link language ElementTag Objects>) that is a boolean.
// Boolean means either a "true" or a "false".
// -->
// <--[language]
// @name ElementTag(Number)
// @group Object System
// @description
// When "ElementTag(Number)" appears in meta documentation, this means the input/output is an ElementTag
// (refer to <@link language ElementTag Objects>) that is an integer number.
// That is, for example: 0, 1, 5, -4, 10002325 or any other number.
// This does NOT include decimal numbers (like 1.5). Those will be documented as <@link language ElementTag(Decimal)>.
//
// In some cases, this will also be documented as "<#>".
// -->
// <--[language]
// @name ElementTag(Decimal)
// @group Object System
// @description
// When "ElementTag(Decimal)" appears in meta documentation, this means the input/output is an ElementTag
// (refer to <@link language ElementTag Objects>) that is a decimal number.
// That is, for example: 0, 1, 5, -4, 10002325, 4.2, -18.281241 or any other number.
// While this is specifically for decimal numbers, the decimal itself is optional (will be assumed as ".0").
//
// In some cases, this will also be documented as "<#.#>".
// -->
@Deprecated
public static ElementTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("el")
public static ElementTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
return new ElementTag(CoreUtilities.toLowerCase(string).startsWith("el@") ? string.substring(3) : string);
}
public static boolean matches(String string) {
return string != null;
}
/**
* Handle null ObjectTags appropriately for potentionally null tags.
* Will show a dB error message and return Element.NULL for null objects.
*
* @param tag The input string that produced a potentially null object, for debugging.
* @param object The potentially null object.
* @param type The type of object expected, for debugging. (EG: 'dNPC')
* @return The object or Element.NULL if the object is null.
*/
public static <T extends ObjectTag> T handleNull(String tag, T object, String type, boolean has_fallback) {
if (object == null) {
if (!has_fallback) {
Debug.echoError("'" + tag + "' is an invalid " + type + "!");
}
return null;
}
return object;
}
private final String element;
public ElementTag(String string) {
this.prefix = "element";
if (string == null) {
if (Debug.verbose) {
try {
throw new RuntimeException("Trace");
}
catch (Exception ex) {
Debug.echoError(ex);
}
Debug.log("Element - Null construction!");
}
this.element = "null";
}
else {
this.element = string;
}
}
public ElementTag(boolean bool) {
this.prefix = "boolean";
this.element = String.valueOf(bool);
}
public ElementTag(int integer) {
this.prefix = "number";
this.element = String.valueOf(integer);
}
public ElementTag(byte byt) {
this.prefix = "number";
this.element = String.valueOf(byt);
}
public ElementTag(short shrt) {
this.prefix = "number";
this.element = String.valueOf(shrt);
}
public ElementTag(long lng) {
this.prefix = "number";
this.element = String.valueOf(lng);
}
public ElementTag(BigDecimal bdl) {
this.prefix = "decimal";
this.element = CoreUtilities.bigDecToString(bdl);
}
public ElementTag(double dbl) {
this.prefix = "decimal";
this.element = CoreUtilities.doubleToString(dbl);
}
public ElementTag(float flt) {
this.prefix = "decimal";
this.element = CoreUtilities.doubleToString(flt);
}
public ElementTag(String prefix, String string) {
if (prefix == null) {
this.prefix = "element";
}
else {
this.prefix = prefix;
}
this.element = string;
}
static final BigDecimal max = new BigDecimal("10E1000");
private BigDecimal getBD(String text) {
BigDecimal bd = new BigDecimal(text);
if (bd.compareTo(max) >= 1) {
Debug.echoError("Unreasonably large number detected!");
return max;
}
return bd;
}
public BigDecimal asBigDecimal() {
return getBD(element.replaceAll("%", ""));
}
public double asDouble() {
return Double.parseDouble(element.replaceAll("%", ""));
}
public float asFloat() {
return Float.parseFloat(element.replaceAll("%", ""));
}
public int asInt() {
try {
return Integer.parseInt(element.replaceAll("(%)|(\\.\\d+)", ""));
}
catch (NumberFormatException ex) {
Debug.echoError("'" + element + "' is not a valid integer!");
return 0;
}
}
public long asLong() {
try {
return Long.parseLong(element.replaceAll("(%)|(\\.\\d+)", ""));
}
catch (NumberFormatException ex) {
Debug.echoError("'" + element + "' is not a valid integer!");
return 0;
}
}
public boolean asBoolean() {
return CoreUtilities.equalsIgnoreCase(element, "true");
}
public String asString() {
return element;
}
public boolean isBoolean() {
return CoreUtilities.equalsIgnoreCase(element, "true") || CoreUtilities.equalsIgnoreCase(element, "false");
}
public boolean isDouble() {
try {
if (!Double.valueOf(element).isNaN()) {
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isFloat() {
try {
if (!Float.valueOf(element).isNaN()) {
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isInt() {
try {
Integer val = Integer.valueOf(element.replaceAll("(%)|(\\.\\d+)", ""));
if (val.hashCode() != 0.5) { // if intentionally always passes
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isString() {
return (element != null && !element.isEmpty());
}
public boolean matchesType(Class<? extends ObjectTag> dClass) {
return ObjectFetcher.checkMatch(dClass, element);
}
public <T extends ObjectTag> T asType(Class<T> dClass, TagContext context) {
return ObjectFetcher.getObjectFrom(dClass, element, context);
}
public boolean matchesEnum(Enum[] values) {
for (Enum value : values) {
if (CoreUtilities.equalsIgnoreCase(value.name(), element)) {
return true;
}
}
return false;
}
private String prefix;
@Override
public String getObjectType() {
return "Element";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String identify() {
return element;
}
@Override
public String savable() {
return "el@" + element;
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public boolean isUnique() {
return false;
}
public static void registerTags() {
/////////////////////
// CONVERSION ATTRIBUTES
/////////////////
registerTag("is", (attribute, object) -> {
// <--[tag]
// @attribute <ElementTag.is[<operator>].to[<element>]>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag ElementTag.is[<operator>].than[<element>]>
// -->
// <--[tag]
// @attribute <ElementTag.is[<operator>].than[<element>]>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag ElementTag.is[<operator>].to[<element>]>
// -->
if (attribute.hasContext(1) && (attribute.startsWith("to", 2) || attribute.startsWith("than", 2)) && attribute.hasContext(2)) {
// Use the Comparable object as implemented for the IF command. First, a new Comparable!
Comparable com = new Comparable();
com.context = attribute.context;
// Check for negative logic
String operator;
if (attribute.getContext(1).startsWith("!")) {
operator = attribute.getContext(1).substring(1);
com.setNegativeLogic();
}
else {
operator = attribute.getContext(1);
}
// Operator is the value of the .is[] context. Valid are Comparable.Operators, same
// as used by the IF command.
Comparable.Operator comparableOperator = null;
try {
comparableOperator = Comparable.Operator.valueOf(operator.replace("==", "EQUALS")
.replace(">=", "OR_MORE").replace("<=", "OR_LESS").replace("<", "LESS")
.replace(">", "MORE").replace("=", "EQUALS").toUpperCase());
}
catch (IllegalArgumentException e) {
}
if (comparableOperator != null) {
com.setOperator(comparableOperator);
// Comparable is the value of this element
com.setComparable(object.toString());
// Compared_to is the value of the .to[] context.
com.setComparedto(attribute.getContext(2));
attribute.fulfill(1);
return new ElementTag(com.determineOutcome());
}
else {
attribute.echoError("Unknown operator '" + operator + "'.");
}
}
return null;
});
// <--[tag]
// @attribute <ElementTag.is_boolean>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Returns whether the element is a boolean ('true' or 'false').
// -->
registerTag("is_boolean", (attribute, object) -> {
return new ElementTag(object.isBoolean());
});
// <--[tag]
// @attribute <ElementTag.is_integer>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Returns whether the element is an integer number (a number without a decimal point).
// -->
registerTag("is_integer", (attribute, object) -> {
if (!ArgumentHelper.matchesInteger(object.element)) {
return new ElementTag(false);
}
try {
object.asLong();
return new ElementTag(true);
}
catch (NumberFormatException ex) {
return new ElementTag(false);
}
});
// <--[tag]
// @attribute <ElementTag.is_decimal>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Returns whether the element is a valid decimal number (the decimal point is optional).
// -->
registerTag("is_decimal", (attribute, object) -> {
if (!ArgumentHelper.matchesDouble(object.element)) {
return new ElementTag(false);
}
try {
return new ElementTag(object.asBigDecimal() != null);
}
catch (NumberFormatException ex) {
return new ElementTag(false);
}
});
// <--[tag]
// @attribute <ElementTag.is_odd>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Returns whether the element is an odd-valued decimal number. Returns 'false' for non-numbers.
// -->
registerTag("is_odd", (attribute, object) -> {
String element = object.element;
return new ElementTag(ArgumentHelper.matchesDouble(element) && (object.asBigDecimal().longValue() % 2) != 0);
});
// <--[tag]
// @attribute <ElementTag.is_even>
// @returns ElementTag(Boolean)
// @group comparison
// @description
// Returns whether the element is an even-valued decimal number. Returns 'false' for non-numbers.
// -->
registerTag("is_even", (attribute, object) -> {
String element = object.element;
return new ElementTag(ArgumentHelper.matchesDouble(element) && (object.asBigDecimal().longValue() % 2) == 0);
});
// <--[tag]
// @attribute <ElementTag.as_element>
// @returns ElementTag
// @group conversion
// @description
// Returns the element as itself.
// For use in special cases, generally not very useful.
// -->
registerTag("as_element", (attribute, object) -> {
return object;
}, "aselement");
// <--[tag]
// @attribute <ElementTag.as_boolean>
// @returns ElementTag(Boolean)
// @group conversion
// @description
// Returns the element as true/false.
// -->
registerTag("as_boolean", (attribute, object) -> {
String element = object.element;
return new ElementTag(element.equalsIgnoreCase("true")
|| element.equalsIgnoreCase("t")
|| element.equalsIgnoreCase("1"));
}, "asboolean");
// <--[tag]
// @attribute <ElementTag.as_decimal>
// @returns ElementTag(Decimal)
// @group conversion
// @description
// Returns the element as a decimal number, or shows an error.
// -->
registerTag("as_decimal", (attribute, object) -> {
String element = object.element;
try {
return new ElementTag(Double.valueOf(element));
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
attribute.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}, "as_double", "asdouble");
registerTag("as_int", (attribute, object) -> {
Deprecations.elementAsIntTag.warn(attribute.context);
String element = object.element;
try {
return new ElementTag(Double.valueOf(element).longValue());
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
attribute.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}, "asint");
// <--[tag]
// @attribute <ElementTag.truncate>
// @returns ElementTag(Number)
// @group conversion
// @description
// Returns the element as a number without a decimal by way of stripping the decimal value off the end.
// That is, rounds towards zero.
// This is an extremely special case tag that should only be used in very specific situations.
// If at all unsure, this is probably the wrong tag. Consider <@link tag elementtag.round> or <@link tag elementtag.round_down> instead.
// -->
registerTag("truncate", (attribute, object) -> {
try {
return new ElementTag(object.asBigDecimal().longValue());
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
attribute.echoError("'" + object.element + "' is not a valid decimal number.");
}
return null;
}
});
// <--[tag]
// @attribute <ElementTag.as_money>
// @returns ElementTag(Decimal)
// @group conversion
// @description
// Returns the element as a number with two decimal places.
// -->
registerTag("as_money", (attribute, object) -> {
String element = object.element;
try {
DecimalFormat d = new DecimalFormat("0.00", CoreUtilities.decimalFormatSymbols);
return new ElementTag(d.format(Double.valueOf(element)));
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
attribute.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}, "asmoney");
// <--[tag]
// @attribute <ElementTag.as_list>
// @returns ListTag
// @group conversion
// @description
// Returns the element as a ListTag.
// -->
registerTag("as_list", (attribute, object) -> {
String element = object.element;
return handleNull(element, ListTag.valueOf(element, attribute.context), "ListTag", attribute.hasAlternative());
}, "aslist");
// <--[tag]
// @attribute <ElementTag.as_map>
// @returns MapTag
// @group conversion
// @description
// Returns the element as a MapTag.
// -->
registerTag("as_map", (attribute, object) -> {
String element = object.element;
return handleNull(element, MapTag.valueOf(element, attribute.context), "MapTag", attribute.hasAlternative());
});
// <--[tag]
// @attribute <ElementTag.as_custom>
// @returns CustomObject
// @group conversion
// @description
// Returns the element as a custom object.
// -->
registerTag("as_custom", (attribute, object) -> {
String element = object.element;
return handleNull(element, CustomObjectTag.valueOf(element, attribute.context), "Custom", attribute.hasAlternative());
}, "ascustom");
// <--[tag]
// @attribute <ElementTag.as_script>
// @returns ScriptTag
// @group conversion
// @description
// Returns the element as a ScriptTag.
// Note: the value must be a valid script.
// -->
registerTag("as_script", (attribute, object) -> {
String element = object.element;
return handleNull(element, ScriptTag.valueOf(element, attribute.context), "ScriptTag", attribute.hasAlternative());
}, "asscript");
// <--[tag]
// @attribute <ElementTag.as_queue>
// @returns QueueTag
// @group conversion
// @description
// Returns the element as a QueueTag.
// Note: the value must be a valid QueueTag.
// -->
registerTag("as_queue", (attribute, object) -> {
String element = object.element;
return handleNull(element, QueueTag.valueOf(element, attribute.context), "QueueTag", attribute.hasAlternative());
}, "asqueue");
// <--[tag]
// @attribute <ElementTag.as_duration>
// @returns DurationTag
// @group conversion
// @description
// Returns the element as a Duration.
// Note: the value must be a valid Duration.
// -->
registerTag("as_duration", (attribute, object) -> {
String element = object.element;
return handleNull(element, DurationTag.valueOf(element, attribute.context), "DurationTag", attribute.hasAlternative());
}, "asduration");
// <--[tag]
// @attribute <ElementTag.escaped>
// @returns ElementTag
// @group conversion
// @description
// Returns the element, escaped for safe reuse.
// Inverts <@link tag ElementTag.unescaped>.
// See <@link language Escaping System>.
// -->
registerTag("escaped", (attribute, object) -> {
String element = object.element;
return new ElementTag(EscapeTagBase.escape(element));
});
// <--[tag]
// @attribute <ElementTag.sql_escaped>
// @returns ElementTag
// @group conversion
// @description
// Returns the element, escaped for safe use in SQL.
// -->
registerTag("sql_escaped", (attribute, object) -> {
String element = object.element;
return new ElementTag(SQLEscaper.escapeSQL(element));
});
// <--[tag]
// @attribute <ElementTag.unescaped>
// @returns ElementTag
// @group conversion
// @description
// Returns the element, unescaped.
// Inverts <@link tag ElementTag.escaped>.
// See <@link language Escaping System>.
// -->
registerTag("unescaped", (attribute, object) -> {
String element = object.element;
return new ElementTag(EscapeTagBase.unEscape(element));
});
// <--[tag]
// @attribute <ElementTag.parsed>
// @returns ElementTag
// @group conversion
// @description
// Returns the element, with any contained tags parsed.
// WARNING: THIS TAG IS DANGEROUS TO USE, DO NOT USE IT UNLESS
// YOU KNOW WHAT YOU ARE DOING. USE AT YOUR OWN RISK.
// -->
registerTag("parsed", (attribute, object) -> {
return TagManager.tagObject(object.element, attribute.context);
});
/////////////////////
// ELEMENT CHECKING ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <ElementTag.difference[<element>]>
// @returns ElementTag(Number)
// @group element checking
// @description
// Returns a number representing the difference between the two elements. (Uses Levenshtein logic).
// -->
registerTag("difference", (attribute, object) -> {
String element = object.element;
String two = attribute.getContext(1);
return new ElementTag(CoreUtilities.getLevenshteinDistance(element, two));
});
// <--[tag]
// @attribute <ElementTag.contains_any_case_sensitive_text[<element>|...]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains any of a list of specified elements, case sensitive.
// -->
registerTag("contains_any_case_sensitive_text", (attribute, object) -> {
String element = object.element;
ListTag list = attribute.contextAsType(1, ListTag.class);
for (String list_element : list) {
if (element.contains(list_element)) {
return new ElementTag(true);
}
}
return new ElementTag(false);
});
tagProcessor.registerFutureTagDeprecation("contains_any_case_sensitive_text", "contains_any_case_sensitive");
// <--[tag]
// @attribute <ElementTag.contains_any_text[<element>|...]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains any of a list of specified elements, case insensitive.
// -->
registerTag("contains_any_text", (attribute, object) -> {
String element = object.element;
ListTag list = ListTag.valueOf(CoreUtilities.toLowerCase(attribute.getContext(1)), attribute.context);
String ellow = CoreUtilities.toLowerCase(element);
for (String list_element : list) {
if (ellow.contains(list_element)) {
return new ElementTag(true);
}
}
return new ElementTag(false);
});
tagProcessor.registerFutureTagDeprecation("contains_any_text", "contains_any");
// <--[tag]
// @attribute <ElementTag.contains_case_sensitive_text[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains a specified element, case sensitive.
// -->
registerTag("contains_case_sensitive_text", (attribute, object) -> {
String element = object.element;
String contains = attribute.getContext(1);
if (element.contains(contains)) {
return new ElementTag("true");
}
else {
return new ElementTag("false");
}
});
tagProcessor.registerFutureTagDeprecation("contains_case_sensitive_text", "contains_case_sensitive");
// <--[tag]
// @attribute <ElementTag.contains_text[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains a specified element, case insensitive. Can use
// regular expression by prefixing the element with regex:
// -->
registerTag("contains_text", (attribute, object) -> {
String element = object.element;
String contains = attribute.getContext(1);
if (CoreUtilities.toLowerCase(contains).startsWith("regex:")) {
if (Pattern.compile(contains.substring(("regex:").length()), Pattern.CASE_INSENSITIVE).matcher(element).find()) {
return new ElementTag("true");
}
else {
return new ElementTag("false");
}
}
else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(contains))) {
return new ElementTag("true");
}
else {
return new ElementTag("false");
}
});
tagProcessor.registerFutureTagDeprecation("contains_text", "contains");
// <--[tag]
// @attribute <ElementTag.contains_all_text[<element>|...]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains all of the specified strings, case insensitive.
// -->
registerTag("contains_all_text", (attribute, object) -> {
String element = object.element;
ListTag list = ListTag.valueOf(CoreUtilities.toLowerCase(attribute.getContext(1)), attribute.context);
String ellow = CoreUtilities.toLowerCase(element);
for (String list_element : list) {
if (!ellow.contains(list_element)) {
return new ElementTag("false");
}
}
return new ElementTag("true");
});
tagProcessor.registerFutureTagDeprecation("contains_all_text", "contains_all");
// <--[tag]
// @attribute <ElementTag.contains_all_case_sensitive_text[<element>|...]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element contains all of the specified strings, case sensitive.
// -->
registerTag("contains_all_case_sensitive_text", (attribute, object) -> {
String element = object.element;
ListTag list = attribute.contextAsType(1, ListTag.class);
for (String list_element : list) {
if (!element.contains(list_element)) {
return new ElementTag("false");
}
}
return new ElementTag("true");
});
tagProcessor.registerFutureTagDeprecation("contains_all_case_sensitive_text", "contains_all_case_sensitive");
// <--[tag]
// @attribute <ElementTag.ends_with[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element ends with a specified element.
// -->
registerTag("ends_with", (attribute, object) -> {
return new ElementTag(CoreUtilities.toLowerCase(object.element).
endsWith(CoreUtilities.toLowerCase(attribute.getContext(1))));
}, "endswith");
// <--[tag]
// @attribute <ElementTag.equals_case_sensitive[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element matches another element, case-sensitive.
// -->
registerTag("equals_case_sensitive", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("The tag ElementTag.equals_case_sensitive[...] must have a value.");
return null;
}
return new ElementTag(object.element.equals(attribute.getContext(1)));
}, "equals_with_case");
// <--[tag]
// @attribute <ElementTag.advanced_matches[<matcher>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element matches some matcher text, using the system behind <@link language Advanced Script Event Matching>.
// -->
registerTag("advanced_matches", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("The tag ElementTag.advanced_matches[...] must have a value.");
return null;
}
return new ElementTag(ScriptEvent.createMatcher(attribute.getContext(1)).doesMatch(object.element));
});
// <--[tag]
// @attribute <ElementTag.regex_matches[<regex>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether the element matches a regex input.
// -->
registerTag("regex_matches", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("The tag ElementTag.matches[...] must have a value.");
return null;
}
return new ElementTag(object.element.matches(attribute.getContext(1)));
}, "matches");
// <--[tag]
// @attribute <ElementTag.regex[<regex>].group[<group>]>
// @returns ElementTag
// @group element checking
// @description
// Returns the specific group from a regex match.
// Specify group 0 for the whole match.
// For example, <element[hello5world].regex[.*(\d).*].group[1]> returns '5'.
// -->
registerTag("regex", (attribute, object) -> {
if (!attribute.hasContext(1) || !attribute.hasContext(2)) {
attribute.echoError("The tag ElementTag.regex[...] must have a value.");
return null;
}
String regex = attribute.getContext(1);
Matcher m = Pattern.compile(regex).matcher(object.element);
if (!m.matches()) {
return null;
}
int group = new ElementTag(attribute.getContext(2)).asInt();
if (group < 0) {
group = 0;
}
if (group > m.groupCount()) {
group = m.groupCount();
}
attribute.fulfill(1);
return new ElementTag(m.group(group));
});
// <--[tag]
// @attribute <ElementTag.length>
// @returns ElementTag(Number)
// @group element checking
// @description
// Returns the length of the element.
// -->
registerTag("length", (attribute, object) -> {
return new ElementTag(object.element.length());
});
// <--[tag]
// @attribute <ElementTag.not>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns the opposite of the element
// IE, true returns false and false returns true.
// -->
registerTag("not", (attribute, object) -> {
return new ElementTag(!object.element.equalsIgnoreCase("true"));
});
// <--[tag]
// @attribute <ElementTag.and[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether both the element and the second element are true.
// -->
registerTag("and", (attribute, object) -> {
return new ElementTag(object.element.equalsIgnoreCase("true") && attribute.getContext(1).equalsIgnoreCase("true"));
});
// <--[tag]
// @attribute <ElementTag.or[<element>]>
// @returns ElementTag(Boolean)
// @group element checking
// @description
// Returns whether either the element or the second element are true.
// -->
registerTag("or", (attribute, object) -> {
return new ElementTag(object.element.equalsIgnoreCase("true") || attribute.getContext(1).equalsIgnoreCase("true"));
});