Search in sources :

Example 71 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project SwissKnife by EgirlsNationDev.

the class PlayerUtil method killPlayer.

public static void killPlayer(Player player) {
    final EntityDamageEvent ede = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.SUICIDE, Float.MAX_VALUE);
    ede.getEntity().setLastDamageCause(ede);
    Bukkit.getPluginManager().callEvent(ede);
    if (ede.isCancelled()) {
        return;
    }
    player.setHealth(0);
}
Also used : EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 72 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project KnockBackFFA by GaMeIsNtOvEr.

the class GameRules method onPlayerMove.

@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
    Player player = e.getPlayer();
    List<String> voids = ArenaConfiguration.get().getStringList("registered-voids");
    for (String vd : voids) {
        if (ArenaConfiguration.get().getLocation("voids." + vd + ".pos1") != null) {
            Location pos1 = ArenaConfiguration.get().getLocation("voids." + vd + ".pos1");
            Location pos2 = ArenaConfiguration.get().getLocation("voids." + vd + ".pos2");
            BoundingBox bb = new BoundingBox(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ());
            if (bb.contains(player.getLocation().toVector()) && player.getWorld() == pos1.getWorld()) {
                Integer damage = ArenaConfiguration.get().getInt("voids." + vd + ".damage");
                if (damage != null) {
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            if (player.isDead() || !bb.contains(player.getLocation().toVector()) || player.getWorld() != pos1.getWorld()) {
                                cancel();
                            } else {
                                player.damage(damage);
                                player.setLastDamageCause(new EntityDamageEvent(player, EntityDamageEvent.DamageCause.VOID, damage));
                            }
                        }
                    }.runTaskTimer(KnockbackFFA.getInstance(), 0, 20);
                } else {
                    throw new NullPointerException("Void " + vd + " damage is null");
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) BoundingBox(org.bukkit.util.BoundingBox) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 73 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project MockBukkit by MockBukkit.

the class LivingEntityMock method damage.

@SuppressWarnings("deprecation")
@Override
public void damage(double amount, Entity source) {
    if (isInvulnerable()) {
        if (source instanceof HumanEntity) {
            if (((Player) source).getGameMode() != GameMode.CREATIVE) {
                return;
            }
        } else {
            return;
        }
    }
    Map<EntityDamageEvent.DamageModifier, Double> modifiers = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
    modifiers.put(EntityDamageEvent.DamageModifier.BASE, 1.0);
    Map<EntityDamageEvent.DamageModifier, Function<Double, Double>> modifierFunctions = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
    modifierFunctions.put(EntityDamageEvent.DamageModifier.BASE, damage -> damage);
    EntityDamageEvent event = source != null ? new EntityDamageByEntityEvent(source, this, EntityDamageEvent.DamageCause.ENTITY_ATTACK, modifiers, modifierFunctions) : new EntityDamageEvent(this, EntityDamageEvent.DamageCause.CUSTOM, modifiers, modifierFunctions);
    event.setDamage(amount);
    Bukkit.getPluginManager().callEvent(event);
    if (!event.isCancelled()) {
        amount = event.getDamage();
        setHealth(health - amount);
    }
}
Also used : Function(com.google.common.base.Function) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EnumMap(java.util.EnumMap) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 74 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project NachoSpigot by CobbleSword.

the class KillCommand method execute.

@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender))
        return true;
    if (sender instanceof Player) {
        Player player = (Player) sender;
        EntityDamageEvent ede = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.SUICIDE, 1000);
        Bukkit.getPluginManager().callEvent(ede);
        if (ede.isCancelled())
            return true;
        ede.getEntity().setLastDamageCause(ede);
        player.setHealth(0);
        sender.sendMessage("Ouch. That look like it hurt.");
    } else {
        sender.sendMessage("You can only perform this command as a player");
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 75 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project NachoSpigot by CobbleSword.

the class EntityLiving method d.

// CraftBukkit start
protected boolean d(final DamageSource damagesource, float f) {
    // void -> boolean, add final
    if (!this.isInvulnerable(damagesource)) {
        final boolean human = this instanceof EntityHuman;
        float originalDamage = f;
        Function<Double, Double> hardHat = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && EntityLiving.this.getEquipment(4) != null) {
                    return -(f - (f * 0.75F));
                }
                return -0.0;
            }
        };
        float hardHatModifier = hardHat.apply((double) f).floatValue();
        f += hardHatModifier;
        Function<Double, Double> blocking = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                if (human) {
                    if (!damagesource.ignoresArmor() && ((EntityHuman) EntityLiving.this).isBlocking() && f > 0.0F) {
                        return -(f - ((1.0F + f) * 0.5F));
                    }
                }
                return -0.0;
            }
        };
        float blockingModifier = blocking.apply((double) f).floatValue();
        f += blockingModifier;
        Function<Double, Double> armor = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                return -(f - EntityLiving.this.applyArmorModifier(damagesource, f.floatValue()));
            }
        };
        float armorModifier = armor.apply((double) f).floatValue();
        f += armorModifier;
        Function<Double, Double> resistance = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                if (!damagesource.isStarvation() && EntityLiving.this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
                    int i = (EntityLiving.this.getEffect(MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
                    int j = 25 - i;
                    float f1 = f.floatValue() * (float) j;
                    return -(f - (f1 / 25.0F));
                }
                return -0.0;
            }
        };
        float resistanceModifier = resistance.apply((double) f).floatValue();
        f += resistanceModifier;
        Function<Double, Double> magic = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                return -(f - EntityLiving.this.applyMagicModifier(damagesource, f.floatValue()));
            }
        };
        float magicModifier = magic.apply((double) f).floatValue();
        f += magicModifier;
        Function<Double, Double> absorption = new Function<Double, Double>() {

            @Override
            public Double apply(Double f) {
                return -(Math.max(f - Math.max(f - EntityLiving.this.getAbsorptionHearts(), 0.0F), 0.0F));
            }
        };
        float absorptionModifier = absorption.apply((double) f).floatValue();
        EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
        if (event.isCancelled()) {
            return false;
        }
        f = (float) event.getFinalDamage();
        // Apply damage to helmet
        if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
            this.getEquipment(4).damage((int) (event.getDamage() * 4.0F + this.random.nextFloat() * event.getDamage() * 2.0F), this);
        }
        // Apply damage to armor
        if (!damagesource.ignoresArmor()) {
            float armorDamage = (float) (event.getDamage() + event.getDamage(DamageModifier.BLOCKING) + event.getDamage(DamageModifier.HARD_HAT));
            this.damageArmor(armorDamage);
        }
        absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION);
        this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F));
        if (f != 0.0F) {
            if (human) {
                // PAIL: Be sure to drag all this code from the EntityHuman subclass each update.
                ((EntityHuman) this).applyExhaustion(damagesource.getExhaustionCost());
                if (f < 3.4028235E37F) {
                    ((EntityHuman) this).a(StatisticList.x, Math.round(f * 10.0F));
                }
            }
            // CraftBukkit end
            float f2 = this.getHealth();
            this.setHealth(f2 - f);
            this.bs().a(damagesource, f2, f);
            // CraftBukkit start
            if (human) {
                return true;
            }
            // CraftBukkit end
            this.setAbsorptionHearts(this.getAbsorptionHearts() - f);
        }
        // CraftBukkit
        return true;
    }
    // CraftBukkit
    return false;
}
Also used : Function(com.google.common.base.Function) 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