Search in sources :

Example 41 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Denizen by DenizenScript.

the class NPCTagBase method onDeath.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
// 
// @Triggers when the NPC dies.
// 
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
// 
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
    NPC citizen = CitizensAPI.getNPCRegistry().getNPC(deathEvent.getEntity());
    if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(citizen);
    EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
    String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
    context.put("death_cause", new ElementTag(deathCause));
    PlayerTag player = null;
    if (event instanceof EntityDamageByEntityEvent) {
        Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
        context.put("killer", new EntityTag(killerEntity).getDenizenObject());
        if (killerEntity instanceof Player) {
            player = PlayerTag.mirrorBukkitPlayer((Player) killerEntity);
        } else if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter instanceof LivingEntity) {
                context.put("shooter", new EntityTag((LivingEntity) shooter).getDenizenObject());
                if (shooter instanceof Player) {
                    player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
                }
                npc.action("death by " + ((LivingEntity) shooter).getType(), player, context);
            }
        }
        npc.action("death by entity", player, context);
        npc.action("death by " + killerEntity.getType(), player, context);
    } else if (event instanceof EntityDamageByBlockEvent) {
        npc.action("death by block", null, context);
    }
    npc.action("death", player, context);
    npc.action("death by " + deathCause, player, context);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 42 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project AllMiniGames by MiniGameWorlds.

the class BreedMob method onEvent.

@Override
protected void onEvent(Event event) {
    if (event instanceof EntityDeathEvent) {
        EntityDeathEvent e = (EntityDeathEvent) event;
        if (!(e.getEntity() instanceof Mob)) {
            return;
        }
        Mob entity = (Mob) e.getEntity();
        // check mob
        if (this.mobs.contains(entity)) {
            layTwoMobs(entity.getLocation());
            // clear drops
            e.getDrops().clear();
        }
    } else if (event instanceof EntityDamageEvent) {
        EntityDamageEvent e = (EntityDamageEvent) event;
        if (e.getEntity() instanceof Player) {
            Player p = (Player) e.getEntity();
            // if death
            if (p.getHealth() <= e.getDamage()) {
                e.setCancelled(true);
                // title
                sendTitle(p, ChatColor.RED + "DIE", "");
                // set live: false
                this.setLive(p, false);
            }
        }
    } else if (event instanceof EntityExplodeEvent) {
        // prevent block breaking by explosion of creeper
        EntityExplodeEvent e = (EntityExplodeEvent) event;
        if (!this.mobs.contains(e.getEntity())) {
            return;
        }
        // prevent breaking
        e.blockList().clear();
        // lay
        layTwoMobs(e.getEntity().getLocation());
        // sound
        SoundTool.play(getPlayers(), Sound.BLOCK_NOTE_BLOCK_CHIME);
    }
}
Also used : Mob(org.bukkit.entity.Mob) Player(org.bukkit.entity.Player) EntityExplodeEvent(org.bukkit.event.entity.EntityExplodeEvent) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 43 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project AllMiniGames by MiniGameWorlds.

the class HiddenArcher method onEvent.

@Override
protected void onEvent(Event event) {
    super.onEvent(event);
    if (event instanceof EntityDamageEvent) {
        EntityDamageEvent damageEvent = (EntityDamageEvent) event;
        damageEvent.setCancelled(true);
        if (damageEvent instanceof EntityDamageByEntityEvent) {
            EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) damageEvent;
            // check
            Entity damager = e.getDamager();
            Entity victimEntity = e.getEntity();
            if (!(damager instanceof Snowball && victimEntity instanceof Player)) {
                return;
            }
            Player victim = (Player) victimEntity;
            Snowball snowball = (Snowball) damager;
            if (snowball.getShooter() instanceof Player) {
                Player shooter = ((Player) snowball.getShooter());
                if (!this.isSameTeam(victim, shooter)) {
                    damageEvent.setCancelled(false);
                    // remove damage
                    e.setDamage(0);
                    this.shootPlayer(shooter, victim);
                }
            }
        }
    } else if (event instanceof ProjectileLaunchEvent) {
        ProjectileLaunchEvent e = (ProjectileLaunchEvent) event;
        Projectile proj = e.getEntity();
        ProjectileSource shooter = proj.getShooter();
        if (proj.getType() == EntityType.SNOWBALL) {
            Player p = (Player) shooter;
            p.getInventory().addItem(new ItemStack(Material.SNOWBALL));
            p.setCooldown(Material.SNOWBALL, 20 * this.reloadCoolDown);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Snowball(org.bukkit.entity.Snowball) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) ProjectileSource(org.bukkit.projectiles.ProjectileSource) ItemStack(org.bukkit.inventory.ItemStack) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Projectile(org.bukkit.entity.Projectile)

Example 44 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project AllMiniGames by MiniGameWorlds.

the class PassMob method onEvent.

@Override
protected void onEvent(Event event) {
    super.onEvent(event);
    if (event instanceof EntityDeathEvent) {
        EntityDeathEvent e = (EntityDeathEvent) event;
        Entity entity = e.getEntity();
        if (this.isPassMobEntity(entity)) {
            Area area = this.getMobArea(entity);
            area.passMobToOtherArea(entity, this.otherArea(area));
            // remove drops
            e.getDrops().clear();
        }
    } else if (event instanceof EntityDamageEvent) {
        EntityDamageEvent e = (EntityDamageEvent) event;
        if (e.getEntity() instanceof Player) {
            Player p = (Player) e.getEntity();
            // if death
            if (p.getHealth() <= e.getDamage()) {
                // cancel damage
                e.setDamage(0);
                // msg
                sendTitle(p, ChatColor.RED + "DIE", "");
                // heal player
                PlayerTool.heal(p);
                Team team = this.getTeam(p);
                Area area = this.getTeamArea(team);
                p.teleport(area.loc);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 45 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project AllMiniGames by MiniGameWorlds.

the class Bridge method onEvent.

@Override
protected void onEvent(Event event) {
    if (event instanceof EntityDamageEvent) {
        EntityDamageEvent e = (EntityDamageEvent) event;
        e.setDamage(0);
    } else if (event instanceof PlayerInteractEvent) {
        useSkill((PlayerInteractEvent) event);
    }
}
Also used : PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Aggregations

EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)109 Player (org.bukkit.entity.Player)43 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)40 Entity (org.bukkit.entity.Entity)30 LivingEntity (org.bukkit.entity.LivingEntity)25 EventHandler (org.bukkit.event.EventHandler)24 Projectile (org.bukkit.entity.Projectile)15 ItemStack (org.bukkit.inventory.ItemStack)13 Test (org.junit.Test)11 Function (com.google.common.base.Function)8 ProjectileSource (org.bukkit.projectiles.ProjectileSource)8 Location (org.bukkit.Location)7 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)7 DamageCause (org.bukkit.event.entity.EntityDamageEvent.DamageCause)7 EntityDeathEvent (org.bukkit.event.entity.EntityDeathEvent)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 World (org.bukkit.World)6 Block (org.bukkit.block.Block)6