forked from PaperMC/Paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.java
More file actions
1294 lines (1177 loc) · 42.6 KB
/
Location.java
File metadata and controls
1294 lines (1177 loc) · 42.6 KB
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 org.bukkit;
import com.google.common.base.Preconditions;
import io.papermc.paper.math.FinePosition;
import io.papermc.paper.math.Rotation;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import org.bukkit.block.Block;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Represents a 3-dimensional position in a world.
* <br>
* No constraints are placed on any angular values other than that they be
* specified in degrees. This means that negative angles or angles of greater
* magnitude than 360 are valid, but may be normalized to any other equivalent
* representation by the implementation.
*/
public class Location implements Cloneable, ConfigurationSerializable, io.papermc.paper.math.FinePosition {
private Reference<World> world;
private double x;
private double y;
private double z;
private float pitch;
private float yaw;
/**
* Constructs a new Location with the given coordinates
*
* @param world The world in which this location resides
* @param x The x-coordinate of this new location
* @param y The y-coordinate of this new location
* @param z The z-coordinate of this new location
*/
public Location(@UndefinedNullability final World world, final double x, final double y, final double z) {
this(world, x, y, z, 0, 0);
}
/**
* Constructs a new Location with the given coordinates and direction
*
* @param world The world in which this location resides
* @param x The x-coordinate of this new location
* @param y The y-coordinate of this new location
* @param z The z-coordinate of this new location
* @param yaw The absolute rotation on the x-plane, in degrees
* @param pitch The absolute rotation on the y-plane, in degrees
*/
public Location(@UndefinedNullability final World world, final double x, final double y, final double z, final float yaw, final float pitch) {
if (world != null) {
this.world = new WeakReference<>(world);
}
this.x = x;
this.y = y;
this.z = z;
this.pitch = pitch;
this.yaw = yaw;
}
/**
* Sets the world that this location resides in
*
* @param world New world that this location resides in
*/
public void setWorld(@Nullable World world) {
this.world = (world == null) ? null : new WeakReference<>(world);
}
/**
* Checks if world in this location is present and loaded.
*
* @return true if is loaded, otherwise false
*/
public boolean isWorldLoaded() {
if (this.world == null) {
return false;
}
World world = this.world.get();
return world != null && world.equals(Bukkit.getWorld(world.getUID()));
}
/**
* Gets the world that this location resides in
*
* @return World that contains this location, or {@code null} if it is not set
* @throws IllegalArgumentException when world is unloaded
* @see #isWorldLoaded()
*/
@UndefinedNullability
public World getWorld() {
if (this.world == null) {
return null;
}
World world = this.world.get();
Preconditions.checkArgument(world != null, "World unloaded");
return world;
}
/**
* Gets the chunk at the represented location
*
* @return Chunk at the represented location
*/
@NotNull
public Chunk getChunk() {
return getWorld().getChunkAt(this);
}
/**
* Gets the block at the represented location
*
* @return Block at the represented location
*/
@NotNull
public Block getBlock() {
return getWorld().getBlockAt(this);
}
/**
* Sets the x-coordinate of this location
*
* @param x X-coordinate
*/
public void setX(double x) {
this.x = x;
}
/**
* Gets the x-coordinate of this location
*
* @return x-coordinate
*/
public double getX() {
return x;
}
/**
* Gets the floored value of the X component, indicating the block that
* this location is contained with.
*
* @return block X
*/
public int getBlockX() {
return locToBlock(x);
}
/**
* Sets the y-coordinate of this location
*
* @param y y-coordinate
*/
public void setY(double y) {
this.y = y;
}
/**
* Gets the y-coordinate of this location
*
* @return y-coordinate
*/
public double getY() {
return y;
}
/**
* Gets the floored value of the Y component, indicating the block that
* this location is contained with.
*
* @return block y
*/
public int getBlockY() {
return locToBlock(y);
}
/**
* Sets the z-coordinate of this location
*
* @param z z-coordinate
*/
public void setZ(double z) {
this.z = z;
}
/**
* Gets the z-coordinate of this location
*
* @return z-coordinate
*/
public double getZ() {
return z;
}
/**
* Gets the floored value of the Z component, indicating the block that
* this location is contained with.
*
* @return block z
*/
public int getBlockZ() {
return locToBlock(z);
}
/**
* Sets the yaw of this location, measured in degrees.
* <ul>
* <li>A yaw of 0 or 360 represents the positive z direction.
* <li>A yaw of 180 represents the negative z direction.
* <li>A yaw of 90 represents the negative x direction.
* <li>A yaw of 270 represents the positive x direction.
* </ul>
* Increasing yaw values are the equivalent of turning to your
* right-facing, increasing the scale of the next respective axis, and
* decreasing the scale of the previous axis.
*
* @param yaw new rotation's yaw
*/
public void setYaw(float yaw) {
this.yaw = yaw;
}
/**
* Gets the yaw of this location, measured in degrees.
* <ul>
* <li>A yaw of 0 or 360 represents the positive z direction.
* <li>A yaw of 180 represents the negative z direction.
* <li>A yaw of 90 represents the negative x direction.
* <li>A yaw of 270 represents the positive x direction.
* </ul>
* Increasing yaw values are the equivalent of turning to your
* right-facing, increasing the scale of the next respective axis, and
* decreasing the scale of the previous axis.
*
* @return the rotation's yaw
*/
public float getYaw() {
return yaw;
}
/**
* Sets the pitch of this location, measured in degrees.
* <ul>
* <li>A pitch of 0 represents level forward facing.
* <li>A pitch of 90 represents downward facing, or negative y
* direction.
* <li>A pitch of -90 represents upward facing, or positive y direction.
* </ul>
* Increasing pitch values the equivalent of looking down.
*
* @param pitch new incline's pitch
*/
public void setPitch(float pitch) {
this.pitch = pitch;
}
/**
* Gets the pitch of this location, measured in degrees.
* <ul>
* <li>A pitch of 0 represents level forward facing.
* <li>A pitch of 90 represents downward facing, or negative y
* direction.
* <li>A pitch of -90 represents upward facing, or positive y direction.
* </ul>
* Increasing pitch values the equivalent of looking down.
*
* @return the incline's pitch
*/
public float getPitch() {
return pitch;
}
/**
* Gets a unit-vector pointing in the direction that this Location is
* facing.
*
* @return a vector pointing the direction of this location's {@link
* #getPitch() pitch} and {@link #getYaw() yaw}
*/
@NotNull
public Vector getDirection() {
Vector vector = new Vector();
double rotX = this.getYaw();
double rotY = this.getPitch();
vector.setY(-Math.sin(Math.toRadians(rotY)));
double xz = Math.cos(Math.toRadians(rotY));
vector.setX(-xz * Math.sin(Math.toRadians(rotX)));
vector.setZ(xz * Math.cos(Math.toRadians(rotX)));
return vector;
}
/**
* Sets the {@link #getYaw() yaw} and {@link #getPitch() pitch} to point
* in the direction of the vector.
*
* @param vector the direction vector
* @return the same location
*/
@NotNull
public Location setDirection(@NotNull Vector vector) {
/*
* Sin = Opp / Hyp
* Cos = Adj / Hyp
* Tan = Opp / Adj
*
* x = -Opp
* z = Adj
*/
final double _2PI = 2 * Math.PI;
final double x = vector.getX();
final double z = vector.getZ();
if (x == 0 && z == 0) {
pitch = vector.getY() > 0 ? -90 : 90;
return this;
}
double theta = Math.atan2(-x, z);
yaw = (float) Math.toDegrees((theta + _2PI) % _2PI);
double x2 = NumberConversions.square(x);
double z2 = NumberConversions.square(z);
double xz = Math.sqrt(x2 + z2);
pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz));
return this;
}
/**
* Adds the location by another.
*
* @param vec The other location
* @return the same location
* @throws IllegalArgumentException for differing worlds
* @see Vector
*/
@NotNull
public Location add(@NotNull Location vec) {
if (vec == null || vec.getWorld() != getWorld()) {
throw new IllegalArgumentException("Cannot add Locations of differing worlds");
}
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
/**
* Adds the location by a vector.
*
* @param vec Vector to use
* @return the same location
* @see Vector
*/
@NotNull
public Location add(@NotNull Vector vec) {
this.x += vec.getX();
this.y += vec.getY();
this.z += vec.getZ();
return this;
}
/**
* Adds the location by another. Not world-aware.
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @return the same location
* @see Vector
*/
@NotNull
public Location add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Adds rotation in the form of yaw and patch to this location. Not world-aware.
*
* @param yaw yaw, measured in degrees.
* @param pitch pitch, measured in degrees.
* @return the same location
* @see Vector
*/
@NotNull
@Contract(value = "_,_ -> this", mutates = "this")
public Location addRotation(final float yaw, final float pitch) {
this.yaw += yaw;
this.pitch += pitch;
return this;
}
/**
* Adds rotation to this location. Not world-aware.
*
* @param rotation the rotation to add.
* @return the same location
* @see Vector
*/
@NotNull
@Contract(value = "_ -> this", mutates = "this")
public Location addRotation(@NotNull Rotation rotation) {
return addRotation(rotation.yaw(), rotation.pitch());
}
/**
* Retrieves the rotation of this location.
*
* @return a new {@code Rotation} object
*/
@NotNull
@Contract(value = " -> new", pure = true)
public Rotation getRotation() {
return Rotation.rotation(yaw, pitch);
}
/**
* Subtracts the location by another.
*
* @param vec The other location
* @return the same location
* @throws IllegalArgumentException for differing worlds
* @see Vector
*/
@NotNull
public Location subtract(@NotNull Location vec) {
if (vec == null || vec.getWorld() != getWorld()) {
throw new IllegalArgumentException("Cannot add Locations of differing worlds");
}
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
/**
* Subtracts the location by a vector.
*
* @param vec The vector to use
* @return the same location
* @see Vector
*/
@NotNull
public Location subtract(@NotNull Vector vec) {
this.x -= vec.getX();
this.y -= vec.getY();
this.z -= vec.getZ();
return this;
}
/**
* Subtracts the location by another. Not world-aware and
* orientation independent.
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @return the same location
* @see Vector
*/
@NotNull
public Location subtract(double x, double y, double z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Subtracts rotation in the form of yaw and patch from this location.
*
* @param yaw yaw, measured in degrees.
* @param pitch pitch, measured in degrees.
* @return the same location
* @see Vector
*/
@NotNull
@Contract(value = "_,_ -> this", mutates = "this")
public Location subtractRotation(final float yaw, final float pitch) {
this.yaw -= yaw;
this.pitch -= pitch;
return this;
}
/**
* Subtracts rotation from this location.
*
* @param rotation the rotation to subtract.
* @return the same location
* @see Vector
*/
@NotNull
@Contract(value = "_ -> this", mutates = "this")
public Location subtractRotation(@NotNull Rotation rotation) {
return subtractRotation(rotation.yaw(), rotation.pitch());
}
/**
* Gets the magnitude of the location, defined as sqrt(x^2+y^2+z^2). The
* value of this method is not cached and uses a costly square-root
* function, so do not repeatedly call this method to get the location's
* magnitude. NaN will be returned if the inner result of the sqrt()
* function overflows, which will be caused if the length is too long. Not
* world-aware and orientation independent.
*
* @return the magnitude
* @see Vector
*/
public double length() {
return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z));
}
/**
* Gets the magnitude of the location squared. Not world-aware and
* orientation independent.
*
* @return the magnitude
* @see Vector
*/
public double lengthSquared() {
return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z);
}
/**
* Get the distance between this location and another. The value of this
* method is not cached and uses a costly square-root function, so do not
* repeatedly call this method to get the location's magnitude. NaN will
* be returned if the inner result of the sqrt() function overflows, which
* will be caused if the distance is too long.
*
* @param o The other location
* @return the distance
* @throws IllegalArgumentException for differing worlds
* @see Vector
*/
public double distance(@NotNull Location o) {
return Math.sqrt(distanceSquared(o));
}
/**
* Get the squared distance between this location and another.
*
* @param o The other location
* @return the distance
* @throws IllegalArgumentException for differing worlds
* @see Vector
*/
public double distanceSquared(@NotNull Location o) {
if (o == null) {
throw new IllegalArgumentException("Cannot measure distance to a null location");
} else if (o.getWorld() == null || getWorld() == null) {
throw new IllegalArgumentException("Cannot measure distance to a null world");
} else if (o.getWorld() != getWorld()) {
throw new IllegalArgumentException("Cannot measure distance between " + getWorld().key().asString() + " and " + o.getWorld().key().asString());
}
return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z);
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar. Not world-aware.
*
* @param m The factor
* @return the same location
* @see Vector
*/
@NotNull
public Location multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Zero this location's components. Not world-aware.
*
* @return the same location
* @see Vector
*/
@NotNull
public Location zero() {
x = 0;
y = 0;
z = 0;
return this;
}
public boolean isChunkLoaded() {
return this.getWorld().isChunkLoaded(locToBlock(x) >> 4, locToBlock(z) >> 4);
}
/**
* Checks if a {@link Chunk} has been generated at this location.
*
* @return true if a chunk has been generated at this location
*/
public boolean isGenerated() {
World world = this.getWorld();
Preconditions.checkNotNull(world, "Location has no world!");
return world.isChunkGenerated(locToBlock(x) >> 4, locToBlock(z) >> 4);
}
/**
* Sets the position of this Location and returns itself
* <p>
* This mutates this object, clone first.
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @return self (not cloned)
*/
@NotNull
public Location set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* Sets the rotation of this location and returns itself.
* <p>
* This mutates this object, clone first.
*
* @param yaw yaw, measured in degrees.
* @param pitch pitch, measured in degrees.
* @return self (not cloned)
*/
@NotNull
@Contract(value = "_,_ -> this", mutates = "this")
public Location setRotation(final float yaw, final float pitch) {
this.yaw = yaw;
this.pitch = pitch;
return this;
}
/**
* Sets the rotation of this location and returns itself.
* <p>
* This mutates this object, clone first.
*
* @param rotation the new rotation.
* @return self (not cloned)
*/
@NotNull
@Contract(value = "_ -> this", mutates = "this")
public Location setRotation(@NotNull Rotation rotation) {
return setRotation(rotation.yaw(), rotation.pitch());
}
/**
* Takes the x/y/z from base and adds the specified x/y/z to it and returns self
* <p>
* This mutates this object, clone first.
*
* @param base The base coordinate to modify
* @param x X coordinate to add to base
* @param y Y coordinate to add to base
* @param z Z coordinate to add to base
* @return self (not cloned)
*/
@NotNull
public Location add(@NotNull Location base, double x, double y, double z) {
return this.set(base.x + x, base.y + y, base.z + z);
}
/**
* Takes the x/y/z from base and subtracts the specified x/y/z to it and returns self
* <p>
* This mutates this object, clone first.
*
* @param base The base coordinate to modify
* @param x X coordinate to subtract from base
* @param y Y coordinate to subtract from base
* @param z Z coordinate to subtract from base
* @return self (not cloned)
*/
@NotNull
public Location subtract(@NotNull Location base, double x, double y, double z) {
return this.set(base.x - x, base.y - y, base.z - z);
}
/**
* @return A new location where X/Y/Z are on the Block location (integer value of X/Y/Z)
*/
@NotNull
public Location toBlockLocation() {
Location blockLoc = clone();
blockLoc.setX(getBlockX());
blockLoc.setY(getBlockY());
blockLoc.setZ(getBlockZ());
return blockLoc;
}
/**
* @return The block key for this location's block location.
* @see Block#getBlockKey(int, int, int)
* @deprecated only encodes y block ranges from -512 to 511 and represents an already changed implementation detail
*/
@Deprecated(since = "1.18.1")
public long toBlockKey() {
return Block.getBlockKey(getBlockX(), getBlockY(), getBlockZ());
}
/**
* @return A new location where X/Y/Z are the center of the block
*/
@NotNull
public Location toCenterLocation() {
Location centerLoc = clone();
centerLoc.setX(getBlockX() + 0.5);
centerLoc.setY(getBlockY() + 0.5);
centerLoc.setZ(getBlockZ() + 0.5);
return centerLoc;
}
/**
* Returns a copy of this location except with y = getWorld().getHighestBlockYAt(this.getBlockX(), this.getBlockZ())
* @return A copy of this location except with y = getWorld().getHighestBlockYAt(this.getBlockX(), this.getBlockZ())
* @throws NullPointerException if {@link #getWorld()} is {@code null}
*/
@NotNull
public Location toHighestLocation() {
return this.toHighestLocation(HeightMap.WORLD_SURFACE);
}
/**
* Returns a copy of this location except with y = getWorld().getHighestBlockYAt(this.getBlockX(), this.getBlockZ(), heightMap)
* @param heightMap The heightmap to use for finding the highest y location.
* @return A copy of this location except with y = getWorld().getHighestBlockYAt(this.getBlockX(), this.getBlockZ(), heightMap)
*/
@NotNull
public Location toHighestLocation(@NotNull final HeightMap heightMap) {
final Location ret = this.clone();
ret.setY(this.getWorld().getHighestBlockYAt(this, heightMap));
return ret;
}
/**
* Creates explosion at this location with given power
* <p>
* Will break blocks and ignite blocks on fire.
*
* @param power The power of explosion, where 4F is TNT
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(float power) {
return this.getWorld().createExplosion(this, power);
}
/**
* Creates explosion at this location with given power and optionally
* setting blocks on fire.
* <p>
* Will break blocks.
*
* @param power The power of explosion, where 4F is TNT
* @param setFire Whether to set blocks on fire
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(float power, boolean setFire) {
return this.getWorld().createExplosion(this, power, setFire);
}
/**
* Creates explosion at this location with given power and optionally
* setting blocks on fire.
*
* @param power The power of explosion, where 4F is TNT
* @param setFire Whether to set blocks on fire
* @param breakBlocks Whether to have blocks be destroyed
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(float power, boolean setFire, boolean breakBlocks) {
return this.getWorld().createExplosion(this, power, setFire, breakBlocks);
}
/**
* Creates explosion at this location with given power, with the specified entity as the source.
* <p>
* Will break blocks and ignite blocks on fire.
*
* @param source The source entity of the explosion
* @param power The power of explosion, where 4F is TNT
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(@Nullable Entity source, float power) {
return this.getWorld().createExplosion(source, this, power, true, true);
}
/**
* Creates explosion at this location with given power and optionally
* setting blocks on fire, with the specified entity as the source.
* <p>
* Will break blocks.
*
* @param source The source entity of the explosion
* @param power The power of explosion, where 4F is TNT
* @param setFire Whether to set blocks on fire
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(@Nullable Entity source, float power, boolean setFire) {
return this.getWorld().createExplosion(source, this, power, setFire, true);
}
/**
* Creates explosion at this location with given power and optionally
* setting blocks on fire, with the specified entity as the source.
*
* @param source The source entity of the explosion
* @param power The power of explosion, where 4F is TNT
* @param setFire Whether to set blocks on fire
* @param breakBlocks Whether to have blocks be destroyed
* @return false if explosion was canceled, otherwise true
*/
public boolean createExplosion(@Nullable Entity source, float power, boolean setFire, boolean breakBlocks) {
return this.getWorld().createExplosion(source, this, power, setFire, breakBlocks);
}
/**
* Returns a list of entities within a bounding box centered around a Location.
* <p>
* Some implementations may impose artificial restrictions on the size of the search bounding box.
*
* @param x 1/2 the size of the box along the x-axis
* @param y 1/2 the size of the box along the y-axis
* @param z 1/2 the size of the box along the z-axis
* @return the collection of entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<Entity> getNearbyEntities(final double x, final double y, final double z) {
final World world = this.getWorld();
if (world == null) {
throw new IllegalArgumentException("Location has no world");
}
return world.getNearbyEntities(this, x, y, z);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param radius X Radius
* @return the collection of entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double radius) {
return this.getNearbyEntitiesByType(LivingEntity.class, radius, radius, radius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xzRadius X/Z Radius
* @param yRadius Y Radius
* @return the collection of living entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double xzRadius, final double yRadius) {
return this.getNearbyEntitiesByType(LivingEntity.class, xzRadius, yRadius, xzRadius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xRadius X Radius
* @param yRadius Y Radius
* @param zRadius Z radius
* @return the collection of living entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double xRadius, final double yRadius, final double zRadius) {
return this.getNearbyEntitiesByType(LivingEntity.class, xRadius, yRadius, zRadius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param radius Radius
* @param predicate a predicate used to filter results
* @return the collection of living entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double radius, final @Nullable Predicate<? super LivingEntity> predicate) {
return this.getNearbyEntitiesByType(LivingEntity.class, radius, radius, radius, predicate);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xzRadius X/Z Radius
* @param yRadius Y Radius
* @param predicate a predicate used to filter results
* @return the collection of living entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double xzRadius, final double yRadius, final @Nullable Predicate<? super LivingEntity> predicate) {
return this.getNearbyEntitiesByType(LivingEntity.class, xzRadius, yRadius, xzRadius, predicate);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xRadius X Radius
* @param yRadius Y Radius
* @param zRadius Z radius
* @param predicate a predicate used to filter results
* @return the collection of living entities near location. This will always be a non-null collection.
*/
public @NotNull Collection<LivingEntity> getNearbyLivingEntities(final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate<? super LivingEntity> predicate) {
return this.getNearbyEntitiesByType(LivingEntity.class, xRadius, yRadius, zRadius, predicate);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param radius X/Y/Z Radius
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double radius) {
return this.getNearbyEntitiesByType(Player.class, radius, radius, radius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xzRadius X/Z Radius
* @param yRadius Y Radius
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double xzRadius, final double yRadius) {
return this.getNearbyEntitiesByType(Player.class, xzRadius, yRadius, xzRadius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xRadius X Radius
* @param yRadius Y Radius
* @param zRadius Z Radius
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double xRadius, final double yRadius, final double zRadius) {
return this.getNearbyEntitiesByType(Player.class, xRadius, yRadius, zRadius);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param radius X/Y/Z Radius
* @param predicate a predicate used to filter results
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double radius, final @Nullable Predicate<? super Player> predicate) {
return this.getNearbyEntitiesByType(Player.class, radius, radius, radius, predicate);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xzRadius X/Z Radius
* @param yRadius Y Radius
* @param predicate a predicate used to filter results
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double xzRadius, final double yRadius, final @Nullable Predicate<? super Player> predicate) {
return this.getNearbyEntitiesByType(Player.class, xzRadius, yRadius, xzRadius, predicate);
}
/**
* Gets nearby players within the specified radius (bounding box)
*
* @param xRadius X Radius
* @param yRadius Y Radius
* @param zRadius Z Radius
* @param predicate a predicate used to filter results
* @return the collection of players near location. This will always be a non-null collection.
*/
public @NotNull Collection<Player> getNearbyPlayers(final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate<? super Player> predicate) {
return this.getNearbyEntitiesByType(Player.class, xRadius, yRadius, zRadius, predicate);
}