-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
dCuboid.java
1455 lines (1273 loc) · 53.3 KB
/
dCuboid.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 net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.nms.interfaces.BlockHelper;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizencore.objects.notable.Note;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.material.MaterialData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dCuboid implements dObject, Cloneable, Notable, Adjustable {
// Cloning
@Override
public dCuboid clone() throws CloneNotSupportedException {
dCuboid cuboid = (dCuboid) super.clone();
cuboid.pairs = new ArrayList<>(pairs.size());
for (LocationPair pair : pairs) {
cuboid.pairs.add(new LocationPair(pair.point_1.clone(), pair.point_2.clone()));
}
cuboid.filter = new ArrayList<>(filter);
return cuboid;
}
/////////////////////
// STATIC METHODS
/////////////////
public static List<dCuboid> getNotableCuboidsContaining(Location location) {
List<dCuboid> cuboids = new ArrayList<dCuboid>();
for (dCuboid cuboid : NotableManager.getAllType(dCuboid.class)) {
if (cuboid.isInsideCuboid(location)) {
cuboids.add(cuboid);
}
}
return cuboids;
}
//////////////////
// OBJECT FETCHER
////////////////
public static dCuboid valueOf(String string) {
return valueOf(string, null);
}
final static Pattern cuboid_by_saved = Pattern.compile("(cu@)?(.+)");
/**
* Gets a Location Object from a string form of id,x,y,z,world
* or a dScript argument (location:)x,y,z,world. If including an Id,
* this location will persist and can be recalled at any time.
*
* @param string the string or dScript argument String
* @return a Location, or null if incorrectly formatted
*/
@Fetchable("cu")
public static dCuboid valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
////////
// Match location formats
// Split values
dList positions = dList.valueOf(string.replace("cu@", ""));
// If there's a | and the first two points look like locations, assume it's a valid location-list constructor.
if (positions.size() > 1
&& dLocation.matches(positions.get(0))
&& dLocation.matches(positions.get(1))) {
if (positions.size() % 2 != 0) {
if (context == null || context.debug) {
dB.echoError("valueOf dCuboid returning null (Uneven number of locations): '" + string + "'.");
}
return null;
}
// Form a cuboid to add to
dCuboid toReturn = new dCuboid();
// Add to the cuboid
for (int i = 0; i < positions.size(); i += 2) {
dLocation pos_1 = dLocation.valueOf(positions.get(i));
dLocation pos_2 = dLocation.valueOf(positions.get(i + 1));
// Must be valid locations
if (pos_1 == null || pos_2 == null) {
if (context == null || context.debug) {
dB.echoError("valueOf in dCuboid returning null (null locations): '" + string + "'.");
}
return null;
}
// Must have valid worlds
if (pos_1.getWorld() == null || pos_2.getWorld() == null) {
if (context == null || context.debug) {
dB.echoError("valueOf in dCuboid returning null (null worlds): '" + string + "'.");
}
return null;
}
toReturn.addPair(pos_1, pos_2);
}
// Ensure validity and return the created cuboid
if (toReturn.pairs.size() > 0) {
return toReturn;
}
}
////////
// Match @object format for Notable dCuboids
Matcher m;
m = cuboid_by_saved.matcher(string);
if (m.matches() && NotableManager.isType(m.group(2), dCuboid.class)) {
return (dCuboid) NotableManager.getSavedObject(m.group(2));
}
if (context == null || context.debug) {
dB.echoError("valueOf dCuboid returning null: " + string);
}
return null;
}
// The regex below: optional-"|" + "<#.#>," x3 + <text> + "|" + "<#.#>," x3 + <text> -- repeating
final static Pattern cuboidLocations =
Pattern.compile("(\\|?([-\\d\\.]+,){3}[\\w\\s]+\\|([-\\d\\.]+,){3}[\\w\\s]+)+",
Pattern.CASE_INSENSITIVE);
public static boolean matches(String string) {
// Starts with cu@? Assume match.
if (CoreUtilities.toLowerCase(string).startsWith("cu@")) {
return true;
}
Matcher m;
// Check for named cuboid: cu@notable_cuboid
m = cuboid_by_saved.matcher(string);
if (m.matches() && NotableManager.isType(m.group(2), dCuboid.class)) {
return true;
}
// Check for standard cuboid format: cu@x,y,z,world|x,y,z,world|...
m = cuboidLocations.matcher(string.replace("cu@", ""));
return m.matches();
}
///////////////
// LocationPairs
/////////////
public static class LocationPair {
public dLocation low;
public dLocation high;
dLocation point_1;
dLocation point_2;
int x_distance;
int y_distance;
int z_distance;
public LocationPair(dLocation point_1, dLocation point_2) {
this.point_1 = point_1;
this.point_2 = point_2;
regenerate();
}
public void changePoint(int number, dLocation point) {
if (number == 1) {
this.point_1 = point;
}
else if (number == 2) {
this.point_2 = point;
}
regenerate();
}
public void regenerate() {
World world = point_1.getWorld();
// 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 dLocation(world, x_low, y_low, z_low);
high = new dLocation(world, x_high, y_high, z_high);
generateDistances();
}
public void generateDistances() {
x_distance = high.getBlockX() - low.getBlockX();
y_distance = high.getBlockY() - low.getBlockY();
z_distance = high.getBlockZ() - low.getBlockZ();
}
}
///////////////////
// Constructors/Instance Methods
//////////////////
// Location Pairs (low, high) that make up the dCuboid
public List<LocationPair> pairs = new ArrayList<>();
// Only put dMaterials in filter.
ArrayList<dObject> filter = new ArrayList<>();
/**
* Construct the cuboid without adding pairs
* ONLY use this if addPair will be called immediately after!
*/
public dCuboid() {
}
public dCuboid(Location point_1, Location point_2) {
addPair(point_1, point_2);
}
public void addPair(Location point_1, Location point_2) {
if (point_1.getWorld() != point_2.getWorld()) {
dB.echoError("Tried to make cross-world cuboid!");
return;
}
if (pairs.size() > 0 && point_1.getWorld() != getWorld()) {
dB.echoError("Tried to make cross-world cuboid set!");
return;
}
// Make a new pair
LocationPair pair = new LocationPair(new dLocation(point_1), new dLocation(point_2));
// Add it to the Cuboid pairs list
pairs.add(pair);
}
public boolean isInsideCuboid(Location location) {
for (LocationPair pair : pairs) {
if (!location.getWorld().equals(pair.low.getWorld())) {
continue;
}
if (!Utilities.isBetween(pair.low.getBlockX(), pair.high.getBlockX() + 1, location.getBlockX())) {
continue;
}
if (!Utilities.isBetween(pair.low.getBlockY(), pair.high.getBlockY() + 1, location.getBlockY())) {
continue;
}
if (Utilities.isBetween(pair.low.getBlockZ(), pair.high.getBlockZ() + 1, location.getBlockZ())) {
return true;
}
}
// Does not match any of the pairs
return false;
}
public dCuboid addBlocksToFilter(List<dMaterial> addl) {
filter.addAll(addl);
return this;
}
public dCuboid removeBlocksFromFilter(List<dMaterial> addl) {
filter.removeAll(addl);
return this;
}
public dCuboid removeFilter() {
filter.clear();
return this;
}
public dCuboid setAsFilter(List<dMaterial> list) {
filter.clear();
filter.addAll(list);
return this;
}
public dList getOutline() {
// +-----2
// /| /|
// +-----+ |
// | +---|-+
// |/ |/
// 1-----+
int max = Settings.blockTagsMaxBlocks();
int index = 0;
dList list = new dList();
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
dLocation loc_2 = pair.high;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int y = loc_1.getBlockY(); y < loc_1.getBlockY() + y_distance; y++) {
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
y,
loc_1.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
y,
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
y,
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
y,
loc_1.getBlockZ()).identify());
index++;
if (index > max) {
return list;
}
}
for (int x = loc_1.getBlockX(); x < loc_1.getBlockX() + x_distance; x++) {
list.add(new dLocation(loc_1.getWorld(),
x,
loc_1.getBlockY(),
loc_1.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_1.getBlockY(),
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_2.getBlockY(),
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_2.getBlockY(),
loc_1.getBlockZ()).identify());
index++;
if (index > max) {
return list;
}
}
for (int z = loc_1.getBlockZ(); z < loc_1.getBlockZ() + z_distance; z++) {
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
loc_1.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
loc_2.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
loc_2.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
loc_1.getBlockY(),
z).identify());
index++;
if (index > max) {
return list;
}
}
list.add(pair.high.identify());
}
return list;
}
public dList getBlocks() {
return getBlocks(null);
}
private boolean matchesMaterialList(Location loc, List<dMaterial> materials) {
if (materials == null) {
return true;
}
dMaterial mat = dMaterial.getMaterialFrom(loc.getBlock().getType(), loc.getBlock().getData());
for (dMaterial material : materials) {
if (mat.equals(material) || (mat.getMaterial() == material.getMaterial()
&& (material.getData() == null || material.getData() == 0))) { // TODO: only if null?
return true;
}
}
return false;
}
public dList getBlocks(List<dMaterial> materials) {
List<dLocation> locs = getBlocks_internal(materials);
dList list = new dList();
for (dLocation loc : locs) {
list.add(loc.identify());
}
return list;
}
public List<dLocation> getBlocks_internal(List<dMaterial> materials) {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
List<dLocation> list = new ArrayList<dLocation>();
int index = 0;
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
for (int z = 0; z != z_distance + 1; z++) {
loc = new dLocation(loc_1.clone().add(x, y, z));
if (loc.getY() < 0 || loc.getY() > 255) {
continue; // TODO: Why is this ever possible?
}
if (!filter.isEmpty()) { // TODO: Should 'filter' exist?
// Check filter
for (dObject material : filter) {
if (((dMaterial) material).matchesMaterialData(
new MaterialData(loc.getBlock().getType(), loc.getBlock().getData()))) {
if (matchesMaterialList(loc, materials)) {
list.add(loc);
}
}
}
}
else {
if (matchesMaterialList(loc, materials)) {
list.add(loc);
}
}
index++;
if (index > max) {
return list;
}
}
}
}
}
return list;
}
public void setBlocks_internal(List<BlockData> materials) {
dLocation loc;
int index = 0;
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
for (int z = 0; z != z_distance + 1; z++) {
if (loc_1.getY() + y >= 0 && loc_1.getY() + y < 256) {
materials.get(index).setBlock(loc_1.clone().add(x, y, z).getBlock(), false);
}
index++;
}
}
}
}
}
public BlockData getBlockAt(double nX, double nY, double nZ, List<BlockData> materials) {
dLocation loc;
int index = 0;
// TODO: calculate rather than cheat
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
for (int z = 0; z != z_distance + 1; z++) {
if (x == nX && nY == y && z == nZ) {
return materials.get(index);
}
index++;
}
}
}
}
return null;
}
public List<dLocation> getBlockLocations() {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
List<dLocation> list = new ArrayList<dLocation>();
int index = 0;
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int x = 0; x != x_distance + 1; x++) {
for (int z = 0; z != z_distance + 1; z++) {
for (int y = 0; y != y_distance + 1; y++) {
loc = new dLocation(loc_1.clone()
.add(x, y, z));
if (!filter.isEmpty()) {
// Check filter
for (dObject material : filter) {
if (loc.getBlock().getType().name().equalsIgnoreCase(((dMaterial) material)
.getMaterial().name())) {
list.add(loc);
}
}
}
else {
list.add(loc);
}
index++;
if (index > max) {
return list;
}
}
}
}
}
return list;
}
public dList getSpawnableBlocks() {
return getSpawnableBlocks(null);
}
/**
* Returns a dList of dLocations with 2 vertical blocks of air
* that are safe for players and similar entities to spawn in,
* but ignoring blocks in midair
*
* @return The dList
*/
public dList getSpawnableBlocks(List<dMaterial> mats) {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
dList list = new dList();
int index = 0;
BlockHelper blockHelper = NMSHandler.getInstance().getBlockHelper();
for (LocationPair pair : pairs) {
dLocation loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
for (int z = 0; z != z_distance + 1; z++) {
loc = new dLocation(loc_1.clone()
.add(x, y, z));
if (blockHelper.isSafeBlock(loc.getBlock().getType())
&& blockHelper.isSafeBlock(loc.clone().add(0, 1, 0).getBlock().getType())
&& loc.clone().add(0, -1, 0).getBlock().getType().isSolid()
&& matchesMaterialList(loc.clone().add(0, -1, 0), mats)) {
// Get the center of the block, so the entity won't suffocate
// inside the edges for a couple of seconds
loc.add(0.5, 0, 0.5);
list.add(loc.identify());
}
index++;
if (index > max) {
return list;
}
}
}
}
}
return list;
}
public World getWorld() {
if (pairs.size() == 0) {
return null;
}
return pairs.get(0).high.getWorld();
}
public dLocation getHigh(int index) {
if (index < 0) {
return null;
}
if (index >= pairs.size()) {
return null;
}
return pairs.get(index).high;
}
public dLocation 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 NotableManager.isSaved(this);
}
@Override
@Note("Cuboids")
public String getSaveObject() {
return identifyFull().substring(3);
}
@Override
public void makeUnique(String id) {
NotableManager.saveAs(this, id);
}
@Override
public void forget() {
NotableManager.remove(this);
}
/////////////////////
// dObject
////////////////////
String prefix = "Cuboid";
@Override
public String getObjectType() {
return "cuboid";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dCuboid setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (isUnique()
? "<G>" + prefix + "='<A>" + NotableManager.getSavedId(this)
+ "(<Y>" + identify() + "<A>)<G>' "
: "<G>" + prefix + "='<Y>" + identify() + "<G>' ");
}
@Override
public String identify() {
if (isUnique()) {
return "cu@" + NotableManager.getSavedId(this);
}
else {
return identifyFull();
}
}
@Override
public String identifySimple() {
return identify();
}
public String identifyFull() {
StringBuilder sb = new StringBuilder();
sb.append("cu@");
for (LocationPair pair : pairs) {
if (pair.low.getWorld() == null || pair.high.getWorld() == null) {
dB.echoError("Null world for cuboid, returning invalid identity!");
return "cu@null";
}
sb.append(pair.low.getBlockX()).append(',').append(pair.low.getBlockY())
.append(',').append(pair.low.getBlockZ()).append(',').append(pair.low.getWorld().getName())
.append('|').append(pair.high.getBlockX()).append(',').append(pair.high.getBlockY())
.append(',').append(pair.high.getBlockZ()).append(',').append(pair.high.getWorld().getName()).append('|');
}
return sb.toString().substring(0, sb.toString().length() - 1);
}
@Override
public String toString() {
return identify();
}
/////////////////////
// dObject Tag Management
/////////////////////
public static void registerTags() {
// <--[tag]
// @attribute <cu@cuboid.blocks[<material>|...]>
// @returns dList(dLocation)
// @description
// Returns each block location within the dCuboid.
// Optionally, specify a list of materials to only return locations
// with that block type.
// -->
registerTag("blocks", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (attribute.hasContext(1)) {
return new dList(((dCuboid) object).getBlocks(dList.valueOf(attribute.getContext(1)).filter(dMaterial.class, attribute.context)))
.getAttribute(attribute.fulfill(1));
}
else {
return new dList(((dCuboid) object).getBlocks())
.getAttribute(attribute.fulfill(1));
}
}
});
registerTag("get_blocks", registeredTags.get("blocks"));
// <--[tag]
// @attribute <cu@cuboid.members_size>
// @returns Element(Number)
// @description
// Returns the number of cuboids defined in the dCuboid.
// -->
registerTag("members_size", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dCuboid) object).pairs.size())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <cu@cuboid.member[<#>]>
// @returns dCuboid
// @description
// Returns a new dCuboid of a single member of this dCuboid. Just specify an index.
// -->
registerTag("member", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
int member = attribute.getIntContext(1);
if (member < 1) {
member = 1;
}
if (member > ((dCuboid) object).pairs.size()) {
member = ((dCuboid) object).pairs.size();
}
return new dCuboid(((dCuboid) object).pairs.get(member - 1).low, ((dCuboid) object).pairs.get(member - 1).high)
.getAttribute(attribute.fulfill(1));
}
});
registerTag("get_member", registeredTags.get("member"));
// <--[tag]
// @attribute <cu@cuboid.spawnable_blocks[<Material>|...]>
// @returns dList(dLocation)
// @description
// Returns each dLocation within the dCuboid that is
// safe for players or similar entities to spawn in.
// Optionally, specify a list of materials to only return locations
// with that block type.
// -->
registerTag("spawnable_blocks", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (attribute.hasContext(1)) {
return new dList(((dCuboid) object).getSpawnableBlocks(dList.valueOf(attribute.getContext(1)).filter(dMaterial.class, attribute.context)))
.getAttribute(attribute.fulfill(1));
}
else {
return new dList(((dCuboid) object).getSpawnableBlocks())
.getAttribute(attribute.fulfill(1));
}
}
});
registerTag("get_spawnable_blocks", registeredTags.get("spawnable_blocks"));
// <--[tag]
// @attribute <cu@cuboid.outline>
// @returns dList(dLocation)
// @description
// Returns each block location on the outline of the dCuboid.
// -->
registerTag("outline", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new dList(((dCuboid) object).getOutline())
.getAttribute(attribute.fulfill(1));
}
});
registerTag("get_outline", registeredTags.get("outline"));
// <--[tag]
// @attribute <cu@cuboid.filter>
// @returns dList(dLocation)
// @description
// Returns the block locations from the dCuboid's filter.
// -->
registerTag("filter", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new dList(((dCuboid) object).filter)
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <cu@cuboid.intersects[<cuboid>]>
// @returns Element(Boolean)
// @description
// Returns whether this cuboid and another intersect.
// -->
registerTag("intersects", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1)) {
dB.echoError("The tag cu@cuboid.intersects[...] must have a value.");
return null;
}
dCuboid cub2 = dCuboid.valueOf(attribute.getContext(1));
if (cub2 != null) {
boolean intersects = false;
whole_loop:
for (LocationPair pair : ((dCuboid) object).pairs) {
for (LocationPair pair2 : cub2.pairs) {
if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
return new Element("false").getAttribute(attribute.fulfill(1));
}
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 Element(intersects).getAttribute(attribute.fulfill(1));
}
return null;
}
});
// <--[tag]
// @attribute <cu@cuboid.contains_location[<location>]>
// @returns Element(Boolean)
// @description
// Returns whether this cuboid contains a location.
// -->
registerTag("contains_location", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1)) {
dB.echoError("The tag cu@cuboid.contains_location[...] must have a value.");
return null;
}
dLocation loc = dLocation.valueOf(attribute.getContext(1));
return new Element(((dCuboid) object).isInsideCuboid(loc)).getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <cu@cuboid.is_within[<cuboid>]>
// @returns Element(Boolean)
// @description
// Returns whether this cuboid is fully inside another cuboid.
// -->
registerTag("is_within", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1)) {
dB.echoError("The tag cu@cuboid.is_within[...] must have a value.");
return null;
}
dCuboid cub2 = dCuboid.valueOf(attribute.getContext(1));
if (cub2 != null) {
boolean contains = true;
for (LocationPair pair2 : ((dCuboid) object).pairs) {
boolean contained = false;
for (LocationPair pair : cub2.pairs) {
if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
if (net.aufdemrand.denizencore.utilities.debugging.dB.verbose) {
dB.log("Worlds don't match!");
}
return new Element("false").getAttribute(attribute.fulfill(1));
}
if (pair2.low.getX() >= pair.low.getX()
&& pair2.low.getY() >= pair.low.getY()
&& pair2.low.getZ() >= pair.low.getZ()
&& pair2.high.getX() <= pair.high.getX()
&& pair2.high.getY() <= pair.high.getY()
&& pair2.high.getZ() <= pair.high.getZ()) {
contained = true;
break;
}
}
if (!contained) {
contains = false;
break;
}
}
return new Element(contains).getAttribute(attribute.fulfill(1));
}
return null;
}
});
// <--[tag]