Search in sources :

Example 31 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Essentials by drtshock.

the class Commandsuicide method run.

@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final EntityDamageEvent ede = new EntityDamageEvent(user.getBase(), EntityDamageEvent.DamageCause.SUICIDE, Float.MAX_VALUE);
    server.getPluginManager().callEvent(ede);
    ede.getEntity().setLastDamageCause(ede);
    user.getBase().setHealth(0);
    user.sendMessage(tl("suicideMessage"));
    user.setDisplayNick();
    ess.broadcastMessage(user, tl("suicideSuccess", user.getDisplayName()));
}
Also used : EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 32 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Plan by plan-player-analytics.

the class DeathEventListener method findWeapon.

public String findWeapon(Entity dead) {
    EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
    if (entityDamageEvent == null)
        return "Unknown (No damage cause defined)";
    Entity killer = ((EntityDamageByEntityEvent) entityDamageEvent).getDamager();
    if (killer instanceof Player)
        return getItemInHand((Player) killer);
    if (killer instanceof Tameable)
        return getPetType((Tameable) killer);
    // Projectile, EnderCrystal and all other causes that are not known yet
    return new EntityNameFormatter().apply(killer.getType().name());
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityNameFormatter(com.djrapitops.plan.delivery.formatting.EntityNameFormatter) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 33 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Plan by plan-player-analytics.

the class DeathEventListener method findKiller.

public Optional<Player> findKiller(Entity dead) {
    EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
    if (!(entityDamageEvent instanceof EntityDamageByEntityEvent)) {
        // Not damaged by entity, can't be a player
        return Optional.empty();
    }
    Entity killer = ((EntityDamageByEntityEvent) entityDamageEvent).getDamager();
    if (killer instanceof Player)
        return Optional.of((Player) killer);
    if (killer instanceof Tameable)
        return getOwner((Tameable) killer);
    if (killer instanceof Projectile)
        return getShooter((Projectile) killer);
    // Recursive call
    if (killer instanceof EnderCrystal)
        return findKiller(killer);
    return Optional.empty();
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 34 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project RoseStacker by Rosewood-Development.

the class EntityListener method handleEntityDeath.

private void handleEntityDeath(EntityEvent event, LivingEntity entity, boolean useLastDamageCause) {
    if (this.stackManager.isWorldDisabled(entity.getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(entity);
    if (stackedEntity == null)
        return;
    if (stackedEntity.getStackSize() == 1) {
        this.stackManager.removeEntityStack(stackedEntity);
        return;
    }
    // Should we kill the entire stack at once?
    EntityDamageEvent lastDamageCause = entity.getLastDamageCause();
    if (useLastDamageCause && (stackedEntity.getStackSettings().shouldKillEntireStackOnDeath() || (Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_KILL_ENTIRE_STACK_ON_DEATH.getBoolean() && PersistentDataUtils.isAiDisabled(entity)) || (lastDamageCause != null && Setting.ENTITY_KILL_ENTIRE_STACK_CONDITIONS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(lastDamageCause.getCause().name()))) || (entity.getKiller() != null && entity.getKiller().hasPermission("rosestacker.killentirestack")))) {
        if (Setting.ENTITY_DROP_ACCURATE_ITEMS.getBoolean()) {
            if (entity instanceof Slime)
                ((Slime) entity).setSize(1);
            if (event instanceof EntityDeathEvent) {
                EntityDeathEvent deathEvent = (EntityDeathEvent) event;
                stackedEntity.dropStackLoot(new ArrayList<>(deathEvent.getDrops()), deathEvent.getDroppedExp());
                deathEvent.getDrops().clear();
            } else {
                stackedEntity.dropStackLoot(null, 0);
            }
        } else if (Setting.ENTITY_DROP_ACCURATE_EXP.getBoolean() && event instanceof EntityDeathEvent) {
            EntityDeathEvent deathEvent = (EntityDeathEvent) event;
            deathEvent.setDroppedExp(deathEvent.getDroppedExp() * stackedEntity.getStackSize());
        }
        Player killer = entity.getKiller();
        if (killer != null && stackedEntity.getStackSize() - 1 > 0)
            killer.incrementStatistic(Statistic.KILL_ENTITY, entity.getType(), stackedEntity.getStackSize() - 1);
        this.stackManager.removeEntityStack(stackedEntity);
        return;
    }
    // Decrease stack size by 1
    stackedEntity.updateDisplay();
    stackedEntity.decreaseStackSize();
    stackedEntity.getEntity().setVelocity(new Vector());
    if (Setting.ENTITY_KILL_TRANSFER_VELOCITY.getBoolean()) {
        stackedEntity.getEntity().setVelocity(entity.getVelocity());
        entity.setVelocity(new Vector());
    }
    if (!Setting.ENTITY_DISPLAY_CORPSE.getBoolean())
        event.getEntity().remove();
}
Also used : StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Player(org.bukkit.entity.Player) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) AsyncEntityDeathEvent(dev.rosewood.rosestacker.event.AsyncEntityDeathEvent) Slime(org.bukkit.entity.Slime) Vector(org.bukkit.util.Vector) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 35 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project MagicPlugin by elBukkit.

the class EntityData method modifyDrops.

public void modifyDrops(EntityDeathEvent event) {
    if (dropXp != null) {
        event.setDroppedExp(dropXp);
    }
    List<ItemStack> dropList = event.getDrops();
    if (!defaultDrops) {
        dropList.clear();
    }
    if (dropsRequirePlayerKiller) {
        EntityDamageEvent lastDamage = event.getEntity().getLastDamageCause();
        if (!(lastDamage instanceof EntityDamageByEntityEvent))
            return;
        Entity damager = ((EntityDamageByEntityEvent) lastDamage).getDamager();
        damager = controller.getDamageSource(damager);
        if (!(damager instanceof Player) || damager == event.getEntity())
            return;
    }
    if (drops != null) {
        for (Deque<WeightedPair<String>> dropTable : drops) {
            String key = RandomUtils.weightedRandom(dropTable);
            if (key != null && !key.equalsIgnoreCase("none")) {
                ItemStack item = controller.createItem(key);
                if (item != null) {
                    dropList.add(item);
                }
            }
        }
    }
    if (loot != null) {
        ConfigurationSection processLoot = loot;
        EntityDamageEvent lastDamage = event.getEntity().getLastDamageCause();
        if (lastDamage instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) lastDamage).getDamager();
            damager = controller.getDamageSource(damager);
            Mage mage = controller.getMage(damager);
            processLoot = new MageParameters(mage, key + ".loot");
            ConfigurationUtils.addConfigurations(processLoot, loot);
        }
        Set<String> keys = processLoot.getKeys(false);
        for (String key : keys) {
            int count = 1;
            double probability;
            ConfigurationSection config = processLoot.getConfigurationSection(key);
            if (config != null) {
                probability = config.getDouble("probability", 1);
                count = config.getInt("count", 1);
            } else {
                probability = processLoot.getDouble(key);
            }
            if (!RandomUtils.checkProbability(probability))
                continue;
            ItemStack item = controller.createItem(key);
            item.setAmount(count);
            dropList.add(item);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) MageParameters(com.elmakers.mine.bukkit.configuration.MageParameters) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) WeightedPair(com.elmakers.mine.bukkit.utility.random.WeightedPair) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

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