-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
dLocation.java
2539 lines (2332 loc) · 99.6 KB
/
dLocation.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.NMSVersion;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.nms.interfaces.EntityHelper;
import net.aufdemrand.denizen.nms.util.PlayerProfile;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.scripts.commands.world.SwitchCommand;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.MaterialCompat;
import net.aufdemrand.denizen.utilities.PathFinder;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizencore.objects.notable.Note;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.block.banner.PatternType;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.MaterialData;
import org.bukkit.util.BlockIterator;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dLocation extends org.bukkit.Location implements dObject, Notable, Adjustable {
// This pattern correctly reads both 0.9 and 0.8 notables
final static Pattern notablePattern =
Pattern.compile("(\\w+)[;,]((-?\\d+\\.?\\d*,){3,5}.+)",
Pattern.CASE_INSENSITIVE);
/////////////////////
// STATIC METHODS
/////////////////
public void makeUnique(String id) {
NotableManager.saveAs(this, id);
}
@Note("Locations")
public String getSaveObject() {
return (getBlockX() + 0.5)
+ "," + getBlockY()
+ "," + (getBlockZ() + 0.5)
+ "," + getPitch()
+ "," + getYaw()
+ "," + getWorld().getName();
}
public static String getSaved(dLocation location) {
for (dLocation saved : NotableManager.getAllType(dLocation.class)) {
if (saved.getBlockX() != location.getX()) {
continue;
}
if (saved.getBlockY() != location.getY()) {
continue;
}
if (saved.getBlockZ() != location.getZ()) {
continue;
}
if ((saved.getWorld() == null && location.getWorld() == null)
|| (saved.getWorld() != null && location.getWorld() != null && saved.getWorld().getName().equals(location.getWorld().getName()))) {
return NotableManager.getSavedId(saved);
}
}
return null;
}
public void forget() {
NotableManager.remove(this);
}
/*
* Called on server startup or /denizen reload locations. Should probably not be called manually.
*/
public static void _recallLocations() {
List<String> loclist = DenizenAPI.getCurrentInstance().getSaves().getStringList("dScript.Locations");
for (String location : loclist) {
Matcher m = notablePattern.matcher(location);
if (m.matches()) {
String id = m.group(1);
dLocation loc = valueOf(m.group(2));
NotableManager.saveAs(loc, id);
}
}
DenizenAPI.getCurrentInstance().getSaves().set("dScript.Locations", null);
}
//////////////////
// OBJECT FETCHER
////////////////
public static dLocation valueOf(String string) {
return valueOf(string, null);
}
/**
* 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("l")
public static dLocation valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (string.startsWith("l@")) {
string = string.substring(2);
}
if (NotableManager.isSaved(string) && NotableManager.isType(string, dLocation.class)) {
return (dLocation) NotableManager.getSavedObject(string);
}
////////
// Match location formats
// Split values
List<String> split = CoreUtilities.split(string, ',');
if (split.size() == 2)
// If 4 values, world-less 2D location format
// x,y
{
try {
return new dLocation(null,
Double.valueOf(split.get(0)),
Double.valueOf(split.get(1)));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.log("Minor: valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
else if (split.size() == 3)
// If 3 values, either worldless location format
// x,y,z or 2D location format x,y,world
{
try {
World world = Bukkit.getWorld(split.get(2));
if (world != null) {
return new dLocation(world,
Double.valueOf(split.get(0)),
Double.valueOf(split.get(1)));
}
return new dLocation(null,
Double.valueOf(split.get(0)),
Double.valueOf(split.get(1)),
Double.valueOf(split.get(2)));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.log("Minor: valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
else if (split.size() == 4)
// If 4 values, standard dScript location format
// x,y,z,world
{
try {
return new dLocation(Bukkit.getWorld(split.get(3)),
Double.valueOf(split.get(0)),
Double.valueOf(split.get(1)),
Double.valueOf(split.get(2)));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.log("Minor: valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
else if (split.size() == 6)
// If 6 values, location with pitch/yaw
// x,y,z,yaw,pitch,world
{
try {
return new dLocation(Bukkit.getWorld(split.get(5)),
Double.valueOf(split.get(0)),
Double.valueOf(split.get(1)),
Double.valueOf(split.get(2)),
Float.valueOf(split.get(3)),
Float.valueOf(split.get(4)));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.log("Minor: valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
if (context == null || context.debug) {
dB.log("Minor: valueOf dLocation returning null: " + string);
}
return null;
}
public static boolean matches(String string) {
if (string == null || string.length() == 0) {
return false;
}
if (string.startsWith("l@")) {
return true;
}
return dLocation.valueOf(string, new BukkitTagContext(null, null, false, null, false, null)) != null;
}
/////////////////////
// CONSTRUCTORS
//////////////////
private boolean is2D = false;
/**
* Turns a Bukkit Location into a Location, which has some helpful methods
* for working with dScript.
*
* @param location the Bukkit Location to reference
*/
public dLocation(Location location) {
// Just save the yaw and pitch as they are; don't check if they are
// higher than 0, because Minecraft yaws are weird and can have
// negative values
super(location.getWorld(), location.getX(), location.getY(), location.getZ(),
location.getYaw(), location.getPitch());
}
public dLocation(Vector vector) {
super(null, vector.getX(), vector.getY(), vector.getZ());
}
public dLocation(World world, double x, double y) {
this(world, x, y, 0);
this.is2D = true;
}
/**
* Turns a world and coordinates into a Location, which has some helpful methods
* for working with dScript.
*
* @param world the world in which the location resides
* @param x x-coordinate of the location
* @param y y-coordinate of the location
* @param z z-coordinate of the location
*/
public dLocation(World world, double x, double y, double z) {
super(world, x, y, z);
}
public dLocation(World world, double x, double y, double z, float yaw, float pitch) {
super(world, x, y, z, pitch, yaw);
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
// A boolean that determines whether this location will identify
// as a notable or not
private boolean raw = false;
private void setRaw(boolean state) {
this.raw = state;
}
@Override
public void setPitch(float pitch) {
super.setPitch(pitch);
}
// TODO: Why does this and the above exist?
@Override
public void setYaw(float yaw) {
super.setYaw(yaw);
}
public boolean hasInventory() {
return getBlock().getState() instanceof InventoryHolder;
}
public Inventory getBukkitInventory() {
return hasInventory() ? ((InventoryHolder) getBlock().getState()).getInventory() : null;
}
public dInventory getInventory() {
return hasInventory() ? dInventory.mirrorBukkitInventory(getBukkitInventory()) : null;
}
public BlockFace getSkullBlockFace(int rotation) {
switch (rotation) {
case 0:
return BlockFace.NORTH;
case 1:
return BlockFace.NORTH_NORTH_EAST;
case 2:
return BlockFace.NORTH_EAST;
case 3:
return BlockFace.EAST_NORTH_EAST;
case 4:
return BlockFace.EAST;
case 5:
return BlockFace.EAST_SOUTH_EAST;
case 6:
return BlockFace.SOUTH_EAST;
case 7:
return BlockFace.SOUTH_SOUTH_EAST;
case 8:
return BlockFace.SOUTH;
case 9:
return BlockFace.SOUTH_SOUTH_WEST;
case 10:
return BlockFace.SOUTH_WEST;
case 11:
return BlockFace.WEST_SOUTH_WEST;
case 12:
return BlockFace.WEST;
case 13:
return BlockFace.WEST_NORTH_WEST;
case 14:
return BlockFace.NORTH_WEST;
case 15:
return BlockFace.NORTH_NORTH_WEST;
default:
return null;
}
}
public byte getSkullRotation(BlockFace face) {
switch (face) {
case NORTH:
return 0;
case NORTH_NORTH_EAST:
return 1;
case NORTH_EAST:
return 2;
case EAST_NORTH_EAST:
return 3;
case EAST:
return 4;
case EAST_SOUTH_EAST:
return 5;
case SOUTH_EAST:
return 6;
case SOUTH_SOUTH_EAST:
return 7;
case SOUTH:
return 8;
case SOUTH_SOUTH_WEST:
return 9;
case SOUTH_WEST:
return 10;
case WEST_SOUTH_WEST:
return 11;
case WEST:
return 12;
case WEST_NORTH_WEST:
return 13;
case NORTH_WEST:
return 14;
case NORTH_NORTH_WEST:
return 15;
}
return -1;
}
public int compare(Location loc1, Location loc2) {
if (loc1 == null || loc2 == null || loc1.equals(loc2)) {
return 0;
}
else {
double dist = distanceSquared(loc1) - distanceSquared(loc2);
return dist == 0 ? 0 : (dist > 0 ? 1 : -1);
}
}
@Override
public int hashCode() {
return (int) (Math.floor(getX()) + Math.floor(getY()) + Math.floor(getZ()));
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof dLocation)) {
return false;
}
dLocation other = (dLocation) o;
if ((other.getWorld() == null && getWorld() != null)
|| (getWorld() == null && other.getWorld() != null)
|| (getWorld() != null && other.getWorld() != null
&& !getWorld().getName().equalsIgnoreCase(other.getWorld().getName()))) {
return false;
}
return Math.floor(getX()) == Math.floor(other.getX())
&& Math.floor(getY()) == Math.floor(other.getY())
&& Math.floor(getZ()) == Math.floor(other.getZ());
}
String prefix = "Location";
@Override
public String getObjectType() {
return "Location";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dLocation setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (isUnique() ? "<G>" + prefix + "='<A>" + identify() + "(<Y>" + identifyRaw() + "<A>)<G>' "
: "<G>" + prefix + "='<Y>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return getSaved(this) != null;
}
@Override
public String identify() {
if (!raw && isUnique()) {
return "l@" + getSaved(this);
}
else {
return identifyRaw();
}
}
@Override
public String identifySimple() {
if (isUnique()) {
return "l@" + getSaved(this);
}
else if (getWorld() == null) {
return "l@" + getBlockX() + "," + getBlockY() + (!is2D ? "," + getBlockZ() : "");
}
else {
return "l@" + getBlockX() + "," + getBlockY() + (!is2D ? "," + getBlockZ() : "")
+ "," + getWorld().getName();
}
}
public String identifyRaw() {
if (getYaw() != 0.0 || getPitch() != 0.0) {
return "l@" + getX() + "," + getY() + "," + getZ() + "," + getPitch() + "," + getYaw()
+ (getWorld() != null ? "," + getWorld().getName() : "");
}
else {
return "l@" + getX() + "," + getY() + (!is2D ? "," + getZ() : "")
+ (getWorld() != null ? "," + getWorld().getName() : "");
}
}
@Override
public String toString() {
return identify();
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
/////////////////////
// BLOCK ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <l@location.block_facing>
// @returns dLocation
// @description
// Returns the relative location vector of where this block is facing.
// Only works for block types that have directionality (such as signs, chests, stairs, etc.).
// This can return for example "1,0,0" to mean the block is facing towards the positive X axis.
// You can use <some_block_location.add[<some_block_location.block_facing>]> to get the block directly in front of this block (based on its facing direction).
// -->
if (attribute.matches("block_facing")) {
if (getBlock().getBlockData() instanceof Directional) {
Vector facing = ((Directional) getBlock().getBlockData()).getFacing().getDirection();
return new dLocation(getWorld(), facing.getX(), facing.getY(), facing.getZ())
.getAttribute(attribute.fulfill(1));
}
else if (getBlock().getBlockData() instanceof Rotatable) {
Vector facing = ((Rotatable) getBlock().getBlockData()).getRotation().getDirection();
return new dLocation(getWorld(), facing.getX(), facing.getY(), facing.getZ())
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.above>
// @returns dLocation
// @description
// Returns the location one block above this location.
// -->
if (attribute.startsWith("above")) {
return new dLocation(this.clone().add(0, 1, 0))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.below>
// @returns dLocation
// @description
// Returns the location one block below this location.
// -->
if (attribute.startsWith("below")) {
return new dLocation(this.clone().add(0, -1, 0))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.block>
// @returns dLocation
// @description
// Returns the location of the block this location is on,
// i.e. returns a location without decimals or direction.
// -->
if (attribute.matches("block")) {
return new dLocation(getWorld(), getBlockX(), getBlockY(), getBlockZ())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.center>
// @returns dLocation
// @description
// Returns the location at the center of the block this location is on.
// -->
if (attribute.startsWith("center")) {
return new dLocation(getWorld(), getBlockX() + 0.5, getBlockY() + 0.5, getBlockZ() + 0.5)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.highest>
// @returns dLocation
// @description
// Returns the location of the highest solid block at the location.
// -->
if (attribute.startsWith("highest")) {
return new dLocation(getWorld().getHighestBlockAt(this).getLocation().add(0, -1, 0))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.base_color>
// @returns Element
// @description
// Returns the base color of the banner at this location.
// For the list of possible colors, see <@link url http://bit.ly/1dydq12>.
// -->
if (attribute.startsWith("base_color")) {
DyeColor color = ((Banner) getBlock().getState()).getBaseColor();
return new Element(color != null ? color.name() : "BLACK").getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.has_inventory>
// @returns Element(Boolean)
// @description
// Returns whether the block at the location has an inventory.
// -->
if (attribute.startsWith("has_inventory")) {
return new Element(getBlock().getState() instanceof InventoryHolder).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.inventory>
// @returns dInventory
// @description
// Returns the dInventory of the block at the location. If the
// block is not a container, returns null.
// -->
if (attribute.startsWith("inventory")) {
dObject obj = Element.handleNull(identify() + ".inventory", getInventory(), "dInventory", attribute.hasAlternative());
return obj == null ? null : obj.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.material>
// @returns dMaterial
// @description
// Returns the material of the block at the location.
// -->
if (attribute.startsWith("material")) {
return dMaterial.getMaterialFrom(getBlock().getType(), getBlock().getData()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.patterns>
// @returns dList
// @group properties
// @mechanism dLocation.patterns
// @description
// Lists the patterns of the banner at this location in the form "li@COLOR/PATTERN|COLOR/PATTERN" etc.
// Available colors: black, blue, brown, cyan, gray, green, light_blue, lime, magenta, orange, pink
// purple, red, silver, white, and yellow.
// Available patterns: base, border, bricks, circle_middle, creeper, cross, curly_border, diagonal_left,
// diagonal_left_mirror, diagonal_right, diagonal_right_mirror, flower, gradient, gradient_up, half_horizontal,
// half_horizontal_mirror, half_vertical, half_vertical_mirror, mojang, rhombus_middle, skull,
// square_bottom_left, square_bottom_right, square_top_left, square_top_right, straight_cross, stripe_bottom,
// stripe_center, stripe_downleft, stripe_downright, stripe_left, stripe_middle, stripe_right, stripe_small,
// stripe_top, triangle_bottom, triangle_top, triangles_bottom, and triangles_top
// -->
if (attribute.startsWith("patterns")) {
dList list = new dList();
for (org.bukkit.block.banner.Pattern pattern : ((Banner) getBlock().getState()).getPatterns()) {
list.add(pattern.getColor().name() + "/" + pattern.getPattern().name());
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.head_rotation>
// @returns Element(Number)
// @description
// Gets the rotation of the head at this location. Can be 1-16.
// @mechanism dLocation.head_rotation
// -->
if (attribute.startsWith("head_rotation")) {
return new Element(getSkullRotation(((Skull) getBlock().getState()).getRotation()) + 1)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.switched>
// @returns Element(Boolean)
// @description
// Returns whether the block at the location is considered to be switched on.
// (For buttons, levers, etc.)
// To change this, see <@link command Switch>
// -->
if (attribute.startsWith("switched")) {
return new Element(SwitchCommand.switchState(getBlock()))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.sign_contents>
// @returns dList
// @mechanism dLocation.sign_contents
// @description
// Returns a list of lines on a sign.
// -->
if (attribute.startsWith("sign_contents")) {
if (getBlock().getState() instanceof Sign) {
return new dList(Arrays.asList(((Sign) getBlock().getState()).getLines()))
.getAttribute(attribute.fulfill(1));
}
else {
return null;
}
}
// <--[tag]
// @attribute <l@location.spawner_type>
// @mechanism dLocation.spawner_type
// @returns dEntity
// @description
// Returns the type of entity spawned by a mob spawner.
// -->
if (attribute.startsWith("spawner_type")) {
if (getBlock().getState() instanceof CreatureSpawner) {
return new dEntity(DenizenEntityType.getByName(((CreatureSpawner) getBlock().getState())
.getSpawnedType().name())).getAttribute(attribute.fulfill(1));
}
else {
return null;
}
}
// <--[tag]
// @attribute <l@location.lock>
// @mechanism dLocation.lock
// @returns Element
// @description
// Returns the password to a locked container.
// -->
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_10_R1)
&& attribute.startsWith("lock") && getBlock().getState() instanceof Lockable) {
Lockable lock = (Lockable) getBlock().getState();
return new Element(lock.isLocked() ? lock.getLock() : null)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.is_locked>
// @mechanism dLocation.lock
// @returns Element(Boolean)
// @description
// Returns whether the container is locked.
// -->
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_10_R1)
&& attribute.startsWith("is_locked") && getBlock().getState() instanceof Lockable) {
return new Element(((Lockable) getBlock().getState()).isLocked())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.is_lockable>
// @mechanism dLocation.lock
// @returns Element(Boolean)
// @description
// Returns whether the container is lockable.
// -->
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_10_R1)
&& attribute.startsWith("is_lockable")) {
return new Element(getBlock().getState() instanceof Lockable)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.drops[(<item>)]>
// @returns dList(dItem)
// @description
// Returns what items the block at the location would drop if broken naturally.
// Optionally specifier a breaker item.
// -->
if (attribute.startsWith("drops")) {
Collection<ItemStack> its;
if (attribute.hasContext(1)) {
dItem item = dItem.valueOf(attribute.getContext(1));
its = getBlock().getDrops(item.getItemStack());
}
else {
its = getBlock().getDrops();
}
dList list = new dList();
for (ItemStack it : its) {
list.add(new dItem(it).identify());
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.flowerpot_contents>
// @returns Element
// @description
// Returns the flower pot contents at the location.
// NOTE: Replaced by materials (such as POTTED_CACTUS) in 1.13 and above.
// -->
if (attribute.startsWith("flowerpot_contents")) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
dB.echoError("As of Minecraft version 1.13 potted flowers each have their own material, such as POTTED_CACTUS.");
}
else if (getBlock().getType() == Material.FLOWER_POT) {
MaterialData contents = NMSHandler.getInstance().getBlockHelper().getFlowerpotContents(getBlock());
return dMaterial.getMaterialFrom(contents.getItemType(), contents.getData())
.getAttribute(attribute.fulfill(1));
}
return null;
}
// <--[tag]
// @attribute <l@location.skull_type>
// @returns Element
// @description
// Returns the type of the skull.
// -->
if (attribute.startsWith("skull_type")) {
BlockState blockState = getBlock().getState();
if (blockState instanceof Skull) {
String t = ((Skull) blockState).getSkullType().name();
return new Element(t).getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.skull_name>
// @returns Element
// @mechanism dLocation.skull_skin
// @description
// Returns the name of the skin the skull is displaying.
// -->
if (attribute.startsWith("skull_name")) {
BlockState blockState = getBlock().getState();
if (blockState instanceof Skull) {
PlayerProfile profile = NMSHandler.getInstance().getBlockHelper().getPlayerProfile((Skull) blockState);
if (profile == null) {
return null;
}
String n = profile.getName();
if (n == null) {
n = ((Skull) blockState).getOwningPlayer().getName();
}
return new Element(n).getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.skull_skin>
// @returns Element
// @mechanism dLocation.skull_skin
// @description
// Returns the skin the skull is displaying - just the name or UUID as text, not a player object.
// -->
if (attribute.startsWith("skull_skin")) {
BlockState blockState = getBlock().getState();
if (blockState instanceof Skull) {
PlayerProfile profile = NMSHandler.getInstance().getBlockHelper().getPlayerProfile((Skull) blockState);
if (profile == null) {
return null;
}
String name = profile.getName();
UUID uuid = profile.getUniqueId();
String texture = profile.getTexture();
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <l@location.skull_skin.full>
// @returns Element|Element
// @mechanism dLocation.skull_skin
// @description
// Returns the skin the skull item is displaying - just the name or UUID as text, not a player object,
// along with the permanently cached texture property.
// -->
if (attribute.startsWith("full")) {
return new Element((uuid != null ? uuid : name != null ? name : null)
+ (texture != null ? "|" + texture : ""))
.getAttribute(attribute.fulfill(1));
}
return new Element(uuid != null ? uuid.toString() : name != null ? name : null).getAttribute(attribute);
}
else {
return null;
}
}
// <--[tag]
// @attribute <l@location.simple.formatted>
// @returns Element
// @description
// Returns the formatted simple version of the dLocation's block coordinates.
// In the format: X 'x', Y 'y', Z 'z', in world 'world'
// For example, X '1', Y '2', Z '3', in world 'world_nether'
// -->
if (attribute.startsWith("simple.formatted")) {
return new Element("X '" + getBlockX()
+ "', Y '" + getBlockY()
+ "', Z '" + getBlockZ()
+ "', in world '" + getWorld().getName() + "'").getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <l@location.simple>
// @returns Element
// @description
// Returns a simple version of the dLocation's block coordinates.
// In the format: x,y,z,world
// For example: 1,2,3,world_nether
// -->
if (attribute.startsWith("simple")) {
if (getWorld() == null) {
return new Element(getBlockX() + "," + getBlockY() + "," + getBlockZ())
.getAttribute(attribute.fulfill(1));
}
else {
return new Element(getBlockX() + "," + getBlockY() + "," + getBlockZ()
+ "," + getWorld().getName()).getAttribute(attribute.fulfill(1));
}
}
/////////////////////
// DIRECTION ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <l@location.precise_impact_normal[<range>]>
// @returns dLocation
// @description
// Returns the exact impact normal at the location this location is pointing at.
// Optionally, specify a maximum range to find the location from.
// -->
if (attribute.startsWith("precise_impact_normal")) {
int range = attribute.getIntContext(1);
if (range < 1) {
range = 200;
}
double xzLen = Math.cos((getPitch() % 360) * (Math.PI / 180));
double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
double ny = Math.sin(getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
Location location = NMSHandler.getInstance().getEntityHelper().getImpactNormal(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
if (location != null) {
return new dLocation(location).getAttribute(attribute.fulfill(1));
}
else {
return null;
}
}
// <--[tag]
// @attribute <l@location.precise_cursor_on[<range>]>
// @returns dLocation
// @description
// Returns the exact location this location is pointing at.
// Optionally, specify a maximum range to find the location from.
// -->
if (attribute.startsWith("precise_cursor_on")) {
int range = attribute.getIntContext(1);
if (range < 1) {
range = 200;
}
double xzLen = Math.cos((getPitch() % 360) * (Math.PI / 180));
double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
double ny = Math.sin(getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
Location location = NMSHandler.getInstance().getEntityHelper().rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
if (location != null) {
return new dLocation(location).getAttribute(attribute.fulfill(1));
}
else {
return null;
}
}
// <--[tag]
// @attribute <l@location.points_between[<location>]>
// @returns dList(dLocation)
// @description
// Finds all locations between this location and another, separated by 1 block-width each.
// -->
if (attribute.startsWith("points_between")) {
dLocation target = dLocation.valueOf(attribute.getContext(1));
if (target == null) {
return null;
}
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <l@location.points_between[<location>].distance[<#.#>]>
// @returns dList(dLocation)
// @description
// Finds all locations between this location and another, separated by the specified distance each.
// -->
double rad = 1d;
if (attribute.startsWith("distance")) {
rad = attribute.getDoubleContext(1);
attribute = attribute.fulfill(1);
}
dList list = new dList();
org.bukkit.util.Vector rel = target.toVector().subtract(this.toVector());
double len = rel.length();
rel = rel.multiply(1d / len);
for (double i = 0d; i < len; i += rad) {