Search in sources :

Example 1 with EntityDamageByEntityEvent

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

the class HurtCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    dEntity source = (dEntity) scriptEntry.getObject("source");
    Element amountElement = scriptEntry.getElement("amount");
    Element cause = scriptEntry.getElement("cause");
    dB.report(scriptEntry, getName(), amountElement.debug() + aH.debugList("entities", entities) + (cause == null ? "" : cause.debug()) + (source == null ? "" : source.debug()));
    double amount = amountElement.asDouble();
    for (dEntity entity : entities) {
        if (entity.getLivingEntity() == null) {
            dB.echoDebug(scriptEntry, entity + " is not a living entity!");
            continue;
        }
        if (cause == null) {
            if (source == null) {
                entity.getLivingEntity().damage(amount);
            } else {
                entity.getLivingEntity().damage(amount, source.getBukkitEntity());
            }
        } else {
            EntityDamageEvent ede = source == null ? new EntityDamageEvent(entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount) : new EntityDamageByEntityEvent(source.getBukkitEntity(), entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount);
            Bukkit.getPluginManager().callEvent(ede);
            if (!ede.isCancelled()) {
                if (source == null) {
                    entity.getLivingEntity().damage(ede.getFinalDamage());
                } else {
                    entity.getLivingEntity().damage(ede.getFinalDamage(), source.getBukkitEntity());
                }
            }
        }
    }
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 2 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent 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)

Example 3 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project MassiveCore by MassiveCraft.

the class MUtil method getLiableDamager.

public static Entity getLiableDamager(EntityDamageEvent event) {
    if (!(event instanceof EntityDamageByEntityEvent))
        return null;
    EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
    Entity ret = edbeEvent.getDamager();
    if (ret instanceof Projectile) {
        Projectile projectile = (Projectile) ret;
        ProjectileSource projectileSource = projectile.getShooter();
        if (projectileSource instanceof Entity)
            ret = (Entity) projectileSource;
    }
    Entity cloudBasedDamager = IntegrationLiabilityAreaEffectCloud.get().getLiableDamager(edbeEvent);
    if (cloudBasedDamager != null)
        ret = cloudBasedDamager;
    return ret;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile)

Example 4 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project RedProtect by FabioZumbi12.

the class RPEntityListener method onEntityDamage.

