Skip to content

Commit

Permalink
Add more context stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 30, 2013
1 parent e59a316 commit 383d34a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Expand Up @@ -202,4 +202,4 @@ public void onHit(EntityDamageByEntityEvent event) {
}


}
}
29 changes: 20 additions & 9 deletions src/main/java/net/aufdemrand/denizen/npc/traits/HealthTrait.java
Expand Up @@ -29,6 +29,9 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class HealthTrait extends Trait implements Listener {

// Saved to the C2 saves.yml
Expand Down Expand Up @@ -216,11 +219,16 @@ public void onDamage(EntityDamageEvent event) {
entityId = npc.getBukkitEntity().getEntityId();

String deathCause = event.getCause().toString().toLowerCase().replace('_', ' ');
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("damage", new Element(event.getDamage()));
context.put("death_cause", new Element(deathCause));


// Check if the entity has been killed by another entity
if (event instanceof EntityDamageByEntityEvent)
{
Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
context.put("killer", new dEntity(killerEntity));

// Check if the damager was a player and, if so, attach
// that player to the action's ScriptEntry
Expand All @@ -232,23 +240,26 @@ public void onDamage(EntityDamageEvent event) {
else if (killerEntity instanceof Projectile)
{
LivingEntity shooter = ((Projectile) killerEntity).getShooter();
if (shooter != null) {

if (shooter instanceof Player)
player = dPlayer.mirrorBukkitPlayer((Player) shooter);
context.put("shooter", new dEntity(shooter));
if (shooter instanceof Player)
player = dPlayer.mirrorBukkitPlayer((Player) shooter);

DenizenAPI.getDenizenNPC(npc).action("death by " +
shooter.getType().toString(), player);
DenizenAPI.getDenizenNPC(npc).action("death by " +
shooter.getType().toString(), player, context);
}
}

DenizenAPI.getDenizenNPC(npc).action("death by entity", player);
DenizenAPI.getDenizenNPC(npc).action("death by entity", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " +
killerEntity.getType().toString(), player);
killerEntity.getType().toString(), player, context);

}
// If not, check if the entity has been killed by a block
else if (event instanceof EntityDamageByBlockEvent)
{
DenizenAPI.getDenizenNPC(npc).action("death by block", player);
DenizenAPI.getDenizenNPC(npc).action("death by block", player, context);

// TODO:
// The line of code below should work, but a Bukkit bug makes the damager
Expand All @@ -258,8 +269,8 @@ else if (event instanceof EntityDamageByBlockEvent)
// ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
}

DenizenAPI.getDenizenNPC(npc).action("death", player);
DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player);
DenizenAPI.getDenizenNPC(npc).action("death", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player, context);

// One of the actions above may have removed the NPC, so check if the
// NPC's entity still exists before proceeding
Expand Down

1 comment on commit 383d34a

@mcmonkey4eva
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to leave before testing this, but it seems clean and simple enough...

Please sign in to comment.