/
dLocation.java
2263 lines (2068 loc) · 86.3 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 com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.item.ItemSkullskin;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
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.denizen.utilities.entity.Rotation;
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 net.minecraft.server.v1_8_R3.TileEntitySkull;
import org.apache.commons.lang.StringUtils;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
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.material.Button;
import org.bukkit.material.Lever;
import org.bukkit.material.MaterialData;
import org.bukkit.util.BlockIterator;
import java.util.*;
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.getBlockX()) {
continue;
}
if (saved.getBlockY() != location.getBlockY()) {
continue;
}
if (saved.getBlockZ() != location.getBlockZ()) {
continue;
}
if (!saved.getWorld().getName().equals(location.getWorld().getName())) {
continue;
}
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
String[] split = StringUtils.split(string, ',');
if (split.length == 2)
// If 4 values, wordless 2D location format
// x,y
{
try {
return new dLocation(null,
Double.valueOf(split[0]),
Double.valueOf(split[1]));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.echoError("valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
if (split.length == 3)
// If 3 values, either worldless location format
// x,y,z or 2D location format x,y,world
{
try {
World world = Bukkit.getWorld(split[2]);
if (world != null) {
return new dLocation(world,
Double.valueOf(split[0]),
Double.valueOf(split[1]));
}
return new dLocation(null,
Double.valueOf(split[0]),
Double.valueOf(split[1]),
Double.valueOf(split[2]));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.echoError("valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
else if (split.length == 4)
// If 4 values, standard dScript location format
// x,y,z,world
{
try {
return new dLocation(Bukkit.getWorld(split[3]),
Double.valueOf(split[0]),
Double.valueOf(split[1]),
Double.valueOf(split[2]));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.echoError("valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
else if (split.length == 6)
// If 6 values, location with pitch/yaw
// x,y,z,yaw,pitch,world
{
try {
return new dLocation(Bukkit.getWorld(split[5]),
Double.valueOf(split[0]),
Double.valueOf(split[1]),
Double.valueOf(split[2]),
Float.valueOf(split[3]),
Float.valueOf(split[4]));
}
catch (Exception e) {
if (context == null || context.debug) {
dB.echoError("valueOf dLocation returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
if (context == null || context.debug) {
dB.log("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(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() ? new dInventory(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.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.startsWith("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()).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()).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")) {
Material type = getBlock().getType();
if (type == Material.IRON_DOOR_BLOCK
|| type == Material.WOODEN_DOOR
|| type == Material.DARK_OAK_DOOR
|| type == Material.BIRCH_DOOR
|| type == Material.ACACIA_DOOR
|| type == Material.JUNGLE_DOOR
|| type == Material.SPRUCE_DOOR) {
Location location = this;
int data = getBlock().getData();
if (data >= 8) {
location = clone().add(0, -1, 0);
}
return new Element((location.getBlock().getData() & 0x4) > 0).getAttribute(attribute.fulfill(1));
}
else if (type == Material.TRAP_DOOR
|| type == Material.IRON_TRAPDOOR) {
return new Element((getBlock().getData() & 0x4) > 0).getAttribute(attribute.fulfill(1));
}
else {
return new Element((getBlock().getData() & 0x8) > 0).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.skull_skin>
// @returns 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.
// -->
// <--[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("skull_skin")) {
if (getBlock().getState() instanceof Skull) {
// TODO: use Bukkit Skull method when updated
GameProfile profile = ((TileEntitySkull) ((CraftWorld) getWorld())
.getTileEntityAt(getBlockX(), getBlockY(), getBlockZ())).getGameProfile();
String name = profile.getName();
UUID id = profile.getId();
com.mojang.authlib.properties.Property property = Iterables.getFirst(profile.getProperties().get("textures"), null);
attribute = attribute.fulfill(1);
if (attribute.startsWith("full")) {
return new Element((id != null ? id : name != null ? name : null)
+ (property != null ? "|" + property.getValue() : ""))
.getAttribute(attribute.fulfill(1));
}
return new Element(id != null ? id.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.
// EG: X 'x', Y 'y', Z 'z', in world 'world'
// -->
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.
// EG: x,y,z,world
// -->
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 = Rotation.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 = Rotation.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.getIntContext(1);
attribute = attribute.fulfill(1);
}
dList list = new dList();
org.bukkit.util.Vector rel = this.toVector().subtract(target.toVector());
double len = rel.length();
rel = rel.multiply(1d / len);
for (double i = 0d; i < len; i += rad) {
list.add(new dLocation(this.clone().add(rel.clone().multiply(i))).identify());
}
return list.getAttribute(attribute);
}
// <--[tag]
// @attribute <l@location.facing_blocks[<#>]>
// @returns dList(dLocation)
// @description
// Finds all block locations in the direction this location is facing,
// optionally with a custom range (default is 100).
// For example a location at 0,0,0 facing straight up
// will include 0,1,0 0,2,0 and so on.
// -->
if (attribute.startsWith("facing_blocks")) {
int range = attribute.getIntContext(1);
if (range < 1) {
range = 100;
}
dList list = new dList();
BlockIterator iterator = new BlockIterator(this, 0, range);
while (iterator.hasNext()) {
list.add(new dLocation(iterator.next().getLocation()).identify());
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.line_of_sight[<location>]>
// @returns Element(Boolean)
// @description
// Returns whether the specified location is within this location's
// line of sight.
// -->
if (attribute.startsWith("line_of_sight") && attribute.hasContext(1)) {
dLocation location = dLocation.valueOf(attribute.getContext(1));
if (location != null) {
return new Element(Rotation.rayTrace(getWorld(), toVector(), location.toVector()) == null)
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.direction.vector>
// @returns dLocation
// @description
// Returns the location's direction as a one-length vector.
// -->
if (attribute.startsWith("direction.vector")) {
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));
return new dLocation(getWorld(), nx, -ny, nz).getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <l@location.direction[<location>]>
// @returns Element
// @description
// Returns the compass direction between two locations.
// If no second location is specified, returns the direction of the location.
// Example returns include "north", "southwest", ...
// -->
if (attribute.startsWith("direction")) {
// Get the cardinal direction from this location to another
if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) {
// Subtract this location's vector from the other location's vector,
// not the other way around
dLocation target = dLocation.valueOf(attribute.getContext(1));
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <l@location.direction[<location>].yaw>
// @returns Element(Decimal)
// @description
// Returns the yaw direction between two locations.
// -->
if (attribute.startsWith("yaw")) {
return new Element(Rotation.normalizeYaw(Rotation.getYaw
(target.toVector().subtract(this.toVector())
.normalize())))
.getAttribute(attribute.fulfill(1));
}
else {
return new Element(Rotation.getCardinal(Rotation.getYaw
(target.toVector().subtract(this.toVector())
.normalize())))
.getAttribute(attribute);
}
}
// Get a cardinal direction from this location's yaw
else {
return new Element(Rotation.getCardinal(getYaw()))
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <l@location.face[<location>]>
// @returns dLocation
// @description
// Returns a location containing a yaw/pitch that point from the current location
// to the target location.
// -->
if (attribute.startsWith("face")
&& attribute.hasContext(1)) {
Location two = dLocation.valueOf(attribute.getContext(1));
return new dLocation(Rotation.faceLocation(this, two))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <l@location.facing[<entity>/<location>]>
// @returns Element(Boolean)
// @description
// Returns whether the location's yaw is facing another
// entity or location.
// -->
if (attribute.startsWith("facing")) {
if (attribute.hasContext(1)) {
// The default number of degrees if there is no degrees attribute
int degrees = 45;
// The attribute to fulfill from
int attributePos = 1;
// <--[tag]
// @attribute <location.facing[<entity>/<location>].degrees[X]>
// @returns Element(Boolean)