Search in sources :

Example 11 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Glowstone by GlowstoneMC.

the class GlowArmorStand method damage.

@Override
public void damage(double amount, Entity source, @NotNull DamageCause cause) {
    if (getNoDamageTicks() > 0 || health <= 0 || !canTakeDamage(cause)) {
        return;
    }
    if (source instanceof Projectile && !(source instanceof Arrow)) {
        return;
    }
    EntityDamageEvent event = EventFactory.getInstance().onEntityDamage(source == null ? new EntityDamageEvent(this, cause, amount) : new EntityDamageByEntityEvent(source, this, cause, amount));
    if (event.isCancelled()) {
        return;
    }
    boolean drop = false;
    if (source instanceof GlowPlayer || source instanceof Arrow && ((Projectile) source).getShooter() instanceof GlowPlayer) {
        GlowPlayer damager = (GlowPlayer) (source instanceof GlowPlayer ? source : ((Arrow) source).getShooter());
        if (damager.getGameMode() == GameMode.ADVENTURE) {
            return;
        } else if (damager.getGameMode() == GameMode.CREATIVE) {
            // Instantly kill the entity
            amount = 2;
        } else {
            // Needs two hits
            amount = 1;
            drop = true;
        }
    }
    setLastDamage(amount);
    setHealth(health - amount, drop);
}
Also used : Arrow(org.bukkit.entity.Arrow) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) Projectile(org.bukkit.entity.Projectile) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 12 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project HawkEye by oliverwoodings.

the class MonitorEntityListener method onEntityDeath.

/**
 * Uses the lastAttacker field in the players {@link PlayerSession} to log the death and cause
 */
