Skip to content

Commit

Permalink
Make kill listeners work with NPCs that have the Health trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Mar 13, 2013
1 parent dc685a7 commit c24fa14
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/main/java/net/aufdemrand/denizen/npc/traits/HealthTrait.java
Expand Up @@ -5,8 +5,11 @@
import net.aufdemrand.denizen.utilities.arguments.Duration;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCDeathEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.minecraft.server.v1_4_R1.EntityHuman;

import org.bukkit.Bukkit;
import org.bukkit.EntityEffect;
import org.bukkit.Location;
Expand All @@ -21,6 +24,7 @@
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;

public class HealthTrait extends Trait implements Listener {

Expand All @@ -43,6 +47,7 @@ public class HealthTrait extends Trait implements Listener {
private String respawnLocation = "<npc.location>";

// internal
private Player player = null;
private boolean dying = false;
private Location loc;

Expand Down Expand Up @@ -186,7 +191,25 @@ public void setHealth(int health) {
((CraftLivingEntity) npc.getBukkitEntity()).getHandle().setHealth(health);
currenthealth = health;
}


public void die()
{
// Set the player as the killer of the NPC, for listeners
if (player != null)
((CraftLivingEntity) npc.getBukkitEntity())
.getHandle().killer = (EntityHuman) ((CraftLivingEntity) ((Entity) player)).getHandle();

setHealth();
EntityDeathEvent entityDeath = new EntityDeathEvent(npc.getBukkitEntity(), null);
NPCDeathEvent npcDeath = new NPCDeathEvent(npc, entityDeath);

DenizenAPI.getCurrentInstance().getServer()
.getPluginManager().callEvent(npcDeath);
DenizenAPI.getCurrentInstance().getServer()
.getPluginManager().callEvent(entityDeath);
npc.despawn(DespawnReason.DEATH);
}

@EventHandler(priority = EventPriority.MONITOR)
public void onDeath(EntityDamageEvent event) {
// Don't use NPCDamageEvent because it doesn't work well
Expand All @@ -200,7 +223,6 @@ public void onDeath(EntityDamageEvent event) {

dying = true;

Player player = null;
String deathCause = event.getCause().toString().toLowerCase().replace('_', ' ');

// Check if the entity has been killed by another entity
Expand Down Expand Up @@ -229,6 +251,7 @@ else if (killerEntity instanceof Projectile)
DenizenAPI.getDenizenNPC(npc).action("death by entity", player);
DenizenAPI.getDenizenNPC(npc).action("death by " +
killerEntity.getType().toString(), player);

}
// If not, check if the entity has been killed by a block
else if (event instanceof EntityDamageByBlockEvent)
Expand All @@ -249,26 +272,24 @@ else if (event instanceof EntityDamageByBlockEvent)
// NPC's entity still exists before proceeding
if (npc.getBukkitEntity() == null)
return;

loc = aH.getLocationFrom(DenizenAPI.getCurrentInstance().tagManager()
.tag(null, DenizenAPI.getDenizenNPC(npc), respawnLocation, false));
if (loc == null) loc = npc.getBukkitEntity().getLocation();

if (animatedeath) {
setHealth();
npc.getBukkitEntity().playEffect(EntityEffect.DEATH);

Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
public void run() {
npc.despawn(DespawnReason.DEATH);
setHealth();
die();
}
} , (long) ((Duration.valueOf(animationDelay).getSeconds() * 20)) );

} else {
npc.despawn(DespawnReason.DEATH);
setHealth();
die();
}

if (respawn) {
Expand Down

0 comments on commit c24fa14

Please sign in to comment.