Skip to content

Commit

Permalink
Allow adjusting the entity in the spawn event
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Apr 30, 2015
1 parent 14735e4 commit 2bac929
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Expand Up @@ -114,6 +114,9 @@ public void creatureSpawn(CreatureSpawnEvent event) {
dEntity entity = new dEntity(event.getEntity());
String reason = event.getSpawnReason().name();

// Remember the entity (for adjusting and stuff before it's spawned)
dEntity.rememberEntity(event.getEntity());

// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getLocation());

Expand Down Expand Up @@ -146,6 +149,8 @@ public void creatureSpawn(CreatureSpawnEvent event) {
String determination = BukkitWorldScriptHelper.doEvents(events,
(entity.isCitizensNPC() ? entity.getDenizenNPC() : null), null, context, true);

dEntity.forgetEntity(event.getEntity());

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -52,6 +52,16 @@ 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) {
rememberedEntities.put(entity.getUniqueId(), entity);
}

public static void forgetEntity(Entity entity) {
rememberedEntities.remove(entity.getUniqueId());
}

public static boolean isNPC(Entity entity) {
return entity != null && entity.hasMetadata("NPC") && entity.getMetadata("NPC").get(0).asBoolean();
}
Expand Down Expand Up @@ -230,10 +240,11 @@ else if (context == null || context.debug) {
return null;
}

@Deprecated
public static Entity getEntityForID(UUID ID) {
public static Entity getEntityForID(UUID id) {
if (rememberedEntities.containsKey(id))
return rememberedEntities.get(id);
for (World world : Bukkit.getWorlds()) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftWorld) world).getHandle().getEntity(ID);
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftWorld) world).getHandle().getEntity(id);

// Make sure the nmsEntity is valid, to prevent unpleasant errors
if (nmsEntity != null) {
Expand Down Expand Up @@ -1114,7 +1125,7 @@ public String identify() {
// if (isSaved(this))
// return "e@" + getSaved(this);

else if (isSpawned())
else if (isSpawned() || rememberedEntities.containsKey(entity.getUniqueId()))
return "e@" + entity.getUniqueId().toString();
}

Expand Down

0 comments on commit 2bac929

Please sign in to comment.