-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
dEntity.java
2500 lines (2152 loc) · 86.1 KB
/
dEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.objects.properties.entity.EntityAge;
import net.aufdemrand.denizen.objects.properties.entity.EntityColor;
import net.aufdemrand.denizen.objects.properties.entity.EntityTame;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptHelper;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizen.utilities.entity.CraftFakePlayer;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.CitizensAPI;
import net.minecraft.server.v1_8_R3.*;
import org.bukkit.*;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftAnimals;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.event.entity.EntityTargetEvent;
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.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dEntity implements dObject, Adjustable {
/////////////////////
// STATIC METHODS
/////////////////
private static final Map<UUID, Entity> rememberedEntities = new HashMap<UUID, Entity>();
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) {
return entity != null && Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(entity);
}
public static dNPC getNPCFrom(Entity entity) {
if (isCitizensNPC(entity))
return dNPC.fromEntity(entity);
else
return null;
}
public static boolean isPlayer(Entity entity) {
return entity != null && entity instanceof Player && !isNPC(entity);
}
public static dPlayer getPlayerFrom(Entity entity) {
if (isPlayer(entity))
return dPlayer.mirrorBukkitPlayer((Player) entity);
else
return null;
}
public dItem getItemInHand() {
if (isLivingEntity()) {
return new dItem(getLivingEntity().getEquipment().getItemInHand().clone());
}
return null;
}
//////////////////
// OBJECT FETCHER
////////////////
public static dEntity valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets a dEntity Object from a string form. </br>
* </br>
* Unique dEntities: </br>
* n@13 will return the entity object of NPC 13 </br>
* e@5884 will return the entity object for the entity with the entityid of 5884 </br>
* e@jimmys_pet will return the saved entity object for the id 'jimmys pet' </br>
* p@aufdemrand will return the entity object for aufdemrand </br>
* </br>
* New dEntities: </br>
* zombie will return an unspawned Zombie dEntity </br>
* super_creeper will return an unspawned custom 'Super_Creeper' dEntity </br>
*
* @param string the string or dScript argument String
* @return a dEntity, or null
*/
@Fetchable("e")
public static dEntity valueOf(String string, TagContext context) {
if (string == null) return null;
Matcher m;
///////
// Handle objects with properties through the object fetcher
m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
return ObjectFetcher.getObjectFrom(dEntity.class, string, context);
}
// Choose a random entity type if "RANDOM" is used
if (string.equalsIgnoreCase("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 dEntity(DenizenEntityType.getByName(randomType.name()), "RANDOM");
}
///////
// Match @object format
// Make sure string matches what this interpreter can accept.
m = entity_by_id.matcher(string);
if (m.matches()) {
String entityGroup = m.group(1).toUpperCase();
// NPC entity
if (entityGroup.matches("N@")) {
dNPC npc = dNPC.valueOf(string);
if (npc != null)
return new dEntity(npc);
else dB.echoError("NPC '" + string
+ "' does not exist!");
}
// Player entity
else if (entityGroup.matches("P@")) {
LivingEntity returnable = dPlayer.valueOf(m.group(2)).getPlayerEntity();
if (returnable != null) {
return new dEntity(returnable);
}
else if (context == null || context.debug) {
dB.echoError("Invalid Player! '" + m.group(2)
+ "' could not be found. Has the player logged off?");
}
}
// Assume entity
else {
try {
UUID entityID = UUID.fromString(m.group(2));
Entity entity = getEntityForID(entityID);
if (entity != null) return new dEntity(entity);
return null;
}
catch (Exception ex) {
ex.getCause(); // DO NOTHING
}
// else if (isSaved(m.group(2)))
// return getSaved(m.group(2));
}
}
string = string.replace("e@", "");
////////
// Match Custom Entity
if (ScriptRegistry.containsScript(string, EntityScriptContainer.class)) {
// Construct a new custom unspawned entity from script
return ScriptRegistry.getScriptContainerAs(string, EntityScriptContainer.class).getEntityFrom();
}
////////
// Match Entity_Type
m = entity_with_data.matcher(string);
if (m.matches()) {
String data1 = null;
String data2 = null;
if (m.group(2) != null) {
data1 = m.group(2);
}
if (m.group(3) != null) {
data2 = m.group(3);
}
// Handle custom DenizenEntityTypes
if (DenizenEntityType.isRegistered(m.group(1))) {
return new dEntity(DenizenEntityType.getByName(m.group(1)), data1, data2);
}
}
if (context == null || context.debug) {
dB.log("valueOf dEntity returning null: " + string);
}
return null;
}
public static Entity getEntityForID(UUID id) {
if (rememberedEntities.containsKey(id))
return rememberedEntities.get(id);
for (World world : Bukkit.getWorlds()) {
net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftWorld) world).getHandle().getEntity(id);
// Make sure the nmsEntity is valid, to prevent unpleasant errors
if (nmsEntity != null) {
return nmsEntity.getBukkitEntity();
}
}
return null;
}
final static Pattern entity_by_id = Pattern.compile("(n@|e@|p@)(.+)",
Pattern.CASE_INSENSITIVE);
final static Pattern entity_with_data = Pattern.compile("(\\w+),?(\\w+)?,?(\\w+)?",
Pattern.CASE_INSENSITIVE);
public static boolean matches(String arg) {
// Accept anything that starts with a valid entity object identifier.
Matcher m;
m = entity_by_id.matcher(arg);
if (m.matches()) {
return true;
}
// No longer picky about e@.. let's remove it from the arg
arg = arg.replace("e@", "").toUpperCase();
// Allow 'random'
if (arg.equals("RANDOM")) {
return true;
}
// Allow any entity script
if (ScriptRegistry.containsScript(arg, EntityScriptContainer.class)) {
return true;
}
// Use regex to make some matcher groups
m = entity_with_data.matcher(arg);
if (m.matches()) {
// Check first word with a valid entity_type (other groups are datas used in constructors)
if (DenizenEntityType.isRegistered(m.group(1))) {
return true;
}
}
// No luck otherwise!
return false;
}
/////////////////////
// CONSTRUCTORS
//////////////////
public dEntity(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 dB.echoError("Entity referenced is null!");
}
@Deprecated
public dEntity(EntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = DenizenEntityType.getByName(entityType.name());
}
else dB.echoError("Entity_type referenced is null!");
}
@Deprecated
public dEntity(EntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}
@Deprecated
public dEntity(EntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = DenizenEntityType.getByName(entityType.name());
this.data1 = data1;
}
else dB.echoError("Entity_type referenced is null!");
}
@Deprecated
public dEntity(EntityType entityType, String data1, String data2) {
if (entityType != null) {
this.entity = null;
this.entity_type = DenizenEntityType.getByName(entityType.name());
this.data1 = data1;
this.data2 = data2;
}
else dB.echoError("Entity_type referenced is null!");
}
public dEntity(DenizenEntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
}
else dB.echoError("DenizenEntityType referenced is null!");
}
public dEntity(DenizenEntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}
public dEntity(DenizenEntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data1 = data1;
}
else dB.echoError("DenizenEntityType referenced is null!");
}
public dEntity(DenizenEntityType entityType, String data1, String data2) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data1 = data1;
this.data2 = data2;
}
else dB.echoError("DenizenEntityType referenced is null!");
}
public dEntity(dNPC 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 dB.echoError("NPC referenced is null!");
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private Entity entity = null;
private DenizenEntityType entity_type = null;
private String data1 = null;
private String data2 = null;
private DespawnedEntity despawned_entity = null;
private dNPC npc = null;
private UUID uuid = null;
private String entityScript = null;
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;
}
/**
* Returns the unique UUID of this entity
*
* @return The UUID
*/
public UUID getUUID() {
return uuid;
}
public String getSaveName() {
String baseID = uuid.toString().toUpperCase().replace("-", "");
return baseID.substring(0, 2) + "." + baseID;
}
/**
* Get the dObject that most accurately describes this entity,
* useful for automatically saving dEntities to contexts as
* dNPCs and dPlayers
*
* @return The dObject
*/
public dObject getDenizenObject() {
if (entity == null) return null;
if (isCitizensNPC())
return getDenizenNPC();
else if (isPlayer())
return new dPlayer(getPlayer());
else
return this;
}
/**
* Get the Bukkit entity corresponding to this dEntity
*
* @return the underlying Bukkit entity
*/
public Entity getBukkitEntity() {
return entity;
}
/**
* Get the living entity corresponding to this dEntity
*
* @return The living entity
*/
public LivingEntity getLivingEntity() {
if (entity instanceof LivingEntity)
return (LivingEntity) entity;
else return null;
}
/**
* Check whether this dEntity is a living entity
*
* @return true or false
*/
public boolean isLivingEntity() {
return (entity instanceof LivingEntity);
}
public boolean hasInventory() {
return getBukkitEntity() instanceof InventoryHolder || isCitizensNPC();
}
/**
* Get the dNPC corresponding to this dEntity
*
* @return The dNPC
*/
public dNPC getDenizenNPC() {
if (npc != null)
return npc;
else
return getNPCFrom(entity);
}
/**
* Check whether this dEntity is an NPC
*
* @return true or false
*/
public boolean isNPC() {
return npc != null || isNPC(entity);
}
public boolean isCitizensNPC() {
return npc != null || isCitizensNPC(entity);
}
/**
* Get the Player corresponding to this dEntity
*
* @return The Player
*/
public Player getPlayer() {
if (isPlayer())
return (Player) entity;
else
return null;
}
/**
* Get the dPlayer corresponding to this dEntity
*
* @return The dPlayer
*/
public dPlayer getDenizenPlayer() {
if (isPlayer())
return new dPlayer(getPlayer());
else
return null;
}
/**
* Check whether this dEntity is a Player
*
* @return true or false
*/
public boolean isPlayer() {
return entity instanceof Player && !isNPC();
}
/**
* Get this dEntity as a Projectile
*
* @return The Projectile
*/
public Projectile getProjectile() {
return (Projectile) entity;
}
/**
* Check whether this dEntity is a Projectile
*
* @return true or false
*/
public boolean isProjectile() {
return entity instanceof Projectile;
}
/**
* Get this Projectile entity's shooter
*
* @return A dEntity of the shooter
*/
public dEntity getShooter() {
if (hasShooter())
return new dEntity((LivingEntity) getProjectile().getShooter());
else
return null;
}
/**
* Set this Projectile entity's shooter
*/
public void setShooter(dEntity shooter) {
if (isProjectile() && shooter.isLivingEntity())
getProjectile().setShooter(shooter.getLivingEntity());
}
/**
* Check whether this entity has a shooter.
*
* @return true or false
*/
public boolean hasShooter() {
return isProjectile() && getProjectile().getShooter() != null && getProjectile().getShooter() instanceof LivingEntity;
// TODO: Handle other shooter source thingy types
}
public Inventory getBukkitInventory() {
if (hasInventory()) {
if (!isCitizensNPC())
return ((InventoryHolder) getBukkitEntity()).getInventory();
}
return null;
}
/**
* Returns this entity's dInventory.
*
* @return the entity's dInventory
*/
public dInventory getInventory() {
return hasInventory() ? isCitizensNPC() ? getDenizenNPC().getDenizenInventory()
: new dInventory(getBukkitInventory()) : null;
}
public String getName() {
if (isCitizensNPC()) {
return getDenizenNPC().getCitizen().getName();
}
if (entity instanceof CraftFakePlayer) {
return ((CraftFakePlayer) entity).getFullName();
}
if (entity instanceof Player) {
return ((Player) entity).getName();
}
String customName = entity.getCustomName();
if (customName != null) {
return customName;
}
return entity_type.getName();
}
/**
* Returns this entity's equipment
*
* @return the entity's equipment
*/
public dList getEquipment() {
if (isCitizensNPC() || isPlayer()) {
return getInventory().getEquipment(); // TODO: Is this part needed?
}
ItemStack[] equipment = getLivingEntity().getEquipment().getArmorContents();
dList equipmentList = new dList();
for (ItemStack item : equipment) {
equipmentList.add(new dItem(item).identify());
}
return equipmentList;
}
/**
* Whether this entity identifies as a generic
* entity type, for instance "e@cow", instead of
* a spawned entity
*
* @return true or false
*/
public boolean isGeneric() {
return !isUnique();
}
/**
* Get the location of this entity
*
* @return The Location
*/
public dLocation getLocation() {
if (isUnique() && entity != null) {
return new dLocation(entity.getLocation());
}
return null;
}
/**
* Get the eye location of this entity
*
* @return The location
*/
public dLocation getEyeLocation() {
if (isPlayer()) {
return new dLocation(getPlayer().getEyeLocation());
}
else if (!isGeneric() && isLivingEntity()) {
return new dLocation(getLivingEntity().getEyeLocation());
}
else if (!isGeneric()) {
return new dLocation(getBukkitEntity().getLocation());
}
return null;
}
/**
* Gets the velocity of this entity
*
* @return The velocity's vector
*/
public Vector getVelocity() {
if (!isGeneric()) {
return entity.getVelocity();
}
return null;
}
/**
* Sets the velocity of this entity
*/
public void setVelocity(Vector vector) {
if (!isGeneric()) {
if (entity instanceof WitherSkull) {
((WitherSkull) entity).setDirection(vector);
}
else {
entity.setVelocity(vector);
}
}
}
/**
* Gets the world of this entity
*
* @return The entity's world
*/
public World getWorld() {
if (!isGeneric()) {
return entity.getWorld();
}
return null;
}
public void spawnAt(Location location) {
// If the entity is already spawned, teleport it.
if (isCitizensNPC()) {
if (getDenizenNPC().getCitizen().isSpawned())
getDenizenNPC().getCitizen().teleport(location, TeleportCause.PLUGIN);
else {
getDenizenNPC().getCitizen().spawn(location);
entity = getDenizenNPC().getCitizen().getEntity();
uuid = getDenizenNPC().getCitizen().getEntity().getUniqueId();
}
}
else if (entity != null && isUnique()) entity.teleport(location);
else {
if (entity_type != null) {
if (despawned_entity != null) {
// If entity had a custom_script, use the script to rebuild the base entity.
if (despawned_entity.custom_script != null) {
// TODO: Build entity from custom script
}
// Else, use the entity_type specified/remembered
else
entity = entity_type.spawnNewEntity(location, mechanisms);
getLivingEntity().teleport(location);
getLivingEntity().getEquipment().setArmorContents(despawned_entity.equipment);
getLivingEntity().setHealth(despawned_entity.health);
despawned_entity = null;
}
else {
org.bukkit.entity.Entity ent = null;
if (entity_type.getName().equals("PLAYER")) {
if (Depends.citizens == null) {
dB.echoError("Cannot spawn entity of type PLAYER!");
return;
}
else {
dNPC npc = new dNPC(net.citizensnpcs.api.CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, data1));
npc.getCitizen().spawn(location);
entity = npc.getEntity();
uuid = entity.getUniqueId();
}
}
else if (entity_type.getName().equals("FALLING_BLOCK")) {
Material material = null;
if (data1 != null && dMaterial.matches(data1)) {
material = dMaterial.valueOf(data1).getMaterial();
// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data1.equalsIgnoreCase("RANDOM") &&
((!material.isBlock()) ||
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {
material = dMaterial.valueOf(data1).getMaterial();
}
}
// If material is null or not a block, default to SAND
if (material == null || (!material.isBlock())) {
material = Material.SAND;
}
byte materialData = 0;
// Get special data value from data2 if it is a valid integer
if (data2 != null && aH.matchesInteger(data2)) {
materialData = (byte) aH.getIntegerFrom(data2);
}
// This is currently the only way to spawn a falling block
ent = location.getWorld().spawnFallingBlock(location, material, materialData);
entity = ent;
uuid = entity.getUniqueId();
}
else {
ent = entity_type.spawnNewEntity(location, mechanisms);
entity = ent;
if (entity == null) {
return;
}
uuid = entity.getUniqueId();
if (entityScript != null)
EntityScriptHelper.setEntityScript(entity, entityScript);
if (entity_type.getName().equals("PIG_ZOMBIE")) {
// Give pig zombies golden swords by default, unless data2 specifies
// a different weapon
if (!dItem.matches(data1)) {
data1 = "gold_sword";
}
((PigZombie) entity).getEquipment()
.setItemInHand(dItem.valueOf(data1).getItemStack());
}
else if (entity_type.getName().equals("SKELETON")) {
// Give skeletons bows by default, unless data2 specifies
// a different weapon
if (!dItem.matches(data2)) {
data2 = "bow";
}
((Skeleton) entity).getEquipment()
.setItemInHand(dItem.valueOf(data2).getItemStack());
}
// If there is some special subtype data associated with this dEntity,
// use the setSubtype method to set it in a clean, object-oriented
// way that uses reflection
//
// Otherwise, just use entity-specific methods manually
if (data1 != null) {
// TODO: Discourage usage of + delete the below (Use properties instead!)
// TODO: Remove in 1.0
try {
// Allow creepers to be powered - replaced by EntityPowered
if (ent instanceof Creeper && data1.equalsIgnoreCase("POWERED")) {
((Creeper) entity).setPowered(true);
}
// Allow setting of blocks held by endermen - replaced by EntityItem
else if (ent instanceof Enderman && dMaterial.matches(data1)) {
((Enderman) entity).setCarriedMaterial(dMaterial.valueOf(data1).getMaterialData());
}
// Allow setting of horse variants and colors - replaced by EntityColor
else if (ent instanceof Horse) {
setSubtype("org.bukkit.entity.Horse", "org.bukkit.entity.Horse$Variant", "setVariant", data1);
if (data2 != null) {
setSubtype("org.bukkit.entity.Horse", "org.bukkit.entity.Horse$Color", "setColor", data2);
}
}
// Allow setting of ocelot types - replaced by EntityColor
else if (ent instanceof Ocelot) {
setSubtype("org.bukkit.entity.Ocelot", "org.bukkit.entity.Ocelot$Type", "setCatType", data1);
}
// Allow setting of sheep colors - replaced by EntityColor
else if (ent instanceof Sheep) {
setSubtype("org.bukkit.entity.Sheep", "org.bukkit.DyeColor", "setColor", data1);
}
// Allow setting of skeleton types - replaced by EntitySkeleton
else if (ent instanceof Skeleton) {
setSubtype("org.bukkit.entity.Skeleton", "org.bukkit.entity.Skeleton$SkeletonType", "setSkeletonType", data1);
}
// Allow setting of slime sizes - replaced by EntitySize
else if (ent instanceof Slime && aH.matchesInteger(data1)) {
((Slime) entity).setSize(aH.getIntegerFrom(data1));
}
// Allow setting of villager professions - replaced by EntityProfession
else if (ent instanceof Villager) {
setSubtype("org.bukkit.entity.Villager", "org.bukkit.entity.Villager$Profession", "setProfession", data1);
}
}
catch (Exception e) {
dB.echoError("Error setting custom entity data.");
dB.echoError(e);
}
}
}
}
}
else dB.echoError("Cannot spawn a null dEntity!");
if (!isUnique()) {
dB.echoError("Error spawning entity - bad entity type, blocked by another plugin, or tried to spawn in an unloaded chunk?");
return;
}
for (Mechanism mechanism : mechanisms) {
adjust(mechanism);
}
mechanisms.clear();
}
}
public void despawn() {
despawned_entity = new DespawnedEntity(this);
getLivingEntity().remove();
}
public void respawn() {
if (despawned_entity != null)
spawnAt(despawned_entity.location);
else if (entity == null)
dB.echoError("Cannot respawn a null dEntity!");
}
public boolean isSpawned() {
return entity != null && isValid();
}
public boolean isValid() {
return entity != null && entity.isValid();
}
public void remove() {
EntityScriptHelper.unlinkEntity(entity);