-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ListTag.java
2790 lines (2605 loc) · 111 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.DenizenCore;
import com.denizenscript.denizencore.exceptions.TagProcessingException;
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.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.containers.core.ProcedureScriptContainer;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.tags.*;
import com.denizenscript.denizencore.tags.core.EscapeTagUtil;
import com.denizenscript.denizencore.utilities.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ListTag implements List<String>, ObjectTag {
// NOTE: Explicitly no example value
// <--[ObjectType]
// @name ListTag
// @prefix li
// @base ElementTag
// @synonyms ArrayTag
// @ExampleTagBase list[one|two]
// @ExampleForReturns
// - foreach %VALUE% as:entry:
// - narrate "found <[entry]>"
// @format
// The identity format for ListTags is each item, one after the other, in order, separated by a pipe '|' symbol.
// For example, for a list of 'taco', 'potatoes', and 'cheese', it would be '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 a pipe on the end.
//
// If the pipe symbol "|" appears in a list entry, it will be replaced by "&pipe",
// similarly if an ampersand "&" appears in a list entry, it will be replaced by "&".
// This is a subset of Denizen standard escaping, see <@link language Escaping 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 Denizen object type, including another list.
//
// List indices start at 1 (so, the tag 'get[1]' gets the very first entry)
// and extend to however many entries the list has (so, if a list has 15 entries, the tag 'get[15]' gets the very last entry).
//
// Inputs that accept list indices will generally accept 'first' to mean '1', 'last' to mean the last entry in the list,
// or negative numbers to automatically select an index starting at the end - so for example 'get[-1]' gets the last entry, 'get[-2]' gets the second-to-last, etc.
//
// -->
public static AsciiMatcher needsEscpingMatcher = new AsciiMatcher("&|");
public static String escapeEntry(String value) {
if (!needsEscpingMatcher.containsAnyMatch(value)) {
return value;
}
return value.replace("&", "&").replace("|", "&pipe");
}
public static String unescapeEntry(String value) {
if (value.indexOf('&') == -1) {
return value;
}
return value.replace("&pipe", "|").replace("&", "&");
}
public final ArrayList<ObjectTag> objectForms;
@Override
public boolean add(String addMe) {
return objectForms.add(new ElementTag(addMe));
}
@Override
public void add(int index, String addMe) {
objectForms.add(index, new ElementTag(addMe));
}
@Override
public boolean addAll(Collection<? extends String> addMe) {
for (String str : addMe) {
add(str);
}
return !addMe.isEmpty();
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
for (String str : c) {
add(index++, str);
}
return true;
}
@Override
public boolean removeAll(Collection<?> c) {
boolean allGone = true;
for (Object obj : c) {
if (!remove(obj)) {
allGone = false;
}
}
return allGone;
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
objectForms.clear();
}
@Override
public boolean contains(Object obj) {
return indexOf(obj) != -1;
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object obj : c) {
if (!contains(obj)) {
return false;
}
}
return true;
}
@Override
public Iterator<String> iterator() {
return listIterator(0);
}
@Override
public ListIterator<String> listIterator() {
return listIterator(0);
}
@Override
public ListIterator<String> listIterator(final int index) {
return new ListTagStringIterator(this, index);
}
public static class ListTagStringIterator implements ListIterator<String> {
public ListTag list;
int index;
public ListTagStringIterator(ListTag list, int index) {
this.list = list;
this.index = index;
}
@Override
public boolean hasNext() {
return index < list.size();
}
@Override
public String next() {
return list.get(index++);
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public String previous() {
return list.get(--index);
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
@Override
public void remove() {
list.remove(index--);
}
@Override
public void set(String s) {
list.set(index, s);
}
@Override
public void add(String s) {
list.add(index++, s);
}
}
@Override
public Object[] toArray() {
Object[] stringArr = new Object[size()];
for (int i = 0; i < stringArr.length; i++) {
stringArr[i] = String.valueOf(getObject(i));
}
return stringArr;
}
@Override
public <T> T[] toArray(T[] a) {
String[] stringArr = (a instanceof String[] && a.length >= size()) ? (String[]) a : new String[size()];
for (int i = 0; i < size(); i++) {
stringArr[i] = String.valueOf(getObject(i));
}
for (int i = size(); i < stringArr.length; i++) {
stringArr[i] = null;
}
return (T[]) stringArr;
}
@Override
public ListTag subList(int fromIndex, int toIndex) {
return new ListTag(objectForms.subList(fromIndex, toIndex));
}
@Override
public int indexOf(Object obj) {
int size = size();
if (obj == null) {
for (int i = 0; i < size; i++) {
if (getObject(i) == null) {
return i;
}
}
}
else if (obj instanceof String) {
for (int i = 0; i < size; i++) {
if (obj.equals(String.valueOf(getObject(i)))) {
return i;
}
}
}
else {
for (int i = 0; i < size; i++) {
if (obj.equals(getObject(i))) {
return i;
}
}
}
return -1;
}
@Override
public int lastIndexOf(Object obj) {
int size = size() - 1;
if (obj == null) {
for (int i = size; i >= 0; i--) {
if (getObject(i) == null) {
return i;
}
}
}
else if (obj instanceof String) {
for (int i = size; i >= 0; i--) {
if (obj.equals(String.valueOf(getObject(i)))) {
return i;
}
}
}
else {
for (int i = size; i >= 0; i--) {
if (obj.equals(getObject(i))) {
return i;
}
}
}
return -1;
}
public ObjectTag removeObject(int index) {
return objectForms.remove(index);
}
@Override
public String remove(int index) {
return String.valueOf(removeObject(index));
}
@Override
public boolean remove(Object key) {
int ind = indexOf(key);
if (ind < 0 || ind >= size()) {
return false;
}
this.remove(ind);
return true;
}
@Override
public int size() {
return objectForms.size();
}
@Override
public boolean isEmpty() {
return objectForms.isEmpty();
}
@Override
public boolean isTruthy() {
return !isEmpty();
}
@Override
public String get(int index) {
return String.valueOf(objectForms.get(index));
}
@Override
public String set(int index, String value) {
return String.valueOf(setObject(index, new ElementTag(value)));
}
public boolean addAll(ListTag inp) {
return objectForms.addAll(inp.objectForms);
}
public boolean addObject(ObjectTag obj) {
return objectForms.add(obj);
}
public void addObject(int index, ObjectTag obj) {
objectForms.add(index, obj);
}
public ObjectTag setObject(int index, ObjectTag obj) {
return objectForms.set(index, obj);
}
public ObjectTag getObject(int id) {
ObjectTag result = objectForms.get(id);
if (result == null) {
return null;
}
return result.refreshState();
}
@Fetchable("li")
public static ListTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
ListTag list;
if (string.startsWith("map@")) {
MapTag map = MapTag.valueOf(string, context);
list = new ListTag();
if (map == null) {
list.add(string);
}
else {
list.addObject(map);
}
return list;
}
return new ListTag(string.startsWith("li@") ? string.substring("li@".length()) : string, context);
}
public static ListTag getListFor(ObjectTag inp, TagContext context) {
if (inp instanceof ListTag) {
return (ListTag) inp;
}
if (inp instanceof ElementTag) {
return valueOf(inp.toString(), context);
}
ListTag output = new ListTag(1);
output.addObject(inp);
return output;
}
public static boolean matches(String arg) {
return true;
}
@Override
public ListTag duplicate() {
ListTag outList = new ListTag(size());
for (ObjectTag obj : objectForms) {
outList.addObject(obj == null ? null : obj.duplicate());
}
return outList;
}
/////////////
// Constructors
//////////
public ListTag(Collection<? extends ObjectTag> objectTagList) {
objectForms = new ArrayList<>(objectTagList);
}
public ListTag(ObjectTag... objects) {
this(Arrays.asList(objects));
}
public ListTag(int capacity) {
objectForms = new ArrayList<>(capacity);
}
public ListTag() {
objectForms = new ArrayList<>();
}
public ListTag(String items) {
this(items, null);
}
public boolean wasLegacy = false;
public ListTag(String items, TagContext context) {
objectForms = new ArrayList<>();
if (items != null && items.length() > 0) {
if (!items.contains("|")) {
addObject(ObjectFetcher.pickObjectFor(items, context));
}
else if (items.endsWith("|")) {
int pipe = items.indexOf('|');
int lastPipe = 0;
while (pipe != -1) {
String value = unescapeEntry(items.substring(lastPipe, pipe));
ObjectTag object = ObjectFetcher.pickObjectFor(value, context);
addObject(object);
lastPipe = pipe + 1;
pipe = items.indexOf('|', lastPipe);
}
}
else {
wasLegacy = true;
// 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 == '|') {
addObject(ObjectFetcher.pickObjectFor(items.substring(start, i), context));
start = i + 1;
}
}
// If there is an item waiting, add it too
if (start < items.length()) {
addObject(ObjectFetcher.pickObjectFor(items.substring(start), context));
}
}
}
}
public ListTag(ListTag input) {
objectForms = new ArrayList<>(input.objectForms);
}
public ListTag(Collection<String> items, boolean isPlainText) {
objectForms = new ArrayList<>(items.size());
for (String str : items) {
objectForms.add(new ElementTag(str, isPlainText));
}
}
public ListTag(List<String> items) {
objectForms = new ArrayList<>(items.size());
for (String str : items) {
objectForms.add(new ElementTag(str));
}
}
// A Set<Object> of items
public ListTag(Set<?> items) {
objectForms = new ArrayList<>(items.size());
for (Object o : items) {
objectForms.add(o instanceof ObjectTag objectTag ? objectTag : new ElementTag(o.toString()));
}
}
public ListTag(Stream<String> items) {
objectForms = items.map(ElementTag::new).collect(Collectors.toCollection(ArrayList::new));
}
public <T> ListTag(Collection<T> objects, Function<T, ObjectTag> convertor) {
this(objects, null, convertor);
}
public <T> ListTag(Collection<T> objects, Predicate<T> filter, Function<T, ObjectTag> convertor) {
this(objects.size());
for (T object : objects) {
if (filter == null || filter.test(object)) {
addObject(convertor.apply(object));
}
}
}
/////////////
// Instance Fields/Methods
//////////
public ListTag addObjects(List<ObjectTag> objectTags) {
objectForms.addAll(objectTags);
return this;
}
public boolean containsObjectsFrom(Class<? extends ObjectTag> dClass) {
for (ObjectTag testable : objectForms) {
if (testable.canBeType(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 (CoreUtilities.equalsIgnoreCase(value.name(), 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.getContext(), true);
}
public <T extends ObjectTag> List<T> filter(Class<T> dClass, Debuggable debugger, boolean showFailure) {
TagContext context = DenizenCore.implementation.getTagContext((ScriptEntry) 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 (obj.canBeType(dClass)) {
T object = obj.asType(dClass, context);
if (object != null) {
results.add(object);
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + DebugInternals.getClassNameOpti(dClass) + "' (conversion returned null).");
}
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + DebugInternals.getClassNameOpti(dClass) + "' (does not match expected type).");
}
}
catch (Exception e) {
Debug.echoError(e);
}
}
return results;
}
private static HashSet<String> deduplicateHelper = new HashSet<>();
public ListTag deduplicate() {
deduplicateHelper.clear();
int size = size();
ListTag list = new ListTag(size);
for (int i = 0; i < size; i++) {
ObjectTag obj = objectForms.get(i);
String entry = CoreUtilities.toLowerCase(String.valueOf(obj));
if (!deduplicateHelper.contains(entry)) {
list.addObject(obj);
deduplicateHelper.add(entry);
}
}
deduplicateHelper.clear();
return list;
}
@Override
public String toString() {
return identify();
}
@Override
public Object getJavaObject() {
ArrayList<Object> result = new ArrayList<>();
for (ObjectTag obj : objectForms) {
result.add(obj.getJavaObject());
}
return result;
}
//////////////////////////////
// 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 "<LG>li@ (Size <GR>0<LG>)";
}
StringBuilder debugText = new StringBuilder();
debugText.append("<LG>li@ (Size <GR>").append(size()).append("<LG>): <Y>");
for (ObjectTag item : objectForms) {
debugText.append(item.debuggable()).append(" <LG>|<Y> ");
}
return debugText.substring(0, debugText.length() - " <LG>|<Y> ".length());
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String identify() {
return identifyList();
}
public String identifyList() {
if (isEmpty()) {
return "li@";
}
StringBuilder output = new StringBuilder();
output.append("li@");
for (ObjectTag object : objectForms) {
output.append(escapeEntry(object.savable())).append('|');
}
return output.toString();
}
@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.substring(0, dScriptArg.length() - spacer.length());
}
public int parseIndex(String index, Attribute attribute, boolean strict) {
int size = size();
if (size == 0) {
if (strict) {
attribute.echoError("Invalid index parse, list is empty.");
return -1;
}
return 0;
}
index = CoreUtilities.toLowerCase(index);
if (index.equals("last")) {
return size - 1;
}
if (index.equals("first")) {
return 0;
}
int integerIndex;
try {
integerIndex = Integer.parseInt(index);
}
catch (NumberFormatException ex) {
attribute.echoError("Invalid index '" + index + "': not a number.");
return -1;
}
if (integerIndex < 0) {
return Math.max(0, size + integerIndex);
}
if (integerIndex == 0) {
attribute.echoError("Invalid index '0': lists start at index 1.");
return 0;
}
if (integerIndex > size) {
if (strict) {
attribute.echoError("Invalid index '" + index + "': list only has " + size + " entries in it.");
return -1;
}
else {
return size;
}
}
return integerIndex - 1;
}
public static void register() {
// <--[tag]
// @attribute <ListTag.combine>
// @returns ListTag
// @description
// Returns a list containing the contents of all sublists within this list.
// -->
tagProcessor.registerStaticTag(ListTag.class, "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.sub_lists[<#>]>
// @returns ListTag(ListTag)
// @description
// Returns a list containing sublists of this list capped to a specific length.
// @Example
// # Narrates "a|b", then "c|d", then "e|f"
// - foreach <list[a|b|c|d|e|f].sub_lists[2]> as:sublist:
// - narrate <[sublist]>
// -->
tagProcessor.registerStaticTag(ListTag.class, ElementTag.class, "sub_lists", (attribute, object, count) -> {
int subListLength = Math.max(1, count.asInt());
ListTag output = new ListTag();
ListTag building = new ListTag();
for (int i = 0; i < object.size(); i++) {
building.addObject(object.getObject(i));
if (building.size() == subListLength) {
output.addObject(building);
building = new ListTag();
}
}
if (!building.isEmpty()) {
output.addObject(building);
}
return output;
});
// <--[tag]
// @attribute <ListTag.space_separated>
// @returns ElementTag
// @description
// Returns the list in a cleaner format, separated by spaces.
// @Example
// # Narrates "one two three"
// - narrate <list[one|two|three].space_separated>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "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.
// @Example
// # Narrates "bob and joe and john"
// - narrate "<list[bob|joe|john].separated_by[ and ]>"
// -->
tagProcessor.registerStaticTag(ElementTag.class, ElementTag.class, "separated_by", (attribute, object, input) -> {
if (object.isEmpty()) {
return new ElementTag("");
}
return new ElementTag(parseString(object, input.asString()));
});
// <--[tag]
// @attribute <ListTag.comma_separated>
// @returns ElementTag
// @description
// Returns the list in a cleaner format, separated by commas.
// @Example
// # Narrates "one, two, three"
// - narrate <list[one|two|three].comma_separated>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "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.
// @Example
// # Narrates "onetwothree"
// - narrate <list[one|two|three].unseparated>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "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 (/).
// @Example
// # Narrates a list of "one|two"
// - narrate <list[one/alpha|two/beta].get_sub_items[1]>
// -->
tagProcessor.registerTag(ListTag.class, "get_sub_items", (attribute, object) -> { // non-static due to hacked sub-tag
int index = -1;
if (ArgumentHelper.matchesInteger(attribute.getParam())) {
index = attribute.getIntParam() - 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.
// @Example
// # Narrates a list of "one|two"
// - narrate <list[one-alpha|two-beta].get_sub_items[1].split_by[-]>
// -->
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;
});
tagProcessor.registerTag(ObjectTag.class, "map_get", (attribute, object) -> {
Deprecations.listOldMapTags.warn(attribute.context);
if (object.isEmpty()) {
return new ElementTag("");
}
ListTag input = getListFor(attribute.getParamObject(), attribute.context);
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;
});
tagProcessor.registerTag(ElementTag.class, "map_find_key", (attribute, object) -> {
Deprecations.listOldMapTags.warn(attribute.context);
String input = attribute.getParam();
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.merge_maps>
// @returns MapTag
// @description
// If this list is a list of MapTags, returns a single MapTag of all the maps combined together.
// So a list that contains map of [a=1;b=2] and a map of [x=3;y=4] will return a single map of [a=1;b;=2;x=3;y=4]
// Duplicate keys will have the the last value that appears in the list.
// -->
tagProcessor.registerStaticTag(MapTag.class, "merge_maps", (attribute, object) -> {
MapTag map = new MapTag();
for (ObjectTag entry : object.objectForms) {
MapTag subMap = MapTag.getMapFor(entry, attribute.context);
if (subMap == null) {
attribute.echoError("Invalid map '" + entry + "' for merge_maps tag.");
return null;
}
map.putAll(subMap);
}
return map;
});
// <--[tag]
// @attribute <ListTag.to_map[(<separator>)]>
// @returns MapTag
// @description
// Interprets a list of "key/value" pairs as a map, and returns the resulting MapTag.
// Optionally specify the map separator symbol, by default '/'.
// Inverted by <@link tag MapTag.to_list>
// -->
tagProcessor.registerStaticTag(MapTag.class, "to_map", (attribute, object) -> {
String symbol = "/";
if (attribute.hasParam()) {
symbol = attribute.getParam();
}
MapTag map = new MapTag();
for (String entry : object) {
int slash = entry.indexOf(symbol);
if (slash == -1) {
return null;
}
String key = entry.substring(0, slash);
String value = entry.substring(slash + symbol.length());
map.putObject(key, new ElementTag(value));
}