-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
EntityTag.java
4255 lines (3948 loc) · 172 KB
/
EntityTag.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.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.abstracts.ProfileEditor;
import com.denizenscript.denizen.nms.interfaces.EntityAnimation;
import com.denizenscript.denizen.nms.interfaces.FakePlayer;
import com.denizenscript.denizen.nms.interfaces.PlayerHelper;
import com.denizenscript.denizen.npc.traits.MirrorTrait;
import com.denizenscript.denizen.objects.properties.entity.EntityAge;
import com.denizenscript.denizen.objects.properties.entity.EntityColor;
import com.denizenscript.denizen.objects.properties.entity.EntityTame;
import com.denizenscript.denizen.scripts.commands.player.DisguiseCommand;
import com.denizenscript.denizen.scripts.containers.core.EntityScriptContainer;
import com.denizenscript.denizen.scripts.containers.core.EntityScriptHelper;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizen.utilities.MultiVersionHelper1_19;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.VanillaTagHelper;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper;
import com.denizenscript.denizen.utilities.entity.FakeEntity;
import com.denizenscript.denizen.utilities.entity.HideEntitiesHelper;
import com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
import org.bukkit.loot.LootTable;
import org.bukkit.loot.Lootable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class EntityTag implements ObjectTag, Adjustable, EntityFormObject, FlaggableObject, Cloneable {
// <--[ObjectType]
// @name EntityTag
// @prefix e
// @base ElementTag
// @implements FlaggableObject, PropertyHolderObject
// @ExampleTagBase player
// @ExampleValues <player>,<npc>
// @ExampleForReturns
// - kill %VALUE%
// @ExampleForReturns
// - heal %VALUE%
// @ExampleForReturns
// - remove %VALUE%
// @format
// The identity format for entities is a spawned entity's UUID, or an entity type.
// For example, 'e@abc123' or 'e@zombie'.
//
// @description
// An EntityTag represents a spawned entity, or a generic entity type.
//
// Note that players and NPCs are valid EntityTags, but are generally represented by the more specific
// PlayerTag and NPCTag objects.
//
// Note that a spawned entity can be a living entity (a player, NPC, or mob) or a nonliving entity (a painting, item frame, etc).
//
// This object type is flaggable.
// Flags on this object type will be stored in the world chunk files as a part of the entity's NBT.
//
// @Matchable
// EntityTag matchers, sometimes identified as "<entity>", "<projectile>", or "<vehicle>":
// "entity" plaintext: always matches.
// "player" plaintext: matches any real player (not NPCs).
// "npc" plaintext: matches any Citizens NPC.
// "vehicle" plaintext: matches for any vehicle type (minecarts, boats, horses, etc).
// "fish" plaintext: matches for any fish type (cod, pufferfish, etc).
// "projectile" plaintext: matches for any projectile type (arrow, trident, fish hook, snowball, etc).
// "hanging" plaintext: matches for any hanging type (painting, item_frame, etc).
// "monster" plaintext: matches for any monster type (creepers, zombies, etc).
// "animal" plaintext: matches for any animal type (pigs, cows, etc).
// "mob" plaintext: matches for any mob type (creepers, pigs, etc).
// "living" plaintext: matches for any living type (players, pigs, creepers, etc).
// "vanilla_tagged:<tag_name>": matches if the given vanilla tag applies to the entity. Allows advanced matchers, for example: "vanilla_tagged:axolotl_*".
// "entity_flagged:<flag>": a Flag Matchable for EntityTag flags.
// "player_flagged:<flag>": a Flag Matchable for PlayerTag flags (will never match non-players).
// "npc_flagged:<flag>": a Flag Matchable for NPCTag flags (will never match non-NPCs).
// "npc_<type>": matches if the NPC is the given entity type (like "npc_cow" or "npc_mob" or "npc_player").
// Any entity type name: matches if the entity is of the given type, using advanced matchers.
//
// -->
/////////////////////
// STATIC METHODS
/////////////////
// List a mechanism here if it can be safely run before spawn.
public static HashSet<String> earlyValidMechanisms = new HashSet<>(Arrays.asList(
"max_health", "health_data", "health",
"visible", "armor_pose", "arms", "base_plate", "is_small", "marker",
"velocity", "age", "is_using_riptide", "size", "item", "scale", "translation",
"left_rotation", "right_rotation", "brightness", "display", "pivot",
"shadow_radius", "shadow_strength"
));
// Definitely not valid: "item"
private static final Map<UUID, Entity> rememberedEntities = new HashMap<>();
public static void rememberEntity(Entity entity) {
if (entity == null) {
return;
}
rememberedEntities.put(entity.getUniqueId(), entity);
}
public static void forgetEntity(Entity entity) {
if (entity == null) {
return;
}
rememberedEntities.remove(entity.getUniqueId());
}
public static boolean isNPC(Entity entity) {
return entity != null && entity.hasMetadata("NPC") && entity.getMetadata("NPC").get(0).asBoolean();
}
public static boolean isCitizensNPC(Entity entity) {
if (entity == null) {
return false;
}
if (Depends.citizens == null) {
return false;
}
if (!CitizensAPI.hasImplementation()) {
return false;
}
if (!(entity instanceof NPCHolder)) {
return false;
}
NPC npc = ((NPCHolder) entity).getNPC();
if (npc == null) {
return false;
}
return true;
}
public static NPCTag getNPCFrom(Entity entity) {
if (isCitizensNPC(entity)) {
return NPCTag.fromEntity(entity);
}
else {
return null;
}
}
public static boolean isPlayer(Entity entity) {
return entity instanceof Player && !isNPC(entity);
}
public static PlayerTag getPlayerFrom(Entity entity) {
if (isPlayer(entity)) {
return PlayerTag.mirrorBukkitPlayer((Player) entity);
}
else {
return null;
}
}
public ItemTag getItemInHand() {
if (isLivingEntity() && getLivingEntity().getEquipment() != null) {
ItemStack its = getLivingEntity().getEquipment().getItemInMainHand();
if (its == null) {
return null;
}
return new ItemTag(its.clone());
}
return null;
}
//////////////////
// OBJECT FETCHER
////////////////
public static boolean allowDespawnedNpcs = false;
@Fetchable("e")
public static EntityTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
UUID id = null;
if (string.startsWith("e@") && !string.startsWith("e@fake")) {
int slash = string.indexOf('/');
if (slash != -1) {
try {
id = UUID.fromString(string.substring(2, slash));
string = string.substring(slash + 1);
Entity entity = getEntityForID(id);
if (entity != null) {
EntityTag result = new EntityTag(entity);
if (string.equalsIgnoreCase(result.getEntityScript())
|| string.equalsIgnoreCase(result.getBukkitEntityType().name())) {
return result;
}
else if (context == null || context.showErrors()) {
Debug.echoError("Invalid EntityTag! ID '" + id + "' is valid, but '" + string + "' does not match its type data.");
}
}
}
catch (Exception ex) {
// DO NOTHING
}
}
}
if (ObjectFetcher.isObjectWithProperties(string)) {
return ObjectFetcher.getObjectFromWithProperties(EntityTag.class, string, context);
}
string = CoreUtilities.toLowerCase(string);
if (string.startsWith("e@")) {
if (string.startsWith("e@fake:")) {
try {
UUID entityID = UUID.fromString(string.substring("e@fake:".length()));
FakeEntity entity = FakeEntity.idsToEntities.get(entityID);
if (entity != null) {
return entity.entity;
}
return null;
}
catch (Exception ex) {
// DO NOTHING
}
}
string = string.substring("e@".length());
}
// Choose a random entity type if "random" is used
if (string.equals("random")) {
EntityType randomType = null;
// When selecting a random entity type, ignore invalid or inappropriate ones
while (randomType == null ||
randomType.name().matches("^(COMPLEX_PART|DROPPED_ITEM|ENDER_CRYSTAL" +
"|ENDER_DRAGON|FISHING_HOOK|ITEM_FRAME|LEASH_HITCH|LIGHTNING" +
"|PAINTING|PLAYER|UNKNOWN|WEATHER|WITHER|WITHER_SKULL)$")) {
randomType = EntityType.values()[CoreUtilities.getRandom().nextInt(EntityType.values().length)];
}
return new EntityTag(randomType, "RANDOM");
}
// NPC entity
if (string.startsWith("n@")) {
NPCTag npc = NPCTag.valueOf(string, context);
if (npc != null) {
if (npc.isSpawned()) {
return new EntityTag(npc);
}
else {
if (!allowDespawnedNpcs && context != null && context.showErrors()) {
Debug.echoDebug(context, "NPC '" + string + "' is not spawned, errors may follow!");
}
return new EntityTag(npc);
}
}
else {
if (context == null || context.debug || CoreConfiguration.debugOverride) {
Debug.echoError("NPC '" + string + "' does not exist!");
}
}
}
// Player entity
else if (string.startsWith("p@")) {
PlayerTag returnable = PlayerTag.valueOf(string, context);
if (returnable != null && returnable.isOnline()) {
return new EntityTag(returnable.getPlayerEntity());
}
else if (context == null || context.showErrors()) {
Debug.echoError("Invalid Player! '" + string + "' could not be found. Has the player logged off?");
}
}
if (ScriptRegistry.containsScript(string, EntityScriptContainer.class)) {
// Construct a new custom unspawned entity from script
EntityTag entity = ScriptRegistry.getScriptContainerAs(string, EntityScriptContainer.class).getEntityFrom();
entity.uuid = id;
return entity;
}
List<String> data = CoreUtilities.split(string, ',');
// Handle custom DenizenEntityTypes
DenizenEntityType type = DenizenEntityType.getByName(data.get(0));
if (type != null && type.getBukkitEntityType() != EntityType.UNKNOWN) {
EntityTag entity = new EntityTag(type, data.size() > 1 ? data.get(1) : null);
entity.uuid = id;
return entity;
}
try {
UUID entityID = id != null ? id : UUID.fromString(string);
Entity entity = getEntityForID(entityID);
if (entity != null) {
return new EntityTag(entity);
}
return null;
}
catch (Exception ex) {
// DO NOTHING
}
if (context == null || context.showErrors()) {
Debug.log("valueOf EntityTag returning null: " + string);
}
return null;
}
public static Entity getEntityForID(UUID id) {
if (rememberedEntities.containsKey(id)) {
return rememberedEntities.get(id);
}
for (World world : Bukkit.getWorlds()) {
Entity entity = NMSHandler.entityHelper.getEntity(world, id);
if (entity != null) {
return entity;
}
}
return null;
}
public static boolean matches(String arg) {
// Accept anything that starts with a valid entity object identifier.
if (arg.startsWith("n@") || arg.startsWith("e@") || arg.startsWith("p@")) {
return true;
}
// No longer picky about e@.. let's remove it from the arg
arg = CoreUtilities.toUpperCase(arg.replace("e@", ""));
// Allow 'random'
if (arg.equals("RANDOM")) {
return true;
}
// Allow any entity script
if (ScriptRegistry.containsScript(arg, EntityScriptContainer.class)) {
return true;
}
// Check first word with a valid entity_type (other groups are datas used in constructors)
if (DenizenEntityType.isRegistered(CoreUtilities.split(arg, ',').get(0))) {
return true;
}
// No luck otherwise!
return false;
}
/////////////////////
// CONSTRUCTORS
//////////////////
public EntityTag(Entity entity) {
if (entity != null) {
this.entity = entity;
entityScript = EntityScriptHelper.getEntityScript(entity);
this.uuid = entity.getUniqueId();
this.entity_type = DenizenEntityType.getByEntity(entity);
if (isCitizensNPC(entity)) {
this.npc = getNPCFrom(entity);
}
}
else {
Debug.echoError("Entity referenced is null!");
}
}
public EntityTag(EntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = DenizenEntityType.getByName(entityType.name());
}
else {
Debug.echoError("Entity_type referenced is null!");
}
}
public EntityTag(EntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}
public EntityTag(EntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = DenizenEntityType.getByName(entityType.name());
this.data1 = data1;
}
else {
Debug.echoError("Entity_type referenced is null!");
}
}
public EntityTag(DenizenEntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
}
else {
Debug.echoError("DenizenEntityType referenced is null!");
}
}
public EntityTag(DenizenEntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}
public EntityTag(DenizenEntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data1 = data1;
}
else {
Debug.echoError("DenizenEntityType referenced is null!");
}
}
public EntityTag(NPCTag npc) {
if (Depends.citizens == null) {
return;
}
if (npc != null) {
this.npc = npc;
if (npc.isSpawned()) {
this.entity = npc.getEntity();
this.entity_type = DenizenEntityType.getByName(npc.getEntityType().name());
this.uuid = entity.getUniqueId();
}
}
else {
Debug.echoError("NPC referenced is null!");
}
}
@Override
public EntityTag duplicate() {
if (isUnique()) {
return this;
}
try {
EntityTag copy = (EntityTag) clone();
if (copy.mechanisms != null) {
copy.mechanisms = new ArrayList<>(copy.mechanisms);
}
return copy;
}
catch (CloneNotSupportedException ex) {
Debug.echoError(ex);
return null;
}
}
@Override
public AbstractFlagTracker getFlagTracker() {
if (isCitizensNPC()) {
return getDenizenNPC().getFlagTracker();
}
else if (isPlayer()) {
return getDenizenPlayer().getFlagTracker();
}
Entity ent = getBukkitEntity();
if (ent != null) {
return new DataPersistenceFlagTracker(ent);
}
else {
// TODO: Warning?
return null;
}
}
@Override
public String getReasonNotFlaggable() {
if (!isSpawned() || getBukkitEntity() == null) {
return "the entity is not spawned";
}
return "unknown reason - something went wrong";
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
if (CoreConfiguration.skipAllFlagCleanings) {
return;
}
if (cleanRateProtect + 60000 > DenizenCore.serverTimeMillis) {
tracker.doTotalClean();
cleanRateProtect = DenizenCore.serverTimeMillis;
}
}
@Override
public boolean isTruthy() {
return isSpawnedOrValidForTag();
}
public Entity entity = null;
public long cleanRateProtect = -60000;
public DenizenEntityType entity_type = null;
private String data1 = null;
private NPCTag npc = null;
public UUID uuid = null;
private String entityScript = null;
public boolean isFake = false;
public boolean isFakeValid = false;
public DenizenEntityType getEntityType() {
return entity_type;
}
public EntityType getBukkitEntityType() {
return entity_type.getBukkitEntityType();
}
public void setEntityScript(String entityScript) {
this.entityScript = entityScript;
}
public String getEntityScript() {
return entityScript;
}
public UUID getUUID() {
if (uuid == null && entity != null) {
uuid = entity.getUniqueId();
}
return uuid;
}
@Override
public EntityTag getDenizenEntity() {
return this;
}
public EntityFormObject getDenizenObject() {
if (entity == null && npc == null) {
return this;
}
if (isCitizensNPC()) {
return getDenizenNPC();
}
else if (isPlayer()) {
return new PlayerTag(getPlayer());
}
else {
return this;
}
}
public Entity getBukkitEntity() {
if (uuid != null && (entity == null || !entity.isValid())) {
if (!isFake) {
Entity backup = Bukkit.getEntity(uuid);
if (backup != null) {
entity = backup;
}
}
}
return entity;
}
public LivingEntity getLivingEntity() {
if (entity instanceof LivingEntity) {
return (LivingEntity) entity;
}
else {
return null;
}
}
public boolean isLivingEntity() {
return entity instanceof LivingEntity;
}
public boolean isLivingEntityType() {
if (getBukkitEntity() == null && entity_type != null) {
return entity_type.getBukkitEntityType().isAlive();
}
return entity instanceof LivingEntity;
}
public boolean isMonsterType() {
if (getBukkitEntity() == null && entity_type != null) {
return Monster.class.isAssignableFrom(entity_type.getBukkitEntityType().getEntityClass());
}
return getBukkitEntity() instanceof Monster;
}
public boolean isMobType() {
if (getBukkitEntity() == null && entity_type != null) {
return Mob.class.isAssignableFrom(entity_type.getBukkitEntityType().getEntityClass());
}
return getBukkitEntity() instanceof Mob;
}
public boolean isAnimalType() {
if (getBukkitEntity() == null && entity_type != null) {
return Animals.class.isAssignableFrom(entity_type.getBukkitEntityType().getEntityClass());
}
return getBukkitEntity() instanceof Animals;
}
public boolean hasInventory() {
return getBukkitEntity() instanceof InventoryHolder || isCitizensNPC();
}
public NPCTag getDenizenNPC() {
if (npc != null) {
return npc;
}
else {
return getNPCFrom(entity);
}
}
public boolean isNPC() {
return npc != null || isNPC(entity);
}
public boolean isCitizensNPC() {
return npc != null || isCitizensNPC(entity);
}
public Player getPlayer() {
if (isPlayer()) {
return (Player) entity;
}
else {
return null;
}
}
public PlayerTag getDenizenPlayer() {
if (isPlayer()) {
return new PlayerTag(getPlayer());
}
else {
return null;
}
}
public boolean isPlayer() {
if (entity == null) {
return entity_type.getBukkitEntityType() == EntityType.PLAYER && npc == null;
}
return entity instanceof Player && !isNPC();
}
public Projectile getProjectile() {
return (Projectile) entity;
}
public boolean isProjectile() {
return entity instanceof Projectile;
}
public EntityTag getShooter() {
if (getBukkitEntity() instanceof TNTPrimed) {
Entity source = ((TNTPrimed) getBukkitEntity()).getSource();
if (source != null) {
return new EntityTag(source);
}
}
else if (isProjectile()) {
ProjectileSource shooter = getProjectile().getShooter();
if (shooter instanceof Entity) {
return new EntityTag((Entity) shooter);
}
}
return null;
}
public void setShooter(EntityTag shooter) {
if (getBukkitEntity() instanceof TNTPrimed) {
((TNTPrimed) getBukkitEntity()).setSource(shooter.getBukkitEntity());
}
else if (isProjectile() && shooter.isLivingEntity()) {
getProjectile().setShooter(shooter.getLivingEntity());
}
}
public boolean hasShooter() {
return getShooter() != null;
}
public Inventory getBukkitInventory() {
if (hasInventory()) {
if (!isCitizensNPC()) {
return ((InventoryHolder) getBukkitEntity()).getInventory();
}
}
return null;
}
public InventoryTag getInventory() {
return hasInventory() ? isCitizensNPC() ? getDenizenNPC().getDenizenInventory() : InventoryTag.mirrorBukkitInventory(getBukkitInventory()) : null;
}
public String getName() {
if (isCitizensNPC()) {
return getDenizenNPC().getCitizen().getName();
}
if (entity instanceof FakePlayer) {
return ((FakePlayer) entity).getFullName();
}
if (entity instanceof Player) {
return entity.getName();
}
String customName = entity == null ? null : entity.getCustomName();
if (customName != null) {
return customName;
}
return entity_type.getName();
}
public ListTag getEquipment() {
ItemStack[] equipment = getLivingEntity().getEquipment().getArmorContents();
ListTag equipmentList = new ListTag();
for (ItemStack item : equipment) {
equipmentList.addObject(new ItemTag(item));
}
return equipmentList;
}
public boolean isGeneric() {
return !isUnique();
}
@Override
public LocationTag getLocation() {
Entity entity = getBukkitEntity();
if (entity != null) {
return new LocationTag(entity.getLocation());
}
return null;
}
public LocationTag getEyeLocation() {
Entity entity = getBukkitEntity();
if (entity == null) {
return null;
}
if (isPlayer()) {
return new LocationTag(getPlayer().getEyeLocation());
}
else if (!isGeneric() && isLivingEntity()) {
return new LocationTag(getLivingEntity().getEyeLocation());
}
else if (!isGeneric()) {
return new LocationTag(entity.getLocation());
}
return null;
}
public Location getTargetBlockSafe(Set<Material> mats, int range) {
try {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
return getLivingEntity().getTargetBlock(mats, range).getLocation();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public Vector getVelocity() {
Entity entity = getBukkitEntity();
if (entity == null) {
return null;
}
return entity.getVelocity();
}
public void setVelocity(Vector vector) {
Entity entity = getBukkitEntity();
if (entity == null) {
return;
}
entity.setVelocity(vector);
}
public World getWorld() {
Entity entity = getBukkitEntity();
if (entity == null) {
return null;
}
return entity.getWorld();
}
public void spawnAt(Location location) {
spawnAt(location, TeleportCause.PLUGIN, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
public void spawnAt(Location location, TeleportCause cause, CreatureSpawnEvent.SpawnReason reason) {
if (location.getWorld() == null) {
Debug.echoError("Cannot teleport or spawn entity at location '" + new LocationTag(location) + "' because it is missing a world.");
return;
}
// If the entity is already spawned, teleport it.
if (isCitizensNPC() || (isUnique() && entity != null)) {
teleport(location, cause);
return;
}
else if (entity_type == null) {
Debug.echoError("Cannot spawn a null EntityTag!");
return;
}
if (entity_type.getBukkitEntityType() == EntityType.PLAYER && !entity_type.isCustom()) {
if (Depends.citizens == null) {
Debug.echoError("Cannot spawn entity of type PLAYER!");
return;
}
NPCTag npc = new NPCTag(net.citizensnpcs.api.CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, data1));
npc.getCitizen().spawn(location);
entity = npc.getEntity();
}
else if (entity_type.getBukkitEntityType() == EntityType.FALLING_BLOCK) {
MaterialTag material = null;
if (data1 != null && MaterialTag.matches(data1)) {
material = MaterialTag.valueOf(data1, CoreUtilities.basicContext);
// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data1.equalsIgnoreCase("RANDOM") &&
((!material.getMaterial().isBlock()) ||
material.getMaterial() == Material.AIR ||
material.getMaterial() == Material.NETHER_PORTAL ||
material.getMaterial() == Material.END_PORTAL)) {
material = MaterialTag.valueOf(data1, CoreUtilities.basicContext);
}
}
else {
for (Mechanism mech : mechanisms) {
if (mech.getName().equals("fallingblock_type")) {
material = mech.valueAsType(MaterialTag.class);
mechanisms.remove(mech);
break;
}
}
}
// If material is null or not a block, default to SAND
if (material == null || !material.getMaterial().isBlock() || !material.hasModernData()) {
material = new MaterialTag(Material.SAND);
}
// This is currently the only way to spawn a falling block
entity = location.getWorld().spawnFallingBlock(location, material.getModernData());
}
else if (entity_type.getBukkitEntityType() == EntityType.PAINTING) {
entity = entity_type.spawnNewEntity(location, mechanisms, entityScript, reason);
location = location.clone();
Painting painting = (Painting) entity;
Art art = null;
BlockFace face = null;
try {
for (Mechanism mech : mechanisms) {
if (mech.getName().equals("painting")) {
art = Art.valueOf(mech.getValue().asString().toUpperCase());
}
else if (mech.getName().equals("rotation")) {
face = BlockFace.valueOf(mech.getValue().asString().toUpperCase());
}
}
}
catch (Exception ex) {
// ignore
}
if (art != null && face != null) { // Paintings are the worst
if (art.getBlockHeight() % 2 == 0) {
location.subtract(0, 1, 0);
}
if (art.getBlockWidth() % 2 == 0) {
if (face == BlockFace.WEST) {
location.subtract(0, 0, 1);
}
else if (face == BlockFace.SOUTH) {
location.subtract(1, 0, 0);
}
}
painting.teleport(location);
painting.setFacingDirection(face, true);
painting.setArt(art, true);
}
}
else {
entity = entity_type.spawnNewEntity(location, mechanisms, entityScript, reason);
}
if (entity == null) {
if (!new LocationTag(location).isChunkLoaded()) {
Debug.echoError("Error spawning entity - tried to spawn in an unloaded chunk.");
}
else {
Debug.echoError("Error spawning entity - bad entity type, or blocked by another plugin?");
}
return;
}
uuid = entity.getUniqueId();
if (entityScript != null) {
EntityScriptHelper.setEntityScript(entity, entityScript);
}
for (Mechanism mechanism : mechanisms) {
safeAdjust(new Mechanism(mechanism.getName(), mechanism.value, mechanism.context));
}
mechanisms.clear();
}
public boolean isSpawnedOrValidForTag() {
if (isFake) {
return true;
}
if (entity == null) { // Note: this breaks thread-patch for entities that need revalidating
if (uuid == null) {
return false;
}
return isValid() || rememberedEntities.containsKey(uuid);
}
NMSHandler.chunkHelper.changeChunkServerThread(entity.getWorld());
try {
return isValid() || rememberedEntities.containsKey(entity.getUniqueId());
}
finally {
NMSHandler.chunkHelper.restoreServerThread(entity.getWorld());
}
}
public boolean isSpawned() {
return isValid();
}
public boolean isValid() {
Entity entity = getBukkitEntity();
return entity != null && (entity.isValid() || (isFake && isFakeValid));
}
public void remove() {
entity.remove();
}
public void teleport(Location location) {
teleport(location, TeleportCause.PLUGIN);
}
public void teleport(Location location, TeleportCause cause) {
if (location.getWorld() == null) {
Debug.echoError("Cannot teleport or spawn entity at location '" + new LocationTag(location) + "' because it is missing a world.");
return;
}
if (isCitizensNPC()) {
if (getDenizenNPC().getCitizen().isSpawned()) {
getDenizenNPC().getCitizen().teleport(location, cause);
}
else {
if (getDenizenNPC().getCitizen().spawn(location)) {
entity = getDenizenNPC().getCitizen().getEntity();
uuid = getDenizenNPC().getCitizen().getEntity().getUniqueId();
}
else {
if (new LocationTag(location).isChunkLoaded()) {
Debug.echoError("Error spawning NPC - tried to spawn in an unloaded chunk.");
}
else {
Debug.echoError("Error spawning NPC - blocked by plugin");
}
}
}
}
else if (isFake) {
NMSHandler.entityHelper.snapPositionTo(entity, location.toVector());
NMSHandler.entityHelper.look(entity, location.getYaw(), location.getPitch());
}
else {
getBukkitEntity().teleport(location, cause);
if (entity.getWorld().equals(location.getWorld())) { // Force the teleport through (for things like mounts)
NMSHandler.entityHelper.teleport(entity, location);
}
}
}
/**
* Make this entity target another living entity, attempting both