Search in sources :

Example 1 with EntityDamageByBlockEvent

use of org.bukkit.event.entity.EntityDamageByBlockEvent in project Denizen-For-Bukkit by DenizenScript.

the class HealthTrait method onDamage.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
//
// @Triggers when the NPC dies. (Requires Health Trait)
//
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
//
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDamage(EntityDamageEvent event) {
    // Check if the event pertains to this NPC
    if (event.getEntity() != npc.getEntity() || dying) {
        return;
    }
    // Make sure this is a killing blow
    if (this.getHealth() - event.getFinalDamage() > 0) {
        return;
    }
    dying = true;
    player = null;
    // Save entityId for EntityDeath event
    entityId = npc.getEntity().getEntityId();
    String deathCause = CoreUtilities.toLowerCase(event.getCause().toString()).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));
        // that player to the action's ScriptEntry
        if (killerEntity instanceof Player) {
            player = dPlayer.mirrorBukkitPlayer((Player) killerEntity);
        } else // account as well
        if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter != null && shooter instanceof LivingEntity) {
                context.put("shooter", new dEntity((LivingEntity) shooter));
                if (shooter instanceof Player) {
                    player = dPlayer.mirrorBukkitPlayer((Player) shooter);
                }
                DenizenAPI.getDenizenNPC(npc).action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
            }
        // TODO: Handle other shooter source thingy types
        }
        DenizenAPI.getDenizenNPC(npc).action("death by entity", player, context);
        DenizenAPI.getDenizenNPC(npc).action("death by " + killerEntity.getType().toString(), player, context);
    } else // If not, check if the entity has been killed by a block
    if (event instanceof EntityDamageByBlockEvent) {
        DenizenAPI.getDenizenNPC(npc).action("death by block", player, context);
    // TODO:
    // The line of code below should work, but a Bukkit bug makes the damager
    // return null. Uncomment it once the bug is fixed.
    // DenizenAPI.getDenizenNPC(npc).action("death by " +
    // ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
    }
    DenizenAPI.getDenizenNPC(npc).action("death", player, context);
    DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player, context);
    // NPC's entity still exists before proceeding
    if (npc.getEntity() == null) {
        return;
    }
    loc = dLocation.valueOf(// TODO: debug option?
    TagManager.tag(// TODO: debug option?
    respawnLocation, new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), false, null, true, null)));
    if (loc == null) {
        loc = npc.getEntity().getLocation();
    }
    if (animatedeath) {
        // Cancel navigation to keep the NPC from damaging players
        // while the death animation is being carried out.
        npc.getNavigator().cancelNavigation();
    // Reset health now to avoid the death from happening instantly
    //setHealth();
    // Play animation (TODO)
    // playDeathAnimation(npc.getEntity());
    }
    die();
    if (respawn && (Duration.valueOf(respawnDelay).getTicks() > 0)) {
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

            public void run() {
                if (CitizensAPI.getNPCRegistry().getById(npc.getId()) == null || npc.isSpawned()) {
                    return;
                } else {
                    npc.spawn(loc);
                }
            }
        }, (Duration.valueOf(respawnDelay).getTicks()));
    }
}
Also used : net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) HashMap(java.util.HashMap) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Element(net.aufdemrand.denizencore.objects.Element) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EventHandler(org.bukkit.event.EventHandler)

Aggregations

HashMap (java.util.HashMap)1 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 Projectile (org.bukkit.entity.Projectile)1 EventHandler (org.bukkit.event.EventHandler)1 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)1 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1