@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityDamage(EntityDamageEvent e) {
    if (e.isCancelled()) {
        return;
    }
    Entity ent = e.getEntity();
    Region r = RedProtect.get().rm.getTopRegion(ent.getLocation());
    if (ent instanceof LivingEntity && !(ent instanceof Monster)) {
        if (r != null && r.flagExists("invincible")) {
            if (r.getFlagBool("invincible")) {
                if (ent instanceof Animals) {
                    ((Animals) ent).setTarget(null);
                }
                e.setCancelled(true);
            }
        }
    }
    if (e instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent de = (EntityDamageByEntityEvent) e;
        Entity e1 = de.getEntity();
        Entity e2 = de.getDamager();
        if (e2 == null) {
            return;
        }
        RedProtect.get().logger.debug("RPEntityListener - Is EntityDamageByEntityEvent event. Damager: " + e2.getType().name());
        if (e2 instanceof Projectile) {
            Projectile a = (Projectile) e2;
            if (a.getShooter() instanceof Entity) {
                e2 = (Entity) a.getShooter();
            }
            a = null;
            if (e2 == null) {
                return;
            }
        }
        RedProtect.get().logger.debug("EntityDamageByEntityEvent event. Damager Player: " + e2.getType().name());
        RedProtect.get().logger.debug("Cause: " + de.getCause().name());
        Region r1 = RedProtect.get().rm.getTopRegion(e1.getLocation());
        Region r2 = RedProtect.get().rm.getTopRegion(e2.getLocation());
        if (de.getCause().equals(DamageCause.LIGHTNING) || de.getCause().equals(DamageCause.BLOCK_EXPLOSION) || de.getCause().equals(DamageCause.FIRE) || de.getCause().equals(DamageCause.WITHER) || de.getCause().equals(DamageCause.CUSTOM) || de.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
            if (r1 != null && !r1.canFire() && !(e2 instanceof Player)) {
                e.setCancelled(true);
                return;
            }
        }
        if (e1 instanceof Player) {
            if (e2 instanceof Player && !e1.equals(e2)) {
                Player p2 = (Player) e2;
                if (r1 != null) {
                    Material mp2 = p2.getItemInHand().getType();
                    if (RedProtect.get().version >= 190) {
                        if (p2.getInventory().getItemInMainHand() != null) {
                            mp2 = p2.getInventory().getItemInMainHand().getType();
                        } else {
                            mp2 = p2.getInventory().getItemInOffHand().getType();
                        }
                    }
                    if (mp2.equals(Material.EGG) && !r1.canProtectiles(p2)) {
                        e.setCancelled(true);
                        RPLang.sendMessage(p2, "playerlistener.region.cantuse");
                        return;
                    }
                    if (r2 != null) {
                        if (mp2.equals(Material.EGG) && !r2.canProtectiles(p2)) {
                            e.setCancelled(true);
                            RPLang.sendMessage(p2, "playerlistener.region.cantuse");
                            return;
                        }
                        if ((r1.flagExists("pvp") && !r1.canPVP((Player) e1, p2)) || (r1.flagExists("pvp") && !r2.canPVP((Player) e1, p2))) {
                            e.setCancelled(true);
                            RPLang.sendMessage(p2, "entitylistener.region.cantpvp");
                        }
                    } else if (r1.flagExists("pvp") && !r1.canPVP((Player) e1, p2)) {
                        e.setCancelled(true);
                        RPLang.sendMessage(p2, "entitylistener.region.cantpvp");
                    }
                } else if (r2 != null && r2.flagExists("pvp") && !r2.canPVP((Player) e1, p2)) {
                    e.setCancelled(true);
                    RPLang.sendMessage(p2, "entitylistener.region.cantpvp");
                }
            }
        } else if (e1 instanceof Animals || e1 instanceof Villager || e1 instanceof Golem) {
            if (r1 != null && e2 instanceof Player) {
                Player p2 = (Player) e2;
                if (!r1.canInteractPassives(p2)) {
                    e.setCancelled(true);
                    RPLang.sendMessage(p2, "entitylistener.region.cantpassive");
                }
            }
        } else if (e1 instanceof Hanging && e2 instanceof Player) {
            Player p2 = (Player) e2;
            if (r1 != null && !r1.canBuild(p2) && !r1.canBreak(e1.getType())) {
                e.setCancelled(true);
                RPLang.sendMessage(p2, "playerlistener.region.cantuse");
                return;
            }
            if (r2 != null && !r2.canBuild(p2) && !r2.canBreak(e1.getType())) {
                e.setCancelled(true);
                RPLang.sendMessage(p2, "playerlistener.region.cantuse");
            }
        } else if (e1 instanceof Hanging && e2 instanceof Monster) {
            if (r1 != null || r2 != null) {
                RedProtect.get().logger.debug("Cancelled ItemFrame drop Item");
                e.setCancelled(true);
            }
        } else if (e2 instanceof Explosive) {
            if ((r1 != null && !r1.canFire()) || (r2 != null && !r2.canFire())) {
                e.setCancelled(true);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Material(org.bukkit.Material) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) Golem(org.bukkit.entity.Golem) Animals(org.bukkit.entity.Animals) Explosive(org.bukkit.entity.Explosive) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Hanging(org.bukkit.entity.Hanging) Monster(org.bukkit.entity.Monster) Villager(org.bukkit.entity.Villager) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) EventHandler(org.bukkit.event.EventHandler)

Example 5 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project RedProtect by FabioZumbi12.

the class MPListener method onEntityDamageByPet.

@EventHandler
public void onEntityDamageByPet(EntityDamageEvent e) {
    if (e.isCancelled()) {
        return;
    }
    Entity ent = e.getEntity();
    if (ent instanceof MyPetBukkitEntity) {
        Region r = RedProtect.get().rm.getTopRegion(ent.getLocation());
        if (r != null && r.flagExists("invincible")) {
            if (r.getFlagBool("invincible")) {
                e.setCancelled(true);
                ((MyPetBukkitEntity) ent).forgetTarget();
            }
        }
    }
    if (e instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent de = (EntityDamageByEntityEvent) e;
        Entity e1 = de.getEntity();
        Entity e2 = de.getDamager();
        Location loc = e1.getLocation();
        Region r1 = RedProtect.get().rm.getTopRegion(loc);
        if (r1 == null) {
            return;
        }
        if (e1 instanceof Animals || e1 instanceof Villager || e1 instanceof Golem) {
            Entity damager = e2;
            if (damager instanceof CraftMyPetProjectile) {
                damager = ((CraftMyPetProjectile) damager).getMyPetProjectile().getShooter().getBukkitEntity();
            }
            if (damager instanceof MyPetBukkitEntity) {
                MyPetBukkitEntity mp2 = (MyPetBukkitEntity) damager;
                Player p2 = mp2.getOwner().getPlayer();
                LivingEntity liv = (LivingEntity) e1;
                if (!r1.canBuild(p2) || !r1.canInteractPassives(p2)) {
                    e.setCancelled(true);
                    mp2.getMyPet().removePet();
                    for (PotionEffect ef : liv.getActivePotionEffects()) {
                        liv.removePotionEffect(ef.getType());
                    }
                    p2.sendMessage(RPLang.get("mplistener.cantattack.passives"));
                    return;
                }
            }
        }
        if (e1 instanceof Player) {
            Entity damager = e2;
            if (damager instanceof CraftMyPetProjectile) {
                damager = ((CraftMyPetProjectile) damager).getMyPetProjectile().getShooter().getBukkitEntity();
            }
            if (damager instanceof MyPetBukkitEntity) {
                MyPetBukkitEntity mp2 = (MyPetBukkitEntity) damager;
                Player p2 = mp2.getOwner().getPlayer();
                if (!r1.canPVP((Player) e1, p2)) {
                    e.setCancelled(true);
                    mp2.getMyPet().removePet();
                    for (PotionEffect ef : p2.getActivePotionEffects()) {
                        p2.removePotionEffect(ef.getType());
                    }
                    p2.sendMessage(RPLang.get("mplistener.cantattack.players"));
                }
            }
        }
    }
}
Also used : MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) PotionEffect(org.bukkit.potion.PotionEffect) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)35 EventHandler (org.bukkit.event.EventHandler)22 Entity (org.bukkit.entity.Entity)19 Player (org.bukkit.entity.Player)17 LivingEntity (org.bukkit.entity.LivingEntity)15 Projectile (org.bukkit.entity.Projectile)12 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)12 ItemStack (org.bukkit.inventory.ItemStack)6 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)5 Element (net.aufdemrand.denizencore.objects.Element)5 ProjectileSource (org.bukkit.projectiles.ProjectileSource)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 Arrow (org.bukkit.entity.Arrow)4 ArrayList (java.util.ArrayList)3 Animals (org.bukkit.entity.Animals)3 Tameable (org.bukkit.entity.Tameable)3 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)3 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)2 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)2