-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ListTag.java
1843 lines (1694 loc) · 71.1 KB
/
ListTag.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.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.containers.core.ProcedureScriptContainer;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.NaturalOrderComparator;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;
public class ListTag extends ArrayList<String> implements ObjectTag {
// <--[language]
// @name ListTag Objects
// @group Object System
// @description
// A ListTag is a list of any data. It can hold any number of objects in any order.
// The objects can be of any basic Denizen object type, including another list
// (escaping sub-lists is strongly recommended).
//
// For format info, see <@link language li@>
//
// -->
// <--[language]
// @name li@
// @group Object Fetcher System
// @description
// li@ refers to the 'object identifier' of a ListTag. The 'li@' is notation for Denizen's Object
// Fetcher. The constructor for a ListTag is the content items of a list separated by a pipe '|' symbol.
// For example, if I had a list of 'taco', 'potatoes', and 'cheese', I would use
// 'li@taco|potatoes|cheese'
//
// A list with zero items in it is simply 'li@'
// and a list with one item is just the one item and no pipes.
//
// For general info, see <@link language ListTag Objects>
// -->
public final ArrayList<ObjectTag> objectForms;
@Override
public boolean add(String addMe) {
objectForms.add(new ElementTag(addMe));
return super.add(addMe);
}
@Override
public void add(int index, String addMe) {
objectForms.add(index, new ElementTag(addMe));
super.add(index, addMe);
}
@Override
public boolean addAll(Collection<? extends String> addMe) {
for (String str : addMe) {
add(str);
}
return !addMe.isEmpty();
}
@Override
public String remove(int index) {
objectForms.remove(index);
return super.remove(index);
}
@Override
public boolean remove(Object key) {
int ind = super.indexOf(key);
if (ind < 0 || ind >= size()) {
return false;
}
this.remove(ind);
return true;
}
public boolean addAll(ListTag inp) {
objectForms.addAll(inp.objectForms);
return super.addAll(inp);
}
public boolean addObject(ObjectTag obj) {
objectForms.add(obj);
return super.add(obj.toString());
}
public void addObject(int index, ObjectTag obj) {
objectForms.add(index, obj);
super.add(index, obj.toString());
}
public void setObject(int index, ObjectTag obj) {
objectForms.set(index, obj);
super.set(index, obj.toString());
}
public ObjectTag getObject(int id) {
return objectForms.get(id);
}
public static ListTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("li")
public static ListTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
///////
// Match @object format
ListTag list = DenizenCore.getImplementation().valueOfFlagListTag(string);
if (list != null) {
return list;
}
// Use value of string, which will separate values by the use of a pipe '|'
return new ListTag(string.startsWith("li@") ? string.substring(3) : string, context);
}
public static ListTag getListFor(ObjectTag inp, TagContext context) {
return inp instanceof ListTag ? (ListTag) inp : valueOf(inp.toString(), context);
}
public static boolean matches(String arg) {
boolean flag = DenizenCore.getImplementation().matchesFlagListTag(arg);
return flag || arg.contains("|") || arg.startsWith("li@");
}
/////////////
// Constructors
//////////
public ListTag(Collection<? extends ObjectTag> objectTagList) {
objectForms = new ArrayList<>(objectTagList);
for (ObjectTag obj : objectTagList) {
super.add(obj.identify());
}
}
public ListTag(ObjectTag... objects) {
this(Arrays.asList(objects));
}
public ListTag() {
objectForms = new ArrayList<>();
}
public ListTag(String items) {
this(items, null);
}
public ListTag(String items, TagContext context) {
if (items != null && items.length() > 0) {
// Count brackets
int brackets = 0;
// Record start position
int start = 0;
// Loop through characters
for (int i = 0; i < items.length(); i++) {
char chr = items.charAt(i);
// Count brackets
if (chr == '[') {
brackets++;
}
else if (chr == ']') {
if (brackets > 0) {
brackets--;
}
}
// Separate if an un-bracketed pipe is found
else if (brackets == 0 && chr == '|') {
super.add(items.substring(start, i));
start = i + 1;
}
}
// If there is an item waiting, add it too
if (start < items.length()) {
super.add(items.substring(start));
}
}
objectForms = new ArrayList<>(size());
for (String str : this) {
objectForms.add(ObjectFetcher.pickObjectFor(str, context));
}
}
public ListTag(String flag, boolean is_flag, List<String> flag_contents) {
if (is_flag) {
this.flag = flag;
}
for (String it : flag_contents) {
super.add(it);
}
objectForms = new ArrayList<>(size());
for (String str : this) {
objectForms.add(new ElementTag(str));
}
}
public ListTag(ListTag input) {
objectForms = new ArrayList<>(input.objectForms);
super.ensureCapacity(input.size());
for (String str : input) {
super.add(str);
}
}
// A List<String> of items
public ListTag(List<String> items) {
if (items != null) {
for (String it : items) {
super.add(it);
}
}
objectForms = new ArrayList<>(size());
for (String str : this) {
objectForms.add(new ElementTag(str));
}
}
// A Set<Object> of items
public ListTag(Set<?> items) {
objectForms = new ArrayList<>(items.size());
if (items != null) {
for (Object o : items) {
String strd = o.toString();
super.add(strd);
if (o instanceof ObjectTag) {
objectForms.add((ObjectTag) o);
}
else {
objectForms.add(new ElementTag(strd));
}
}
}
}
// A List<String> of items, with a prefix
public ListTag(List<String> items, String prefix) {
for (String element : items) {
super.add(prefix + element);
}
objectForms = new ArrayList<>(size());
for (String str : this) {
objectForms.add(new ElementTag(str));
}
}
/////////////
// Instance Fields/Methods
//////////
public ListTag addObjects(List<ObjectTag> ObjectTags) {
for (ObjectTag obj : ObjectTags) {
addObject(obj);
}
return this;
}
// Returns if the list contains objects from the specified dClass
// by using the matches() method.
public boolean containsObjectsFrom(Class<? extends ObjectTag> dClass) {
// Iterate through elements until one matches() the dClass
for (ObjectTag testable : objectForms) {
if (CoreUtilities.canPossiblyBeType(testable, dClass)) {
return true;
}
}
return false;
}
public List<String> filter(Enum[] values) {
List<String> list = new ArrayList<>(values.length);
for (String string : this) {
for (Enum value : values) {
if (value.name().equalsIgnoreCase(string)) {
list.add(string);
}
}
}
if (!list.isEmpty()) {
return list;
}
return null;
}
public <T extends ObjectTag> List<T> filter(Class<T> dClass, ScriptEntry entry) {
return filter(dClass, (entry == null ? CoreUtilities.basicContext : entry.entryData.getTagContext()), true);
}
public <T extends ObjectTag> List<T> filter(Class<T> dClass, Debuggable debugger, boolean showFailure) {
TagContext context = DenizenCore.getImplementation().getTagContext(null);
context.debug = debugger.shouldDebug();
return filter(dClass, context, showFailure);
}
public <T extends ObjectTag> List<T> filter(Class<T> dClass, TagContext context) {
return filter(dClass, context, context == null || context.debug);
}
public <T extends ObjectTag> List<T> filter(Class<T> dClass, TagContext context, boolean showFailure) {
List<T> results = new ArrayList<>(objectForms.size());
for (ObjectTag obj : objectForms) {
try {
if (CoreUtilities.canPossiblyBeType(obj, dClass)) {
T object = CoreUtilities.asType(obj, dClass, context);
if (object != null) {
results.add(object);
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + dClass.getSimpleName() + "' (conversion returned null).");
}
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + dClass.getSimpleName() + "' (does not match expected type).");
}
}
catch (Exception e) {
Debug.echoError(e);
}
}
return results;
}
public ListTag deduplicate() {
ListTag list = new ListTag();
int size = size();
for (int i = 0; i < size; i++) {
String entry = get(i);
boolean duplicate = false;
for (int x = 0; x < i; x++) {
if (get(x).equalsIgnoreCase(entry)) {
duplicate = true;
break;
}
}
if (!duplicate) {
list.addObject(objectForms.get(i));
}
}
return list;
}
@Override
public String toString() {
return identify();
}
//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////
private String prefix = "List";
@Override
public String getPrefix() {
return prefix;
}
@Override
public ListTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debuggable() {
if (isEmpty()) {
return "li@";
}
StringBuilder debugText = new StringBuilder();
debugText.append("<G>li@<Y> ");
for (ObjectTag item : objectForms) {
debugText.append(item.debuggable()).append(" <G>|<Y> ");
}
return debugText.substring(0, debugText.length() - " <G>|<Y> ".length());
}
public String flag = null;
@Override
public boolean isUnique() {
return flag != null;
}
@Override
public String getObjectType() {
return "List";
}
@Override
public String identify() {
if (flag != null && size() == 1) {
return get(0);
}
return identifyList();
}
public String identifyList() {
if (isEmpty()) {
return "li@";
}
StringBuilder output = new StringBuilder();
output.append("li@");
for (String item : this) {
output.append(item).append('|');
}
return output.substring(0, output.length() - 1);
}
@Override
public String identifySimple() {
return identify();
}
private static String parseString(ListTag obj, String spacer) {
StringBuilder dScriptArg = new StringBuilder();
for (String item : obj) {
dScriptArg.append(item);
dScriptArg.append(spacer);
}
return dScriptArg.toString().substring(0,
dScriptArg.length() - spacer.length());
}
public static void registerTags() {
// <--[tag]
// @attribute <ListTag.combine>
// @returns ListTag
// @description
// returns a list containing the contents of all sublists within this list.
// -->
registerTag("combine", (attribute, object) -> {
ListTag output = new ListTag();
for (ObjectTag obj : object.objectForms) {
output.addObjects(ListTag.getListFor(obj, attribute.context).objectForms);
}
return output;
});
// <--[tag]
// @attribute <ListTag.space_separated>
// @returns ElementTag
// @description
// returns the list in a cleaner format, separated by spaces.
// For example: a list of "one|two|three" will return "one two three".
// -->
registerTag("space_separated", (attribute, object) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
return new ElementTag(parseString(object, " "));
}, "as_string", "asstring");
// <--[tag]
// @attribute <ListTag.separated_by[<text>]>
// @returns ElementTag
// @description
// returns the list formatted, with each item separated by the defined text.
// For example: <list[bob|joe|john].separated_by[ and ]> will return "bob and joe and john".
// -->
registerTag("separated_by", (attribute, object) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
String input = attribute.getContext(1);
return new ElementTag(parseString(object, input));
});
// <--[tag]
// @attribute <ListTag.comma_separated>
// @returns ElementTag
// @description
// returns the list in a cleaner format, separated by commas.
// For example: a list of "one|two|three" will return "one, two, three".
// -->
registerTag("comma_separated", (attribute, object) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
return new ElementTag(parseString(object, ", "));
}, "ascslist", "as_cslist");
// <--[tag]
// @attribute <ListTag.unseparated>
// @returns ElementTag
// @description
// returns the list in a less clean format, separated by nothing.
// For example: a list of "one|two|three" will return "onetwothree".
// -->
registerTag("unseparated", (attribute, object) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
return new ElementTag(parseString(object, ""));
});
// <--[tag]
// @attribute <ListTag.get_sub_items[<#>]>
// @returns ListTag
// @description
// returns a list of the specified sub items in the list, as split by the
// forward-slash character (/).
// For example: .get_sub_items[1] on a list of "one/alpha|two/beta" will return "one|two".
// -->
registerTag("get_sub_items", (attribute, object) -> {
int index = -1;
if (ArgumentHelper.matchesInteger(attribute.getContext(1))) {
index = attribute.getIntContext(1) - 1;
}
// <--[tag]
// @attribute <ListTag.get_sub_items[<#>].split_by[<element>]>
// @returns ListTag
// @description
// returns a list of the specified sub item in the list, allowing you to specify a
// character in which to split the sub items by. WARNING: When setting your own split
// character, make note that it is CASE SENSITIVE.
// For example: .get_sub_items[1].split_by[-] on a list of "one-alpha|two-beta" will return "one|two".
// -->
String split = "/";
if (attribute.startsWith("split_by", 2)) {
if (attribute.hasContext(2) && attribute.getContext(2).length() > 0) {
split = attribute.getContext(2);
}
attribute.fulfill(1);
}
if (index < 0) {
return null;
}
ListTag sub_list = new ListTag();
for (String item : object) {
String[] strings = item.split(Pattern.quote(split));
if (strings.length > index) {
sub_list.add(strings[index]);
}
else {
sub_list.add("null");
}
}
return sub_list;
});
// <--[tag]
// @attribute <ListTag.map_get[<element>|...]>
// @returns ListTag or ElementTag
// @description
// Interprets a list of "key/value" pairs as a map, and returns the value for the given key.
// For example: one/a|two/b.map_get[one] returns a.
// Optionally, specify a list of keys to get a list back. If any listed keys are not present, will give null.
// -->
registerTag("map_get", (attribute, object) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
ListTag input = getListFor(attribute.getContextObject(1), attribute.context);
// <--[tag]
// @attribute <ListTag.map_get[<element>|...].split_by[<element>]>
// @returns ListTag or ElementTag
// @description
// Interprets a list of "key" + "value" pairs split by the input symbol as a map, and returns the value for the given key.
// For example: one/a|two/b.map_get[one].split_by[/] returns a.
// Optionally, specify a list of keys to get a list back. If any listed keys are not present, will give null.
// -->
String split = "/";
if (attribute.startsWith("split_by", 2)) {
if (attribute.hasContext(2) && attribute.getContext(2).length() > 0) {
split = attribute.getContext(2);
}
attribute.fulfill(1);
}
ListTag result = new ListTag();
for (String key : input) {
for (String item : object) {
String[] strings = item.split(Pattern.quote(split), 2);
if (strings.length > 1 && strings[0].equalsIgnoreCase(key)) {
result.add(strings[1]);
}
}
}
if (input.size() == 1 && result.size() == 1) {
return new ElementTag(result.get(0));
}
else if (input.size() > 1) {
if (result.size() != input.size()) {
return null;
}
return result;
}
return null;
});
// <--[tag]
// @attribute <ListTag.map_find_key[<element>]>
// @returns ElementTag
// @description
// Interprets a list of "key/value" pairs as a map, and returns the key for the given value.
// For example: one/a|two/b.map_find_key[a] returns one.
// -->
registerTag("map_find_key", (attribute, object) -> {
String input = attribute.getContext(1);
// <--[tag]
// @attribute <ListTag.map_find_key[<element>].split_by[<element>]>
// @returns ElementTag
// @description
// Interprets a list of "key" + "value" pairs split by the input symbol as a map, and returns the value for the given key.
// For example: one/a|two/b.map_find_key[a].split_by[/] returns one.
// -->
String split = "/";
if (attribute.startsWith("split_by", 2)) {
if (attribute.hasContext(2) && attribute.getContext(2).length() > 0) {
split = attribute.getContext(2);
}
attribute.fulfill(1);
}
for (String item : object) {
String[] strings = item.split(Pattern.quote(split), 2);
if (strings.length > 1 && strings[1].equalsIgnoreCase(input)) {
return new ElementTag(strings[0]);
}
}
return null;
});
// <--[tag]
// @attribute <ListTag.size>
// @returns ElementTag(Number)
// @description
// returns the size of the list.
// For example: a list of "one|two|three" will return "3".
// -->
registerTag("size", (attribute, object) -> {
return new ElementTag(object.size());
});
// <--[tag]
// @attribute <ListTag.is_empty>
// @returns ElementTag(Boolean)
// @description
// returns whether the list is empty.
// For example: a list of "" returns true, while "one" returns false.
// -->
registerTag("is_empty", (attribute, object) -> {
return new ElementTag(object.isEmpty());
});
// <--[tag]
// @attribute <ListTag.insert[...|...].at[<#>]>
// @returns ListTag
// @description
// returns a new ListTag with the items specified inserted to the specified location.
// For example: .insert[two|three].at[2] on a list of "one|four" will return "one|two|three|four".
// -->
registerTag("insert", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.insert[...] must have a value.");
return null;
}
ListTag items = getListFor(attribute.getContextObject(1), attribute.context);
if (attribute.startsWith("at", 2) && attribute.hasContext(2)) {
ListTag result = new ListTag(object);
int index = attribute.getIntContext(2) - 1;
if (index < 0) {
index = 0;
}
if (index > result.size()) {
index = result.size();
}
for (int i = 0; i < items.size(); i++) {
result.addObject(index + i, items.getObject(i));
}
attribute.fulfill(1);
return result;
}
else {
Debug.echoError("The tag ListTag.insert[...] must be followed by .at[#]!");
return null;
}
});
// <--[tag]
// @attribute <ListTag.set[...|...].at[<#>]>
// @returns ListTag
// @description
// returns a new ListTag with the items specified inserted to the specified location, replacing the element
// already at that location.
// For example: .set[potato].at[2] on a list of "one|two|three" will return "one|potato|three".
// -->
registerTag("set", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.set[...] must have a value.");
return null;
}
if (object.isEmpty()) {
return null;
}
ListTag items = getListFor(attribute.getContextObject(1), attribute.context);
if (attribute.startsWith("at", 2) && attribute.hasContext(2)) {
ListTag result = new ListTag(object);
int index = attribute.getIntContext(2) - 1;
if (index < 0) {
index = 0;
}
if (index > result.size() - 1) {
index = result.size() - 1;
}
attribute.fulfill(1);
result.remove(index);
for (int i = 0; i < items.size(); i++) {
result.addObject(index + i, items.objectForms.get(i));
}
return result;
}
else {
Debug.echoError("The tag ListTag.set[...] must be followed by .at[#]!");
return null;
}
});
// <--[tag]
// @attribute <ListTag.include[...|...]>
// @returns ListTag
// @description
// returns a new ListTag including the items specified.
// For example: .include[three|four] on a list of "one|two" will return "one|two|three|four".
// -->
registerTag("include", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.include[...] must have a value.");
return null;
}
ListTag copy = new ListTag(object);
copy.addAll(getListFor(attribute.getContextObject(1), attribute.context));
return copy;
});
// <--[tag]
// @attribute <ListTag.exclude[...|...]>
// @returns ListTag
// @description
// returns a new ListTag excluding the items specified.
// For example: .exclude[two|four] on a list of "one|two|three|four" will return "one|three".
// -->
registerTag("exclude", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.exclude[...] must have a value.");
return null;
}
ListTag exclusions = getListFor(attribute.getContextObject(1), attribute.context);
// Create a new ListTag that will contain the exclusions
ListTag copy = new ListTag(object);
// Iterate through
for (String exclusion : exclusions) {
for (int i = 0; i < copy.size(); i++) {
if (copy.get(i).equalsIgnoreCase(exclusion)) {
copy.remove(i--);
}
}
}
// Return the modified list
return copy;
});
// <--[tag]
// @attribute <ListTag.remove[<#>|...]>
// @returns ListTag
// @description
// returns a new ListTag excluding the items at the specified index.
// For example: .remove[2] on a list of "one|two|three|four" will return "one|three|four".
// Also supports [first] and [last] values.
// -->
registerTag("remove", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.remove[#] must have a value.");
return null;
}
ListTag indices = getListFor(attribute.getContextObject(1), attribute.context);
ListTag copy = new ListTag(object);
for (String index : indices) {
int remove;
if (index.equalsIgnoreCase("last")) {
remove = copy.size() - 1;
}
else if (index.equalsIgnoreCase("first")) {
remove = 0;
}
else {
remove = new ElementTag(index).asInt() - 1;
}
if (remove >= 0 && remove < copy.size()) {
copy.set(remove, "\0");
}
}
for (int i = 0; i < copy.size(); i++) {
if (copy.get(i).equals("\0")) {
copy.remove(i--);
}
}
return copy;
});
// <--[tag]
// @attribute <ListTag.replace[(regex:)<element>]>
// @returns ElementTag
// @description
// Returns the list with all instances of an element removed.
// Specify regex: at the start of the replace element to replace elements that match the Regex.
// -->
// <--[tag]
// @attribute <ListTag.replace[(regex:)<element>].with[<element>]>
// @returns ListTag
// @description
// Returns the list with all instances of an element replaced with another.
// Specify regex: at the start of the replace element to replace elements that match the Regex.
// -->
registerTag("replace", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.replace[...] must have a value.");
return null;
}
String replace = attribute.getContext(1);
ObjectTag replacement = null;
if (attribute.startsWith("with", 2)) {
if (attribute.hasContext(2)) {
replacement = attribute.getContextObject(2);
attribute.fulfill(1);
}
}
ListTag list = new ListTag();
if (replace.startsWith("regex:")) {
String regex = replace.substring("regex:".length());
Pattern tempPat = Pattern.compile(regex);
for (int i = 0; i < object.size(); i++) {
if (tempPat.matcher(object.get(i)).matches()) {
if (replacement != null) {
list.addObject(replacement);
}
}
else {
list.addObject(object.getObject(i));
}
}
}
else {
String lower = CoreUtilities.toLowerCase(replace);
for (int i = 0; i < object.size(); i++) {
if (CoreUtilities.toLowerCase(object.get(i)).equals(lower)) {
if (replacement != null) {
list.addObject(replacement);
}
}
else {
list.addObject(object.getObject(i));
}
}
}
return list;
});
// <--[tag]
// @attribute <ListTag.reverse>
// @returns ListTag
// @description
// returns a copy of the list, with all items placed in opposite order.
// For example: a list of "one|two|three" will become "three|two|one".
// -->
registerTag("reverse", (attribute, object) -> {
ArrayList<ObjectTag> objs = new ArrayList<>(object.objectForms);
Collections.reverse(objs);
return new ListTag(objs);
});
// <--[tag]
// @attribute <ListTag.deduplicate>
// @returns ListTag
// @description
// returns a copy of the list with any duplicate items removed.
// For example: a list of "one|one|two|three" will become "one|two|three".
// -->
registerTag("deduplicate", (attribute, object) -> {
return object.deduplicate();
});
// <--[tag]
// @attribute <ListTag.get[<#>|...]>
// @returns ObjectTag
// @description
// returns an element of the value specified by the supplied context.
// For example: .get[1] on a list of "one|two" will return "one", and .get[2] will return "two"
// Specify more than one index to get a list of results.
// -->
TagRunnable.ObjectInterface<ListTag> getRunnable = (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.get[...] must have a value.");
return null;
}
if (object.isEmpty()) {
if (!attribute.hasAlternative()) {
Debug.echoError("Can't get from an empty list.");
}
return null;
}
ListTag indices = getListFor(attribute.getContextObject(1), attribute.context);
if (indices.size() > 1) {
ListTag results = new ListTag();
for (String index : indices) {
int ind = Integer.parseInt(index);
if (ind > 0 && ind <= object.size()) {
results.add(object.get(ind - 1));
}
}
return results;
}
if (indices.size() > 0) {
int index = Integer.parseInt(indices.get(0)) - 1;
if (index >= object.size()) {
if (!attribute.hasAlternative()) {
Debug.echoError("Invalid list.get index.");
}
return null;
}
if (index < 0) {
index = 0;
}
// <--[tag]
// @attribute <ListTag.get[<#>].to[<#>]>
// @returns ListTag
// @description
// returns all elements in the range from the first index to the second.
// For example: .get[1].to[3] on a list of "one|two|three|four" will return "one|two|three"
// -->
if (attribute.startsWith("to", 2) && attribute.hasContext(2)) {
int index2 = attribute.getIntContext(2) - 1;
if (index2 >= object.size()) {
index2 = object.size() - 1;
}
if (index2 < 0) {
index2 = 0;
}
ListTag newList = new ListTag();
for (int i = index; i <= index2; i++) {
newList.addObject(object.objectForms.get(i));
}
attribute.fulfill(1);
return newList;
}
else {
return object.objectForms.get(index);
}
}
return null;
};
registerTag("get", getRunnable);
registerTag("", getRunnable);
// <--[tag]
// @attribute <ListTag.find_all_partial[<element>]>
// @returns ListTag(ElementTag(Number))
// @description