/
dEntity.java
697 lines (538 loc) · 24.8 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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_5_R3.CraftWorld;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Villager;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dEntity implements dObject {
/////////////////////
// PATTERNS
/////////////////
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);
/////////////////////
// STATIC METHODS
/////////////////
public static Map<String, dEntity> uniqueObjects = new HashMap<String, dEntity>();
public static boolean isSaved(String id) {
return uniqueObjects.containsKey(id.toUpperCase());
}
public static boolean isSaved(dEntity entity) {
return uniqueObjects.containsValue(entity);
}
public static dEntity getSaved(String id) {
if (uniqueObjects.containsKey(id.toUpperCase()))
return uniqueObjects.get(id.toUpperCase());
else return null;
}
public static String getSaved(dEntity entity) {
for (Map.Entry<String, dEntity> i : uniqueObjects.entrySet())
if (i.getValue() == entity) return i.getKey();
return null;
}
public static void saveAs(dEntity entity, String id) {
if (entity == null) return;
uniqueObjects.put(id.toUpperCase(), entity);
}
public static void remove(String id) {
uniqueObjects.remove(id.toUpperCase());
}
//////////////////
// OBJECT FETCHER
////////////////
/**
* 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
*/
@ObjectFetcher("e")
public static dEntity valueOf(String string) {
if (string == null) return null;
// 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_DRAGON|FISHING_HOOK|ITEM_FRAME|LIGHTNING|PAINTING|PLAYER|UNKNOWN|WEATHER|WITHER)$") == true) {
randomType = EntityType.values()[Utilities.getRandom().nextInt(EntityType.values().length)];
}
return new dEntity(randomType, "RANDOM");
}
///////
// Match @object format
// Make sure string matches what this interpreter can accept.
Matcher m;
m = entity_by_id.matcher(string);
if (m.matches()) {
String entityGroup = m.group(1).toUpperCase();
// NPC entity
if (entityGroup.matches("N@")) {
NPC returnable = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(2)));
if (returnable != null) return new dEntity(returnable.getBukkitEntity());
else dB.echoError("Invalid NPC! '" + entityGroup
+ "' could not be found. Has it been despawned or killed?");
}
// Player entity
else if (entityGroup.matches("P@")) {
LivingEntity returnable = aH.getPlayerFrom(m.group(2)).getPlayerEntity();
if (returnable != null) return new dEntity(returnable);
else dB.echoError("Invalid Player! '" + entityGroup
+ "' could not be found. Has the player logged off?");
}
// Assume entity
else {
if (aH.matchesInteger(m.group(2))) {
int entityID = Integer.valueOf(m.group(2));
Entity entity = null;
for (World world : Bukkit.getWorlds()) {
entity = ((CraftWorld) world).getHandle().getEntity(entityID).getBukkitEntity();
if (entity != null) break;
}
if (entity != null) return new dEntity(entity);
}
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(m.group(0), EntityScriptContainer.class).getEntityFrom();
}
////////
// Match Entity_Type
m = entity_with_data.matcher(string);
String data = null;
if (m.matches()) {
if (m.group(2) != null) {
data = m.group(2).toUpperCase();
}
for (EntityType type : EntityType.values()) {
if (type.name().equalsIgnoreCase(m.group(1)))
// Construct a new 'vanilla' unspawned dEntity
return new dEntity(type, data);
}
}
dB.log("valueOf dEntity returning null: " + string);
return null;
}
public static boolean matches(String arg) {
Matcher m;
m = entity_by_id.matcher(arg);
if (m.matches()) return true;
arg = arg.replace("e@", "");
if (arg.equalsIgnoreCase("RANDOM"))
return true;
if (ScriptRegistry.containsScript(arg, EntityScriptContainer.class))
return true;
m = entity_with_data.matcher(arg);
if (m.matches()) {
for (EntityType type : EntityType.values())
if (type.name().equalsIgnoreCase(m.group(1))) return true;
}
return false;
}
/////////////////////
// CONSTRUCTORS
//////////////////
public dEntity(Entity entity) {
if (entity != null) {
this.entity = entity;
this.entity_type = entity.getType();
} else dB.echoError("Entity referenced is null!");
}
public dEntity(EntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
} else dB.echoError("Entity_type referenced is null!");
}
public dEntity(EntityType entityType, String data) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data = data;
} else dB.echoError("Entity_type referenced is null!");
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private Entity entity = null;
private EntityType entity_type = null;
private String data = null;
private DespawnedEntity despawned_entity = null;
public Entity getBukkitEntity() {
return entity;
}
public LivingEntity getLivingEntity() {
if (entity instanceof LivingEntity)
return (LivingEntity) entity;
else return null;
}
public boolean isLivingEntity() {
return (entity instanceof LivingEntity);
}
public void spawnAt(Location location) {
// If the entity is already spawned, teleport it.
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 = location.getWorld().spawnEntity(location, entity_type);
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.name().matches("FALLING_BLOCK")) {
Material material = null;
if (data != null && dMaterial.matches(data)) {
material = dMaterial.valueOf(data).getMaterial();
// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data.equals("RANDOM") &&
(material.isBlock() == false ||
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {
material = dMaterial.valueOf(data).getMaterial();
}
}
// If material is null or not a block, default to SAND
if (material == null || material.isBlock() == false) {
material = Material.SAND;
}
// This is currently the only way to spawn a falling block
ent = location.getWorld().spawnFallingBlock(location, material, (byte) 0);
entity = ent;
}
else {
ent = location.getWorld().spawnEntity(location, entity_type);
entity = ent;
// 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 (data != null) {
try {
// Allow creepers to be powered
if (ent instanceof Creeper && data.equalsIgnoreCase("POWERED")) {
((Creeper) entity).setPowered(true);
}
else if (ent instanceof Enderman && dMaterial.matches(data)) {
((Enderman) entity).setCarriedMaterial(dMaterial.valueOf(data).getMaterialData());
}
else if (ent instanceof Ocelot) {
setSubtype(Ocelot.class, "Type", "setCatType", data);
}
else if (ent instanceof Skeleton) {
setSubtype(Skeleton.class, "SkeletonType", "setSkeletonType", data);
}
else if (ent instanceof Slime && aH.matchesInteger(data)) {
((Slime) entity).setSize(aH.getIntegerFrom(data));
}
else if (ent instanceof Villager) {
setSubtype(Villager.class, "Profession", "setProfession", data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
else dB.echoError("Cannot spawn a null dEntity!");
}
}
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.echoDebug("Entity " + identify() + " is already spawned!");
else
dB.echoError("Cannot respawn a null dEntity!");
}
public boolean isSpawned() {
return entity != null;
}
public dEntity rememberAs(String id) {
dEntity.saveAs(this, id);
return this;
}
/**
* Set the subtype of this entity by using the chosen method and Enum from
* this Bukkit entity's class and:
* 1) using a random subtype if value is "RANDOM"
* 2) looping through the entity's subtypes until one matches the value string
*
* Example: setSubtype(Ocelot.class, "Type", "setCatType", "SIAMESE_CAT");
*
* @param entityClass The Bukkit entity class of the entity.
* @param typeName The name of the entity class' Enum with subtypes.
* @param method The name of the method used to set the subtype of this entity.
* @param value The value of the subtype.
*/
public void setSubtype (Class<? extends Entity> entityClass, String typeName, String method, String value)
throws Exception {
Class<?> typeClass = Class.forName(entityClass.getName() + "$" + typeName);
Object[] types = typeClass.getEnumConstants();
if (value.matches("RANDOM")) {
entityClass.getMethod(method, typeClass).invoke(entity, types[Utilities.getRandom().nextInt(types.length)]);
}
else {
for (Object type : types) {
if (type.toString().equalsIgnoreCase(value)) {
entityClass.getMethod(method, typeClass).invoke(entity, type);
break;
}
}
}
}
// Used to store some information about a livingEntity while it's despawned
private class DespawnedEntity {
Integer health = null;
Location location = null;
ItemStack[] equipment = null;
String custom_script = null;
public DespawnedEntity(dEntity entity) {
if (entity != null) {
// Save some important info to rebuild the entity
health = entity.getLivingEntity().getHealth();
location = entity.getLivingEntity().getLocation();
equipment = entity.getLivingEntity().getEquipment().getArmorContents();
if (CustomNBT.hasCustomNBT(entity.getLivingEntity(), "denizen-script-id"))
custom_script = CustomNBT.getCustomNBT(entity.getLivingEntity(), "denizen-script-id");
}
}
}
//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////
private String prefix = "Entity";
@Override
public String getType() {
return "dEntity";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dEntity setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return "<G>" + prefix + "='<Y>" + identify() + "<G>' ";
}
@Override
public String identify() {
// Check if entity is a Player or NPC
if (getBukkitEntity() != null) {
if (CitizensAPI.getNPCRegistry().isNPC(getBukkitEntity()))
return "n@" + CitizensAPI.getNPCRegistry().getNPC(getBukkitEntity()).getId();
else if (getBukkitEntity() instanceof Player)
return "p@" + ((Player) getBukkitEntity()).getName();
}
// Check if entity is a 'saved entity'
else if (isUnique())
return "e@" + getSaved(this);
// Check if entity is spawned, therefore having a bukkit entityId
else if (isSpawned())
return "e@" + getBukkitEntity().getEntityId();
// Check if an entity_type is available
else if (entity_type != null)
return "e@" + entity_type.name();
return "null";
}
@Override
public boolean isUnique() {
if (entity instanceof Player) return true;
if (CitizensAPI.getNPCRegistry().isNPC(entity)) return true;
return isSaved(this);
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
if (entity == null) {
dB.echoDebug("dEntity has returned null.");
return "null";
}
if (attribute.startsWith("custom_name")) {
if (getLivingEntity().getCustomName() == null) return "null";
return new Element(getLivingEntity().getCustomName()).getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("name")) {
if (CitizensAPI.getNPCRegistry().isNPC(entity))
return new Element(CitizensAPI.getNPCRegistry().getNPC(entity).getName())
.getAttribute(attribute.fulfill(1));
if (entity instanceof Player)
return new Element(((Player) entity).getName())
.getAttribute(attribute.fulfill(1));
return new Element(entity.getType().getName())
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("entity_type"))
return new Element(entity.getType().getName())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("custom_id")) {
if (CustomNBT.hasCustomNBT(getLivingEntity(), "denizen-script-id"))
return new dScript(CustomNBT.getCustomNBT(getLivingEntity(), "denizen-script-id"))
.getAttribute(attribute.fulfill(1));
else
return new Element(getBukkitEntity().getType().name())
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("location.cursor_on")) {
int range = attribute.getIntContext(2);
if (range < 1) range = 50;
return new dLocation(getLivingEntity().getTargetBlock(null, range).getLocation())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("location.standing_on"))
return new dLocation(entity.getLocation().add(0, -1, 0))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("location"))
return new dLocation(entity.getLocation())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("health.formatted")) {
int maxHealth = getLivingEntity().getMaxHealth();
if (attribute.hasContext(2))
maxHealth = attribute.getIntContext(2);
if ((float) getLivingEntity().getHealth() / maxHealth < .10)
return new Element("dying").getAttribute(attribute.fulfill(2));
else if ((float) getLivingEntity().getHealth() / maxHealth < .40)
return new Element("seriously wounded").getAttribute(attribute.fulfill(2));
else if ((float) getLivingEntity().getHealth() / maxHealth < .75)
return new Element("injured").getAttribute(attribute.fulfill(2));
else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element("scraped").getAttribute(attribute.fulfill(2));
else return new Element("healthy").getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("health.percentage")) {
int maxHealth = getLivingEntity().getMaxHealth();
if (attribute.hasContext(2))
maxHealth = attribute.getIntContext(2);
return new Element(String.valueOf(((float) getLivingEntity().getHealth() / maxHealth) * 100))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("health.max"))
return new Element(String.valueOf(getLivingEntity().getMaxHealth()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("health"))
return new Element(String.valueOf(getLivingEntity().getHealth()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_inside_vehicle"))
return new Element(String.valueOf(entity.isInsideVehicle()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("killer"))
return new dPlayer(getLivingEntity().getKiller())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("last_damage_cause"))
return new Element(String.valueOf(entity.getLastDamageCause().getCause().toString()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("last_damage"))
return new Element(String.valueOf(getLivingEntity().getLastDamage()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("time_lived"))
return new Duration(entity.getTicksLived() / 20)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("can_pickup_items"))
return new Element(String.valueOf(getLivingEntity().getCanPickupItems()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("entity_id"))
return new Element(String.valueOf(entity.getEntityId()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("fall_distance"))
return new Element(String.valueOf(entity.getFallDistance()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("uuid"))
return new Element(String.valueOf(entity.getUniqueId().toString()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("has_effect")) {
// Add later
}
if (attribute.startsWith("equipment")) {
// Add later
}
if (attribute.startsWith("world")) {
return new dWorld(entity.getWorld())
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug")) {
return new Element(debug())
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("type")) {
return new Element(getType())
.getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
}