@HawkEvent(dataType = { DataType.PVP_DEATH, DataType.MOB_DEATH, DataType.OTHER_DEATH, DataType.ENTITY_KILL })
public void onEntityDeath(EntityDeathEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof Player) {
        // Player death
        Player victim = (Player) entity;
        // Mob or PVP death
        if (victim.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) (victim.getLastDamageCause())).getDamager();
            if (damager instanceof Player) {
                if (!Config.isLogged(DataType.PVP_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.PVP_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            } else {
                if (!Config.isLogged(DataType.MOB_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.MOB_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            }
        // Other death
        } else {
            if (!Config.isLogged(DataType.OTHER_DEATH) && !Config.LogDeathDrops)
                return;
            EntityDamageEvent dEvent = victim.getLastDamageCause();
            String cause = dEvent == null ? "Unknown" : victim.getLastDamageCause().getCause().name();
            String[] words = cause.split("_");
            for (int i = 0; i < words.length; i++) words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
            cause = Util.join(Arrays.asList(words), " ");
            DataManager.addEntry(new DataEntry(victim, DataType.OTHER_DEATH, victim.getLocation(), cause));
        }
        // Log item drops
        if (Config.LogDeathDrops) {
            String data = null;
            for (ItemStack stack : event.getDrops()) {
                if (stack.getData() != null)
                    data = stack.getAmount() + "x " + stack.getTypeId() + ":" + stack.getData().getData();
                else
                    data = stack.getAmount() + "x " + stack.getTypeId();
                DataManager.addEntry(new DataEntry(victim, DataType.ITEM_DROP, victim.getLocation(), data));
            }
        }
    } else {
        // Mob Death
        if (!Config.isLogged(DataType.ENTITY_KILL))
            return;
        if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
            // Only interested in player kills
            if (!(damager instanceof Player))
                return;
            Player player = (Player) damager;
            DataManager.addEntry(new DataEntry(player, DataType.ENTITY_KILL, entity.getLocation(), Util.getEntityName(entity)));
        }
    }
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ItemStack(org.bukkit.inventory.ItemStack) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 13 with EntityDamageByEntityEvent

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

the class EntityDamagedScriptEvent method onEntityDamaged.

@EventHandler
public void onEntityDamaged(EntityDamageEvent event) {
    entity = new EntityTag(event.getEntity());
    cause = new ElementTag(CoreUtilities.toLowerCase(event.getCause().name()));
    damager = null;
    projectile = null;
    held = null;
    if (event instanceof EntityDamageByEntityEvent) {
        damager = new EntityTag(((EntityDamageByEntityEvent) event).getDamager());
        if (damager.isProjectile()) {
            projectile = damager;
            if (damager.hasShooter()) {
                damager = damager.getShooter();
            }
        }
        if (damager != null) {
            held = damager.getItemInHand();
            if (held != null) {
                held.setAmount(1);
            }
        }
    }
    this.event = event;
    fire(event);
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EventHandler(org.bukkit.event.EventHandler)

Example 14 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Prism-Bukkit by prism.

the class DeathUtils method getCauseNiceName.

/**
 * Returns the name of what caused an entity to die.
 *
 * @return String
 */
public static String getCauseNiceName(Entity entity) {
    EntityDamageEvent e = entity.getLastDamageCause();
    if (e == null) {
        return "unknown";
    }
    // Determine the root cause
    DamageCause damageCause = e.getCause();
    Entity killer = null;
    // If was damaged by an entity
    if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entity.getLastDamageCause();
        // Arrow?
        if (entityDamageByEntityEvent.getDamager() instanceof Arrow) {
            Projectile arrow = (Arrow) entityDamageByEntityEvent.getDamager();
            ProjectileSource source = arrow.getShooter();
            if (source instanceof Player) {
                killer = ((Player) source);
            }
        } else {
            killer = entityDamageByEntityEvent.getDamager();
        }
    }
    if (entity instanceof Player) {
        Player player = (Player) entity;
        // yourself with instant damage it doesn't show as suicide.
        if (killer instanceof Player) {
            // Themself
            if (killer.getName().equals(player.getName())) {
                return "suicide";
            }
            // translate bukkit events to nicer names
            if ((damageCause.equals(DamageCause.ENTITY_ATTACK) || damageCause.equals(DamageCause.PROJECTILE))) {
                return "pvp";
            }
        }
    }
    // Causes of death for either entities or players
    if (damageCause.equals(DamageCause.ENTITY_ATTACK)) {
        return "mob";
    } else if (damageCause.equals(DamageCause.PROJECTILE)) {
        return "skeleton";
    } else if (damageCause.equals(DamageCause.ENTITY_EXPLOSION)) {
        return "creeper";
    } else if (damageCause.equals(DamageCause.CONTACT)) {
        return "cactus";
    } else if (damageCause.equals(DamageCause.BLOCK_EXPLOSION)) {
        return "tnt";
    } else if (damageCause.equals(DamageCause.FIRE) || damageCause.equals(DamageCause.FIRE_TICK)) {
        return "fire";
    } else if (damageCause.equals(DamageCause.MAGIC)) {
        return "potion";
    }
    return damageCause.name().toLowerCase();
}
Also used : Arrow(org.bukkit.entity.Arrow) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) DamageCause(org.bukkit.event.entity.EntityDamageEvent.DamageCause) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Projectile(org.bukkit.entity.Projectile)

Example 15 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project xAuth by CypherX.

the class xAuthEntityListener method onEntityDamage.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDamage(EntityDamageEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof Player && ((Player) entity).isOnline()) {
        // player taking damage
        xAuthPlayer xp = plyrMngr.getPlayer(((Player) entity).getName());
        if (plyrMngr.isRestricted(xp, event) || plyrMngr.hasGodmode(xp, event.getCause()))
            event.setCancelled(true);
    }
    if (event instanceof EntityDamageByEntityEvent) {
        // player dealing damage to other entity
        EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
        Entity damager = edbeEvent.getDamager();
        if (damager instanceof Player) {
            xAuthPlayer player = plyrMngr.getPlayer(((Player) damager).getName());
            if (plyrMngr.isRestricted(player, edbeEvent)) {
                plyrMngr.sendNotice(player);
                event.setCancelled(true);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) com.cypherx.xauth.xAuthPlayer(com.cypherx.xauth.xAuthPlayer) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) com.cypherx.xauth.xAuthPlayer(com.cypherx.xauth.xAuthPlayer) 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