/
LocationTag.java
5528 lines (5208 loc) · 240 KB
/
LocationTag.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizen.objects.properties.bukkit.BukkitColorExtensions;
import com.denizenscript.denizen.objects.properties.material.*;
import com.denizenscript.denizen.scripts.commands.world.SwitchCommand;
import com.denizenscript.denizen.utilities.*;
import com.denizenscript.denizen.utilities.blocks.SpawnableHelper;
import com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker;
import com.denizenscript.denizen.utilities.flags.LocationFlagSearchHelper;
import com.denizenscript.denizen.utilities.world.PathFinder;
import com.denizenscript.denizen.utilities.world.WorldListChangeTracker;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.EntityHelper;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.SimplexNoise;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.block.banner.PatternType;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.structure.Mirror;
import org.bukkit.block.structure.StructureRotation;
import org.bukkit.block.structure.UsageMode;
import org.bukkit.entity.*;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.*;
import org.bukkit.loot.LootTable;
import org.bukkit.loot.Lootable;
import org.bukkit.material.Attachable;
import org.bukkit.material.MaterialData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.*;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class LocationTag extends org.bukkit.Location implements ObjectTag, Notable, Adjustable, FlaggableObject {
// <--[ObjectType]
// @name LocationTag
// @prefix l
// @base ElementTag
// @implements FlaggableObject
// @ExampleTagBase player.location
// @ExampleValues <player.location>,<npc.location>
// @ExampleForReturns
// - teleport <player> %VALUE%
// @format
// The identity format for locations is <x>,<y>,<z>,<pitch>,<yaw>,<world>
// Note that you can leave off the world, and/or pitch and yaw, and/or the z value.
// You cannot leave off both the z and the pitch+yaw at the same time.
// For example, 'l@1,2.15,3,45,90,space' or 'l@7.5,99,3.2'
//
// @description
// A LocationTag represents a point in the world.
//
// Note that 'l' prefix is a lowercase 'L', the first letter in 'location'.
//
// This object type is flaggable.
// Flags on this object type will be stored in the chunk file inside the world folder.
//
// @Matchable
// LocationTag matchers, sometimes identified as "<location>" or "<block>":
// "location" plaintext: always matches.
// "block_flagged:<flag>": a Flag Matchable for location flags at the given block location.
// "location_in:<area>": runs AreaObject checks, as defined below.
// If none of the above are used, and the location is at a real block, a MaterialTag matchable is used. Refer to <@link objecttype MaterialTag> matchable list.
//
// -->
/**
* The world name if a world reference is bad.
*/
public String backupWorld;
public int trackedWorldChange;
public String getWorldName() {
if (backupWorld != null) {
return backupWorld;
}
World w = super.getWorld();
if (w != null) {
backupWorld = w.getName();
}
return backupWorld;
}
@Override
public World getWorld() {
World w = super.getWorld();
if (w != null) {
if (trackedWorldChange == WorldListChangeTracker.changes) {
return w;
}
if (backupWorld == null) {
backupWorld = w.getName();
}
}
if (backupWorld == null) {
return null;
}
trackedWorldChange = WorldListChangeTracker.changes;
super.setWorld(Bukkit.getWorld(backupWorld));
return super.getWorld();
}
@Override
public LocationTag clone() {
return (LocationTag) super.clone();
}
public void makeUnique(String id) {
NoteManager.saveAs(this, id);
}
@Note("Locations")
public String getSaveObject() {
return getX() + "," + getY() + "," + getZ() + "," + getPitch() + "," + getYaw() + "," + getWorldName();
}
public static String getSaved(LocationTag location) {
return NoteManager.getSavedId(location);
}
@Override
public void forget() {
NoteManager.remove(this);
}
@Fetchable("l")
public static LocationTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (string.startsWith("l@")) {
string = string.substring(2);
}
if (!TagManager.isStaticParsing) {
Notable noted = NoteManager.getSavedObject(string);
if (noted instanceof LocationTag) {
return (LocationTag) noted;
}
}
List<String> split = CoreUtilities.split(string, ',');
try {
int size = split.size();
if (size < 2 || size > 6) {
if ((context == null || context.showErrors()) && !TagManager.isStaticParsing) {
Debug.log("Minor: valueOf LocationTag returning null, not formatted as a LocationTag: " + string);
}
return null;
}
// If 2 values, worldless 2D location format: x,y
// If 3 values, worldless location format: x,y,z
// If 4 values, standard dScript location format: x,y,z,world
// If 5 values, worldless location with pitch/yaw: x,y,z,pitch,yaw
// If 6 values, location with pitch/yaw: x,y,z,pitch,yaw,world
double x = Double.parseDouble(split.get(0));
double y = Double.parseDouble(split.get(1));
double z = 0;
float yaw = 0, pitch = 0;
String world = null;
if (size > 2) {
z = Double.parseDouble(split.get(2));
}
if (size == 5 || size == 6) {
pitch = Float.parseFloat(split.get(3));
yaw = Float.parseFloat(split.get(4));
}
if (size == 4 || size == 6) {
world = split.get(size - 1);
if (world.startsWith("w@")) {
world = world.substring("w@".length());
}
}
return new LocationTag(world, x, y, z, yaw, pitch);
}
catch (Exception e) {
if (context == null || context.showErrors()) {
Debug.log("Minor: valueOf LocationTag returning null: " + string + "(internal exception:" + e.getMessage() + ")");
}
return null;
}
}
public static boolean matches(String string) {
if (string == null || string.length() == 0) {
return false;
}
if (string.startsWith("l@")) {
return true;
}
return LocationTag.valueOf(string, CoreUtilities.noDebugContext) != null;
}
@Override
public ObjectTag duplicate() {
return clone();
}
/**
* Turns a Bukkit Location into a LocationTag, which has some helpful methods
* for working with dScript.
*
* @param location the Bukkit Location to reference
*/
public LocationTag(Location location) {
this(location.getWorld(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
if (location instanceof LocationTag) {
backupWorld = ((LocationTag) location).backupWorld;
}
}
public LocationTag(Vector vector) {
this((World) null, vector.getX(), vector.getY(), vector.getZ(), 0, 0);
}
public LocationTag(World world, Vector vector) {
this(world, vector.getX(), vector.getY(), vector.getZ(), 0, 0);
}
public LocationTag(World world, double x, double y) {
this(world, x, y, 0);
}
/**
* 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 LocationTag(World world, double x, double y, double z) {
this(world, x, y, z, 0, 0);
}
public LocationTag(double x, double y, double z, String worldName) {
super(worldName == null ? null : Bukkit.getWorld(worldName), x, y, z);
backupWorld = worldName;
}
public LocationTag(World world, double x, double y, double z, float yaw, float pitch) {
super(world, x, y, z, EntityHelper.normalizeYaw(yaw), pitch);
if (world != null) {
backupWorld = world.getName();
}
}
public LocationTag(String worldName, double x, double y, double z, float yaw, float pitch) {
super(worldName == null ? null : Bukkit.getWorld(worldName), x, y, z, EntityHelper.normalizeYaw(yaw), pitch);
backupWorld = worldName;
}
public boolean isChunkLoaded() {
return getWorld() != null && getWorld().isChunkLoaded(getBlockX() >> 4, getBlockZ() >> 4);
}
public boolean isChunkLoadedSafe() {
try {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
return isChunkLoaded();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
@Override
public Block getBlock() {
if (getWorld() == null) {
Debug.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
return super.getBlock();
}
public Block getBlockForTag(Attribute attribute) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return super.getBlock();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public BlockData getBlockDataForTag(Attribute attribute) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return super.getBlock().getBlockData();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public Material getBlockTypeForTag(Attribute attribute) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return super.getBlock().getType();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public static BlockState getBlockStateSafe(Block block) {
NMSHandler.chunkHelper.changeChunkServerThread(block.getWorld());
try {
return block.getState();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(block.getWorld());
}
}
public BiomeNMS getBiome() {
return NMSHandler.instance.getBiomeAt(super.getBlock());
}
public BiomeNMS getBiomeForTag(Attribute attribute) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return getBiome();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public Location getHighestBlockForTag(Attribute attribute) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return getWorld().getHighestBlockAt(this).getLocation();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public Collection<ItemStack> getDropsForTag(Attribute attribute, ItemStack item) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return null;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return null;
}
return item == null ? super.getBlock().getDrops() : super.getBlock().getDrops(item);
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public int getExpDropForTag(Attribute attribute, ItemStack item) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
attribute.echoError("LocationTag trying to read block, but cannot because no world is specified.");
return 0;
}
if (!isChunkLoaded()) {
attribute.echoError("LocationTag trying to read block, but cannot because the chunk is unloaded. Use the 'chunkload' command to ensure the chunk is loaded.");
return 0;
}
return NMSHandler.blockHelper.getExpDrop(super.getBlock(), item);
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public BlockState getBlockState() {
return getBlock().getState();
}
public BlockState getBlockStateForTag(Attribute attribute) {
Block block = getBlockForTag(attribute);
if (block == null) {
return null;
}
return getBlockStateSafe(block);
}
public static boolean isSameBlock(Location a, Location b) {
return a != null && b != null && a.getWorld() == b.getWorld() && a.getBlockX() == b.getBlockX()
&& a.getBlockY() == b.getBlockY() && a.getBlockZ() == b.getBlockZ();
}
public LocationTag getBlockLocation() {
return new LocationTag(getWorld(), getBlockX(), getBlockY(), getBlockZ());
}
@Override
public AbstractFlagTracker getFlagTracker() {
if (getWorld() == null) {
return null;
}
return new DataPersistenceFlagTracker(getChunk(), "flag_tracker_" + getBlockX() + "_" + getBlockY() + "_" + getBlockZ() + "_");
}
@Override
public AbstractFlagTracker getFlagTrackerForTag() {
if (!isChunkLoadedSafe()) {
return null;
}
return getFlagTracker();
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
@Override
public String getReasonNotFlaggable() {
if (getWorld() == null) {
return "missing world";
}
if (!isChunkLoadedSafe()) {
return "chunk is not loaded";
}
return "unknown reason";
}
@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);
}
@Override
public LocationTag add(double x, double y, double z) {
super.add(x, y, z);
return this;
}
@Override
public LocationTag add(Vector input) {
super.add(input);
return this;
}
@Override
public LocationTag add(Location input) {
super.add(input.getX(), input.getY(), input.getZ());
return this;
}
@Override
public LocationTag multiply(double input) {
super.multiply(input);
return this;
}
@Override
public void setWorld(World world) {
super.setWorld(world);
backupWorld = world == null ? null : world.getName();
}
public double distanceSquaredNoWorld(Location loc2) {
return NumberConversions.square(getX() - loc2.getX()) + NumberConversions.square(getY() - loc2.getY()) + NumberConversions.square(getZ() - loc2.getZ());
}
public Inventory getBukkitInventory() {
BlockState state = getBlockState();
if (state instanceof InventoryHolder) {
return((InventoryHolder) state).getInventory();
}
return null;
}
public InventoryTag getInventory() {
Inventory inv = getBukkitInventory();
if (inv != null) {
return InventoryTag.mirrorBukkitInventory(inv);
}
Material type = getBlock().getType();
if (type == Material.ANVIL || type == Material.CHIPPED_ANVIL || type == Material.DAMAGED_ANVIL) {
return new InventoryTag(Bukkit.createInventory(null, InventoryType.ANVIL), "location", this.clone());
}
return 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 static double[] getRotatedAroundX(double angle, double y, double z) {
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double newY = (y * cos) - (z * sin);
double newZ = (y * sin) + (z * cos);
return new double[] { newY, newZ };
}
public static double[] getRotatedAroundY(double angle, double x, double z) {
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double newX = (x * cos) + (z * sin);
double newZ = (x * -sin) + (z * cos);
return new double[] { newX, newZ };
}
public static double[] getRotatedAroundZ(double angle, double x, double y) {
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double newX = (x * cos) - (y * sin);
double newY = (x * sin) + (y * cos);
return new double[] { newX, newY };
}
public static double[] parsePointsAroundArgs(Attribute attribute) {
MapTag inputMap = attribute.inputParameterMap();
ElementTag radiusElement = inputMap.getRequiredObjectAs("radius", ElementTag.class, attribute);
ElementTag amountElement = inputMap.getRequiredObjectAs("points", ElementTag.class, attribute);
if (radiusElement == null || amountElement == null) {
return null;
}
double radius = radiusElement.asDouble();
int amount = amountElement.asInt();
if (amount < 1) {
attribute.echoError("Invalid amount of points! There must be at least 1 point.");
return null;
}
return new double[] { radius, amount };
}
public static class FloodFiller {
public Set<LocationTag> result;
public int iterationLimit;
public AreaContainmentObject areaLimit;
public Material requiredMaterial;
public String matcher;
public void run(LocationTag start, AreaContainmentObject area) {
iterationLimit = Settings.blockTagsMaxBlocks();
areaLimit = area;
result = new LinkedHashSet<>();
flood(start.getBlockLocation());
}
public void flood(LocationTag loc) {
if (iterationLimit-- <= 0 || result.contains(loc) || !areaLimit.doesContainLocation(loc)) {
return;
}
if (!loc.isChunkLoaded()) {
return;
}
if (matcher == null ? loc.getBlock().getType() != requiredMaterial : !loc.tryAdvancedMatcher(matcher)) {
return;
}
result.add(loc);
flood(loc.clone().add(-1, 0, 0));
flood(loc.clone().add(1, 0, 0));
flood(loc.clone().add(0, 0, -1));
flood(loc.clone().add(0, 0, 1));
flood(loc.clone().add(0, -1, 0));
flood(loc.clone().add(0, 1, 0));
}
}
public int compare(Location loc1, Location loc2) {
if (loc1 == null || loc2 == null || loc1.equals(loc2)) {
return 0;
}
else {
return Double.compare(distanceSquared(loc1), distanceSquared(loc2));
}
}
@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 LocationTag)) {
return false;
}
LocationTag other = (LocationTag) o;
if ((other.getWorldName() == null && getWorldName() != null)
|| (getWorldName() == null && other.getWorldName() != null)
|| (getWorldName() != null && other.getWorldName() != null
&& !CoreUtilities.equalsIgnoreCase(getWorldName(), other.getWorldName()))) {
return false;
}
return getX() == other.getX()
&& getY() == other.getY()
&& getZ() == other.getZ()
&& getYaw() == other.getYaw()
&& getPitch() == other.getPitch();
}
String prefix = "Location";
@Override
public String getPrefix() {
return prefix;
}
@Override
public LocationTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debuggable() {
String saved = getSaved(this);
if (saved != null) {
return "<Y>" + saved + "<GR> (" + identifyRaw().replace(",", "<G>,<GR> ") + "<GR>)";
}
else {
return "<Y>" + identifyRaw().replace(",", "<G>,<Y> ");
}
}
@Override
public boolean isUnique() {
return getSaved(this) != null;
}
@Override
public String identify() {
String saved = getSaved(this);
if (saved != null) {
return "l@" + saved;
}
return identifyRaw();
}
@Override
public String identifySimple() {
String saved = getSaved(this);
if (saved != null) {
return saved;
}
else if (getWorldName() == null) {
return "l@" + getBlockX() + "," + getBlockY() + "," + getBlockZ();
}
else {
return "l@" + getBlockX() + "," + getBlockY() + "," + getBlockZ() + "," + getWorldName();
}
}
public String identifyRaw() {
if (getYaw() != 0.0 || getPitch() != 0.0) {
return "l@" + CoreUtilities.doubleToString(getX()) + "," + CoreUtilities.doubleToString(getY())
+ "," + CoreUtilities.doubleToString(getZ()) + "," + CoreUtilities.floatToCleanString(getPitch())
+ "," + CoreUtilities.floatToCleanString(getYaw())
+ (getWorldName() != null ? "," + getWorldName() : "");
}
else {
return "l@" + CoreUtilities.doubleToString(getX()) + "," + CoreUtilities.doubleToString(getY())
+ "," + CoreUtilities.doubleToString(getZ())
+ (getWorldName() != null ? "," + getWorldName() : "");
}
}
@Override
public String toString() {
return identify();
}
@Override
public Object getJavaObject() {
return clone();
}
public static void register() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <LocationTag.block_facing>
// @returns LocationTag
// @mechanism LocationTag.block_facing
// @group world
// @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).
// -->
tagProcessor.registerTag(LocationTag.class, "block_facing", (attribute, object) -> {
BlockData block = object.getBlockDataForTag(attribute);
MaterialTag material = new MaterialTag(block);
if (!MaterialDirectional.describes(material)) {
return null;
}
Vector vec = MaterialDirectional.getFrom(material).getDirectionVector();
if (vec == null) {
return null;
}
return new LocationTag(object.getWorld(), vec);
});
// <--[tag]
// @attribute <LocationTag.with_facing_direction>
// @returns LocationTag
// @group world
// @description
// Returns the location with its direction set to the block's facing direction.
// Only works for block types that have directionality (such as signs, chests, stairs, etc.).
// You can use <some_block_location.with_facing_direction.forward[1]> to get the block directly in front of this block (based on its facing direction).
// -->
tagProcessor.registerTag(LocationTag.class, "with_facing_direction", (attribute, object) -> {
BlockData block = object.getBlockDataForTag(attribute);
MaterialTag material = new MaterialTag(block);
if (!MaterialDirectional.describes(material)) {
return null;
}
Vector facing = MaterialDirectional.getFrom(material).getDirectionVector();
if (facing == null) {
return null;
}
LocationTag result = object.clone();
result.setDirection(facing);
return result;
});
// <--[tag]
// @attribute <LocationTag.above[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location above this location. Optionally specify a number of blocks to go up.
// This just moves straight along the Y axis, equivalent to <@link tag LocationTag.add> with input 0,1,0 (or the input value instead of '1').
// -->
tagProcessor.registerTag(LocationTag.class, "above", (attribute, object) -> {
return new LocationTag(object.clone().add(0, attribute.hasParam() ? attribute.getDoubleParam() : 1, 0));
});
// <--[tag]
// @attribute <LocationTag.below[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location below this location. Optionally specify a number of blocks to go down.
// This just moves straight along the Y axis, equivalent to <@link tag LocationTag.sub> with input 0,1,0 (or the input value instead of '1').
// -->
tagProcessor.registerTag(LocationTag.class, "below", (attribute, object) -> {
return new LocationTag(object.clone().subtract(0, attribute.hasParam() ? attribute.getDoubleParam() : 1, 0));
});
// <--[tag]
// @attribute <LocationTag.forward_flat[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location in front of this location based on yaw but not pitch. Optionally specify a number of blocks to go forward.
// -->
tagProcessor.registerTag(LocationTag.class, "forward_flat", (attribute, object) -> {
Location loc = object.clone();
loc.setPitch(0);
Vector vector = loc.getDirection().multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().add(vector));
});
// <--[tag]
// @attribute <LocationTag.backward_flat[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location behind this location based on yaw but not pitch. Optionally specify a number of blocks to go backward.
// This is equivalent to <@link tag LocationTag.forward_flat> in the opposite direction.
// -->
tagProcessor.registerTag(LocationTag.class, "backward_flat", (attribute, object) -> {
Location loc = object.clone();
loc.setPitch(0);
Vector vector = loc.getDirection().multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().subtract(vector));
});
// <--[tag]
// @attribute <LocationTag.forward[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location in front of this location based on pitch and yaw. Optionally specify a number of blocks to go forward.
// -->
tagProcessor.registerTag(LocationTag.class, "forward", (attribute, object) -> {
Vector vector = object.getDirection().multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().add(vector));
});
// <--[tag]
// @attribute <LocationTag.backward[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location behind this location based on pitch and yaw. Optionally specify a number of blocks to go backward.
// This is equivalent to <@link tag LocationTag.forward> in the opposite direction.
// -->
tagProcessor.registerTag(LocationTag.class, "backward", (attribute, object) -> {
Vector vector = object.getDirection().multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().subtract(vector));
});
// <--[tag]
// @attribute <LocationTag.left[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location to the left of this location based on pitch and yaw. Optionally specify a number of blocks to go left.
// This is equivalent to <@link tag LocationTag.forward> with a +90 degree rotation to the yaw and the pitch set to 0.
// -->
tagProcessor.registerTag(LocationTag.class, "left", (attribute, object) -> {
Location loc = object.clone();
loc.setPitch(0);
Vector vector = loc.getDirection().rotateAroundY(Math.PI / 2).multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().add(vector));
});
// <--[tag]
// @attribute <LocationTag.right[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location to the right of this location based on pitch and yaw. Optionally specify a number of blocks to go right.
// This is equivalent to <@link tag LocationTag.forward> with a -90 degree rotation to the yaw and the pitch set to 0.
// -->
tagProcessor.registerTag(LocationTag.class, "right", (attribute, object) -> {
Location loc = object.clone();
loc.setPitch(0);
Vector vector = loc.getDirection().rotateAroundY(Math.PI / 2).multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().subtract(vector));
});
// <--[tag]
// @attribute <LocationTag.up[(<#.#>)]>
// @returns LocationTag
// @group math
// @description
// Returns the location above this location based on pitch and yaw. Optionally specify a number of blocks to go up.
// This is equivalent to <@link tag LocationTag.forward> with a +90 degree rotation to the pitch.
// To just get the location above this location, use <@link tag LocationTag.above> instead.
// -->
tagProcessor.registerTag(LocationTag.class, "up", (attribute, object) -> {
Location loc = object.clone();
loc.setPitch(loc.getPitch() - 90);
Vector vector = loc.getDirection().multiply(attribute.hasParam() ? attribute.getDoubleParam() : 1);
return new LocationTag(object.clone().add(vector));
});