-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
CuboidTag.java
1610 lines (1497 loc) · 63.8 KB
/
CuboidTag.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.denizen.objects;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
public class CuboidTag implements ObjectTag, Cloneable, Notable, Adjustable, AreaContainmentObject, FlaggableObject {
// <--[ObjectType]
// @name CuboidTag
// @prefix cu
// @base ElementTag
// @implements FlaggableObject, AreaObject
// @ExampleTagBase cuboid[my_noted_cuboid]
// @ExampleValues my_cuboid_note
// @format
// The identity format for cuboids is <world>,<x1>,<y1>,<z1>,<x2>,<y2>,<z2>
// Multi-member cuboids can simply continue listing x,y,z pairs.
// For example, 'cu@space,1,2,3,4,5,6'.
//
// @description
// A CuboidTag represents a cuboidal region in the world.
//
// The word 'cuboid' means a less strict cube.
// Basically: a "cuboid" is to a 3D "cube" what a "rectangle" is to a 2D "square".
//
// One 'cuboid' consists of two points: the low point and a high point.
// a CuboidTag can contain as many cuboids within itself as needed (this allows forming more complex shapes from a single CuboidTag).
//
// Note that the coordinates used are inclusive, meaning that a CuboidTag always includes the blocks identified as the low and high corner points.
// This means for example that a cuboid from "5,5,5" to "5,5,5" will contain one full block, and have a size of "1,1,1".
//
// This object type can be noted.
//
// This object type is flaggable when it is noted.
// Flags on this object type will be stored in the notables.yml file.
//
// -->
@Override
public CuboidTag clone() {
CuboidTag cuboid;
try {
cuboid = (CuboidTag) super.clone();
}
catch (CloneNotSupportedException ex) { // Should never happen.
Debug.echoError(ex);
cuboid = new CuboidTag();
}
cuboid.noteName = null;
cuboid.flagTracker = null;
cuboid.pairs = new ArrayList<>(pairs.size());
for (LocationPair pair : pairs) {
cuboid.pairs.add(new LocationPair(pair.low.clone(), pair.high.clone()));
}
return cuboid;
}
/////////////////////
// STATIC METHODS
/////////////////
public static List<CuboidTag> getNotableCuboidsContaining(Location location) {
List<CuboidTag> cuboids = new ArrayList<>();
for (CuboidTag cuboid : NoteManager.getAllType(CuboidTag.class)) {
if (cuboid.isInsideCuboid(location)) {
cuboids.add(cuboid);
}
}
return cuboids;
}
//////////////////
// OBJECT FETCHER
////////////////
@Deprecated
public static CuboidTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("cu")
public static CuboidTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (CoreUtilities.toLowerCase(string).startsWith("cu@")) {
string = string.substring("cu@".length());
}
Notable noted = NoteManager.getSavedObject(string);
if (noted instanceof CuboidTag) {
return (CuboidTag) noted;
}
if (CoreUtilities.contains(string, '@')) {
if (CoreUtilities.contains(string, '|') && string.contains("l@")) {
Debug.echoError("Warning: likely improperly constructed CuboidTag '" + string + "' - use to_cuboid");
}
else {
return null;
}
}
if (CoreUtilities.contains(string, '|')) {
ListTag positions = ListTag.valueOf(string, context);
if (positions.size() > 1
&& LocationTag.matches(positions.get(0))
&& LocationTag.matches(positions.get(1))) {
if (positions.size() % 2 != 0) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf CuboidTag returning null (Uneven number of locations): '" + string + "'.");
}
return null;
}
CuboidTag toReturn = new CuboidTag();
for (int i = 0; i < positions.size(); i += 2) {
LocationTag pos_1 = LocationTag.valueOf(positions.get(i), context);
LocationTag pos_2 = LocationTag.valueOf(positions.get(i + 1), context);
if (pos_1 == null || pos_2 == null) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf in CuboidTag returning null (null locations): '" + string + "'.");
}
return null;
}
if (pos_1.getWorldName() == null || pos_2.getWorldName() == null) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf in CuboidTag returning null (null worlds): '" + string + "'.");
}
return null;
}
toReturn.addPair(pos_1, pos_2);
}
if (toReturn.pairs.size() > 0) {
return toReturn;
}
}
}
else if (CoreUtilities.contains(string, ',')) {
List<String> subStrs = CoreUtilities.split(string, ',');
if (subStrs.size() < 7 || (subStrs.size() - 1) % 6 != 0) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf CuboidTag returning null (Improper number of commas): '" + string + "'.");
}
return null;
}
CuboidTag toReturn = new CuboidTag();
String worldName = subStrs.get(0);
if (worldName.startsWith("w@")) {
worldName = worldName.substring("w@".length());
}
try {
for (int i = 0; i < subStrs.size() - 1; i += 6) {
LocationTag locationOne = new LocationTag(parseRoundDouble(subStrs.get(i + 1)),
parseRoundDouble(subStrs.get(i + 2)), parseRoundDouble(subStrs.get(i + 3)), worldName);
LocationTag locationTwo = new LocationTag(parseRoundDouble(subStrs.get(i + 4)),
parseRoundDouble(subStrs.get(i + 5)), parseRoundDouble(subStrs.get(i + 6)), worldName);
toReturn.addPair(locationOne, locationTwo);
}
}
catch (NumberFormatException ex) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf CuboidTag returning null (Improper number value inputs): '" + ex.getMessage() + "'.");
}
return null;
}
if (toReturn.pairs.size() > 0) {
return toReturn;
}
}
if (context == null || context.showErrors()) {
Debug.echoError("Minor: valueOf CuboidTag returning null: " + string);
}
return null;
}
public static double parseRoundDouble(String str) {
return Math.floor(Double.parseDouble(str));
}
public static boolean matches(String string) {
if (valueOf(string, CoreUtilities.noDebugContext) != null) {
return true;
}
return false;
}
@Override
public CuboidTag duplicate() {
if (noteName != null) {
return this;
}
return clone();
}
@Override
public int hashCode() {
if (noteName != null) {
return noteName.hashCode();
}
return pairs.size() + pairs.get(0).low.hashCode();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof CuboidTag)) {
return false;
}
CuboidTag cuboid2 = (CuboidTag) other;
if (cuboid2.pairs.size() != pairs.size()) {
return false;
}
if ((noteName == null) != (cuboid2.noteName == null)) {
return false;
}
if (noteName != null && !noteName.equals(cuboid2.noteName)) {
return false;
}
for (int i = 0; i < pairs.size(); i++) {
LocationPair pair1 = pairs.get(i);
LocationPair pair2 = cuboid2.pairs.get(i);
if (!pair1.low.getWorldName().equals(pair2.low.getWorldName())) {
return false;
}
if (pair1.low.distanceSquared(pair2.low) >= 0.5) {
return false;
}
if (pair1.high.distanceSquared(pair2.high) >= 0.5) {
return false;
}
}
return true;
}
@Override
public String getNoteName() {
return noteName;
}
@Override
public boolean doesContainLocation(Location loc) {
return isInsideCuboid(loc);
}
@Override
public CuboidTag getCuboidBoundary() {
CuboidTag result = new CuboidTag(getLow(0), getHigh(0));
for (int i = 1; i < pairs.size(); i++) {
result = result.including(getLow(i)).including(getHigh(i));
}
return result;
}
///////////////
// LocationPairs
/////////////
public static class LocationPair {
public LocationTag low;
public LocationTag high;
public int xDistance() {
return high.getBlockX() - low.getBlockX();
}
public int yDistance() {
return high.getBlockY() - low.getBlockY();
}
public int zDistance() {
return high.getBlockZ() - low.getBlockZ();
}
public LocationPair(LocationTag point_1, LocationTag point_2) {
regenerate(point_1, point_2);
}
public void regenerate(LocationTag point_1, LocationTag point_2) {
String world = point_1.getWorldName();
// Find the low and high locations based on the points
// specified
int x_high = (point_1.getBlockX() >= point_2.getBlockX()
? point_1.getBlockX() : point_2.getBlockX());
int x_low = (point_1.getBlockX() <= point_2.getBlockX()
? point_1.getBlockX() : point_2.getBlockX());
int y_high = (point_1.getBlockY() >= point_2.getBlockY()
? point_1.getBlockY() : point_2.getBlockY());
int y_low = (point_1.getBlockY() <= point_2.getBlockY()
? point_1.getBlockY() : point_2.getBlockY());
int z_high = (point_1.getBlockZ() >= point_2.getBlockZ()
? point_1.getBlockZ() : point_2.getBlockZ());
int z_low = (point_1.getBlockZ() <= point_2.getBlockZ()
? point_1.getBlockZ() : point_2.getBlockZ());
// Specify defining locations to the pair
low = new LocationTag(x_low, y_low, z_low, world);
high = new LocationTag(x_high, y_high, z_high, world);
}
}
///////////////////
// Constructors/Instance Methods
//////////////////
// Location Pairs (low, high) that make up the CuboidTag
public List<LocationPair> pairs = new ArrayList<>();
public String noteName = null;
public AbstractFlagTracker flagTracker = null;
/**
* Construct the cuboid without adding pairs
* ONLY use this if addPair will be called immediately after!
*/
public CuboidTag() {
}
public CuboidTag(Location point_1, Location point_2) {
addPair(new LocationTag(point_1), new LocationTag(point_2));
}
public void addPair(LocationTag point_1, LocationTag point_2) {
if (point_1.getWorldName() == null) {
Debug.echoError("Tried to make cuboid without a world!");
return;
}
if (!point_1.getWorldName().equals(point_2.getWorldName())) {
Debug.echoError("Tried to make cross-world cuboid!");
return;
}
if (pairs.size() > 0 && !point_1.getWorldName().equals(getWorld().getName())) {
Debug.echoError("Tried to make cross-world cuboid set!");
return;
}
// Make a new pair
LocationPair pair = new LocationPair(point_1, point_2);
// Add it to the Cuboid pairs list
pairs.add(pair);
}
public static boolean isBetween(double a, double b, double c) {
return b > a ? (c >= a && c < b) : (c >= b && c < a); // Cuboid's have to be compensated for weirdly
}
public boolean isInsideCuboid(Location location) {
if (location.getWorld() == null) {
return false;
}
for (LocationPair pair : pairs) {
if (!location.getWorld().getName().equals(pair.low.getWorldName())) {
continue;
}
if (!isBetween(pair.low.getBlockX(), pair.high.getBlockX() + 1, location.getBlockX())) {
continue;
}
if (!isBetween(pair.low.getBlockY(), pair.high.getBlockY() + 1, location.getBlockY())) {
continue;
}
if (isBetween(pair.low.getBlockZ(), pair.high.getBlockZ() + 1, location.getBlockZ())) {
return true;
}
}
return false;
}
@Override
public ListTag getShell() {
int max = Settings.blockTagsMaxBlocks();
int index = 0;
ListTag list = new ListTag();
for (LocationPair pair : pairs) {
LocationTag low = pair.low;
LocationTag high = pair.high;
int y_distance = pair.yDistance();
int z_distance = pair.zDistance();
int x_distance = pair.xDistance();
for (int x = 0; x <= x_distance; x++) {
for (int y = 0; y <= y_distance; y++) {
list.addObject(new LocationTag(low.getWorld(), low.getBlockX() + x, low.getBlockY() + y, low.getBlockZ()));
list.addObject(new LocationTag(low.getWorld(), low.getBlockX() + x, low.getBlockY() + y, high.getBlockZ()));
index++;
if (index > max) {
return list;
}
}
for (int z = 1; z < z_distance; z++) {
list.addObject(new LocationTag(low.getWorld(), low.getBlockX() + x, low.getBlockY(), low.getBlockZ() + z));
list.addObject(new LocationTag(low.getWorld(), low.getBlockX() + x, high.getBlockY(), low.getBlockZ() + z));
index++;
if (index > max) {
return list;
}
}
}
for (int y = 1; y < y_distance; y++) {
for (int z = 1; z < z_distance; z++) {
list.addObject(new LocationTag(low.getWorld(), low.getBlockX(), low.getBlockY() + y, low.getBlockZ() + z));
list.addObject(new LocationTag(low.getWorld(), high.getBlockX(), low.getBlockY() + y, low.getBlockZ() + z));
index++;
if (index > max) {
return list;
}
}
}
}
return list;
}
public ListTag getOutline2D(double y) {
int max = Settings.blockTagsMaxBlocks();
int index = 0;
ListTag list = new ListTag();
for (LocationPair pair : pairs) {
LocationTag loc_1 = pair.low;
LocationTag loc_2 = pair.high;
int z_distance = pair.zDistance();
int x_distance = pair.xDistance();
list.addObject(new LocationTag(loc_2.getWorld(), loc_2.getBlockX(), y, loc_2.getBlockZ()));
for (int x = loc_1.getBlockX(); x < loc_1.getBlockX() + x_distance; x++) {
list.addObject(new LocationTag(loc_1.getWorld(), x, y, loc_2.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), x, y, loc_1.getBlockZ()));
index++;
if (index > max) {
return list;
}
}
for (int z = loc_1.getBlockZ(); z < loc_1.getBlockZ() + z_distance; z++) {
list.addObject(new LocationTag(loc_1.getWorld(), loc_2.getBlockX(), y, z));
list.addObject(new LocationTag(loc_1.getWorld(), loc_1.getBlockX(), y, z));
index++;
if (index > max) {
return list;
}
}
}
return list;
}
public ListTag getOutline() {
int max = Settings.blockTagsMaxBlocks();
int index = 0;
ListTag list = new ListTag();
for (LocationPair pair : pairs) {
LocationTag loc_1 = pair.low;
LocationTag loc_2 = pair.high;
int y_distance = pair.yDistance();
int z_distance = pair.zDistance();
int x_distance = pair.xDistance();
for (int y = loc_1.getBlockY(); y < loc_1.getBlockY() + y_distance; y++) {
list.addObject(new LocationTag(loc_1.getWorld(), loc_1.getBlockX(), y, loc_1.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), loc_2.getBlockX(), y, loc_2.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), loc_1.getBlockX(), y, loc_2.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), loc_2.getBlockX(), y, loc_1.getBlockZ()));
index++;
if (index > max) {
return list;
}
}
for (int x = loc_1.getBlockX(); x < loc_1.getBlockX() + x_distance; x++) {
list.addObject(new LocationTag(loc_1.getWorld(), x, loc_1.getBlockY(), loc_1.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), x, loc_1.getBlockY(), loc_2.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), x, loc_2.getBlockY(), loc_2.getBlockZ()));
list.addObject(new LocationTag(loc_1.getWorld(), x, loc_2.getBlockY(), loc_1.getBlockZ()));
index++;
if (index > max) {
return list;
}
}
for (int z = loc_1.getBlockZ(); z < loc_1.getBlockZ() + z_distance; z++) {
list.addObject(new LocationTag(loc_1.getWorld(), loc_1.getBlockX(), loc_1.getBlockY(), z));
list.addObject(new LocationTag(loc_1.getWorld(), loc_2.getBlockX(), loc_2.getBlockY(), z));
list.addObject(new LocationTag(loc_1.getWorld(), loc_1.getBlockX(), loc_2.getBlockY(), z));
list.addObject(new LocationTag(loc_1.getWorld(), loc_2.getBlockX(), loc_1.getBlockY(), z));
index++;
if (index > max) {
return list;
}
}
list.addObject(pair.high);
}
return list;
}
@Override
public ListTag getBlocks(Predicate<Location> test) {
List<LocationTag> locs = getBlocks_internal(test);
ListTag list = new ListTag();
for (LocationTag loc : locs) {
list.addObject(loc);
}
return list;
}
public List<LocationTag> getBlocks_internal(Predicate<Location> test) {
if (test == null) {
return getBlockLocationsUnfiltered(true);
}
int yMin = getWorld().getWorld().getMinHeight(), yMax = getWorld().getWorld().getMaxHeight();
int max = Settings.blockTagsMaxBlocks();
LocationTag loc;
List<LocationTag> list = new ArrayList<>();
int index = 0;
for (LocationPair pair : pairs) {
LocationTag loc_1 = pair.low;
int y_distance = pair.yDistance();
int z_distance = pair.zDistance();
int x_distance = pair.xDistance();
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
if (loc_1.getY() + y < yMin || loc_1.getY() + y > yMax) {
continue;
}
for (int z = 0; z != z_distance + 1; z++) {
loc = new LocationTag(loc_1.clone().add(x, y, z));
if (index++ > max) {
return list;
}
if (test.test(loc)) {
list.add(loc);
}
}
}
}
}
return list;
}
public List<LocationTag> getBlockLocationsUnfiltered(boolean doMax) {
int max = doMax ? Settings.blockTagsMaxBlocks() : Integer.MAX_VALUE;
List<LocationTag> list = new ArrayList<>();
int index = 0;
for (LocationPair pair : pairs) {
LocationTag loc_1 = pair.low;
int y_distance = pair.yDistance();
int z_distance = pair.zDistance();
int x_distance = pair.xDistance();
for (int x = 0; x <= x_distance; x++) {
for (int y = 0; y <= y_distance; y++) {
for (int z = 0; z <= z_distance; z++) {
LocationTag loc = new LocationTag(loc_1.clone().add(x, y, z));
list.add(loc);
if (index++ > max) {
return list;
}
}
}
}
}
return list;
}
public Collection<Entity> getEntitiesPossiblyWithinForTag() {
WorldTag world = getWorld();
if (pairs.size() != 1) {
return world.getEntitiesForTag();
}
BoundingBox box = BoundingBox.of(getLow(0).toVector(), getHigh(0).toVector().add(new Vector(1, 1, 1)));
return world.getPossibleEntitiesForBoundary(box);
}
@Override
public WorldTag getWorld() {
if (pairs.isEmpty()) {
return null;
}
return new WorldTag(pairs.get(0).high.getWorldName());
}
public LocationTag getHigh(int index) {
if (index < 0) {
return null;
}
if (index >= pairs.size()) {
return null;
}
return pairs.get(index).high;
}
public LocationTag getLow(int index) {
if (index < 0) {
return null;
}
if (index >= pairs.size()) {
return null;
}
return pairs.get(index).low;
}
///////////////////
// Notable
///////////////////
@Override
public boolean isUnique() {
return noteName != null;
}
@Override
@Note("Cuboids")
public Object getSaveObject() {
YamlConfiguration section = new YamlConfiguration();
section.set("object", identifyFull());
section.set("flags", flagTracker.toString());
return section;
}
@Override
public void makeUnique(String id) {
CuboidTag toNote = clone();
toNote.noteName = id;
toNote.flagTracker = new SavableMapFlagTracker();
NoteManager.saveAs(toNote, id);
}
@Override
public void forget() {
NoteManager.remove(this);
noteName = null;
flagTracker = null;
}
/////////////////////
// ObjectTag
////////////////////
String prefix = "Cuboid";
@Override
public String getObjectType() {
return "cuboid";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public CuboidTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debuggable() {
if (isUnique()) {
return "<LG>cu@<Y>" + noteName + " <GR>(" + identifyFull() + ")";
}
else {
return identifyFull();
}
}
@Override
public String identify() {
if (isUnique()) {
return "cu@" + noteName;
}
else {
return identifyFull();
}
}
@Override
public String identifySimple() {
return identify();
}
public String identifyFull() {
StringBuilder sb = new StringBuilder();
sb.append("cu@").append(pairs.get(0).low.getWorldName());
for (LocationPair pair : pairs) {
sb.append(',').append(pair.low.getBlockX()).append(',').append(pair.low.getBlockY()).append(',').append(pair.low.getBlockZ())
.append(',').append(pair.high.getBlockX()).append(',').append(pair.high.getBlockY()).append(',').append(pair.high.getBlockZ());
}
return sb.toString();
}
@Override
public String toString() {
return identify();
}
@Override
public AbstractFlagTracker getFlagTracker() {
return flagTracker;
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
if (noteName != null) {
this.flagTracker = tracker;
}
}
@Override
public String getReasonNotFlaggable() {
if (noteName == null) {
return "the area is not noted - only noted areas can hold flags";
}
return "unknown reason - something went wrong";
}
@Override
public CuboidTag withWorld(WorldTag world) {
CuboidTag newCuboid = clone();
for (LocationPair pair : newCuboid.pairs) {
pair.low.setWorld(world.getWorld());
pair.high.setWorld(world.getWorld());
}
return newCuboid;
}
/////////////////////
// ObjectTag Tag Management
/////////////////////
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
AreaContainmentObject.registerTags(CuboidTag.class, tagProcessor);
// <--[tag]
// @attribute <CuboidTag.random>
// @returns LocationTag
// @description
// Returns a random block location within the cuboid.
// (Note: random selection will not be fairly weighted for multi-member cuboids).
// -->
tagProcessor.registerTag(LocationTag.class, "random", (attribute, cuboid) -> {
LocationPair pair = cuboid.pairs.get(CoreUtilities.getRandom().nextInt(cuboid.pairs.size()));
Vector range = pair.high.toVector().subtract(pair.low.toVector()).add(new Vector(1, 1, 1));
range.setX(CoreUtilities.getRandom().nextInt(range.getBlockX()));
range.setY(CoreUtilities.getRandom().nextInt(range.getBlockY()));
range.setZ(CoreUtilities.getRandom().nextInt(range.getBlockZ()));
LocationTag out = pair.low.clone();
out.add(range);
return out;
});
// <--[tag]
// @attribute <CuboidTag.members_size>
// @returns ElementTag(Number)
// @description
// Returns the number of cuboids defined in the CuboidTag.
// -->
tagProcessor.registerTag(ElementTag.class, "members_size", (attribute, cuboid) -> {
return new ElementTag(cuboid.pairs.size());
});
// <--[tag]
// @attribute <CuboidTag.outline>
// @returns ListTag(LocationTag)
// @description
// Returns each block location on the outline of the CuboidTag.
// -->
tagProcessor.registerTag(ListTag.class, "outline", (attribute, cuboid) -> {
return cuboid.getOutline();
}, "get_outline");
// <--[tag]
// @attribute <CuboidTag.outline_2d[<#.#>]>
// @returns ListTag(LocationTag)
// @description
// Returns a list of block locations along the 2D outline of this CuboidTag, at the specified Y level.
// -->
tagProcessor.registerTag(ListTag.class, "outline_2d", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("CuboidTag.outline_2d[...] tag must have an input.");
return null;
}
double y = attribute.getDoubleParam();
return cuboid.getOutline2D(y);
});
// <--[tag]
// @attribute <CuboidTag.intersects[<cuboid>]>
// @returns ElementTag(Boolean)
// @description
// Returns whether this cuboid and another intersect.
// -->
tagProcessor.registerTag(ElementTag.class, "intersects", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag CuboidTag.intersects[...] must have a value.");
return null;
}
CuboidTag cub2 = attribute.paramAsType(CuboidTag.class);
if (cub2 != null) {
boolean intersects = false;
whole_loop:
for (LocationPair pair : cuboid.pairs) {
for (LocationPair pair2 : cub2.pairs) {
if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
return new ElementTag("false");
}
if (pair2.low.getX() <= pair.high.getX()
&& pair2.low.getY() <= pair.high.getY()
&& pair2.low.getZ() <= pair.high.getZ()
&& pair2.high.getX() >= pair.low.getX()
&& pair2.high.getY() >= pair.low.getY()
&& pair2.high.getZ() >= pair.low.getZ()) {
intersects = true;
break whole_loop;
}
}
}
return new ElementTag(intersects);
}
return null;
});
// <--[tag]
// @attribute <CuboidTag.list_members>
// @returns ListTag(CuboidTag)
// @description
// Returns a list of all sub-cuboids in this CuboidTag (for cuboids that contain multiple sub-cuboids).
// -->
tagProcessor.registerTag(ListTag.class, "list_members", (attribute, cuboid) -> {
List<LocationPair> pairs = cuboid.pairs;
ListTag list = new ListTag();
for (LocationPair pair : pairs) {
list.addObject(new CuboidTag(pair.low.clone(), pair.high.clone()));
}
return list;
});
// <--[tag]
// @attribute <CuboidTag.get[<index>]>
// @returns CuboidTag
// @description
// Returns a cuboid representing the one component of this cuboid (for cuboids that contain multiple sub-cuboids).
// -->
tagProcessor.registerTag(CuboidTag.class, "get", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag CuboidTag.get[...] must have a value.");
return null;
}
else {
int member = attribute.getIntParam();
if (member < 1) {
member = 1;
}
if (member > cuboid.pairs.size()) {
member = cuboid.pairs.size();
}
LocationPair pair = cuboid.pairs.get(member - 1);
return new CuboidTag(pair.low.clone(), pair.high.clone());
}
}, "member", "get_member");
// <--[tag]
// @attribute <CuboidTag.set[<cuboid>].at[<index>]>
// @returns CuboidTag
// @mechanism CuboidTag.set_member
// @description
// Returns a modified copy of this cuboid, with the specific sub-cuboid index changed to hold the input cuboid.
// -->
tagProcessor.registerTag(CuboidTag.class, "set", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag CuboidTag.set[...] must have a value.");
return null;
}
else {
CuboidTag subCuboid = attribute.paramAsType(CuboidTag.class);
if (!attribute.startsWith("at", 2)) {
attribute.echoError("The tag CuboidTag.set[...] must be followed by an 'at'.");
return null;
}
if (!attribute.hasContext(2)) {
attribute.echoError("The tag CuboidTag.set[...].at[...] must have an 'at' value.");
return null;
}
int member = attribute.getIntContext(2);
if (member < 1) {
member = 1;
}
if (member > cuboid.pairs.size()) {
member = cuboid.pairs.size();
}
attribute.fulfill(1);
LocationPair pair = subCuboid.pairs.get(0);
CuboidTag cloned = cuboid.clone();
cloned.pairs.set(member - 1, new LocationPair(pair.low.clone(), pair.high.clone()));
return cloned;
}
});
// <--[tag]
// @attribute <CuboidTag.add_member[<cuboid>|...]>
// @returns CuboidTag
// @mechanism CuboidTag.add_member
// @description
// Returns a modified copy of this cuboid, with the input cuboid(s) added at the end.
// -->
tagProcessor.registerTag(CuboidTag.class, "add_member", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag CuboidTag.add_member[...] must have a value.");
return null;
}
cuboid = cuboid.clone();
int member = cuboid.pairs.size() + 1;
ObjectTag param = attribute.getParamObject();
// <--[tag]
// @attribute <CuboidTag.add_member[<cuboid>|...].at[<index>]>
// @returns CuboidTag
// @mechanism CuboidTag.add_member
// @description
// Returns a modified copy of this cuboid, with the input cuboid(s) added at the specified index.
// -->
if (attribute.startsWith("at", 2)) {
if (!attribute.hasContext(2)) {
attribute.echoError("The tag CuboidTag.add_member[...].at[...] must have an 'at' value.");
return null;
}
member = attribute.getIntContext(2);
attribute.fulfill(1);
}
if (member < 1) {
member = 1;
}
if (member > cuboid.pairs.size() + 1) {
member = cuboid.pairs.size() + 1;
}
if (!(param instanceof CuboidTag) && param.toString().startsWith("li@")) { // Old cuboid identity used '|' symbol, so require 'li@' to be a list
for (CuboidTag subCuboid : param.asType(ListTag.class, attribute.context).filter(CuboidTag.class, attribute.context)) {
LocationPair pair = subCuboid.pairs.get(0);
cuboid.pairs.add(member - 1, new LocationPair(pair.low.clone(), pair.high.clone()));
member++;
}
}
else {
CuboidTag subCuboid = param.asType(CuboidTag.class, attribute.context);
LocationPair pair = subCuboid.pairs.get(0);
cuboid.pairs.add(member - 1, new LocationPair(pair.low.clone(), pair.high.clone()));
}
return cuboid;
});
// <--[tag]
// @attribute <CuboidTag.remove_member[<#>]>
// @returns CuboidTag
// @mechanism CuboidTag.remove_member
// @description
// Returns a modified copy of this cuboid, with member at the input index removed.
// -->
tagProcessor.registerTag(CuboidTag.class, "remove_member", (attribute, cuboid) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag CuboidTag.remove_member[...] must have a value.");
return null;
}
cuboid = cuboid.clone();
int member = attribute.getIntParam();
if (member < 1) {
member = 1;
}
if (member > cuboid.pairs.size() + 1) {
member = cuboid.pairs.size() + 1;
}
cuboid.pairs.remove(member - 1);
return cuboid;
});
// <--[tag]
// @attribute <CuboidTag.center>
// @returns LocationTag
// @description
// Returns the location of the exact center of the cuboid.
// Not valid for multi-member CuboidTags.
// -->
tagProcessor.registerTag(LocationTag.class, "center", (attribute, cuboid) -> {
LocationPair pair;
if (!attribute.hasParam()) {
pair = cuboid.pairs.get(0);
}