Java Code Examples for org.bukkit.event.entity.EntityDamageByEntityEvent#setDamage()

The following examples show how to use org.bukkit.event.entity.EntityDamageByEntityEvent#setDamage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Healing.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	if(e instanceof EntityDamageByEntityEvent) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	LivingEntity target = (LivingEntity) event.getEntity();
	

	double newHealth = target.getHealth()+event.getDamage() + level;
	
	if(newHealth >= target.getMaxHealth())
		newHealth = target.getMaxHealth();
	target.setHealth(newHealth);
	event.setDamage(0);
	target.getWorld().playEffect(target.getLocation(), Effect.POTION_BREAK, 10);
	}
}
 
Example 2
Source File: Kraken.java    From EliteMobs with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void fireballDamage(EntityDamageByEntityEvent event) {

    if (event.isCancelled()) return;
    if (!event.getEntity().getType().equals(EntityType.PLAYER)) return;
    if (!event.getDamager().getType().equals(EntityType.FIREBALL)) return;
    if (!isFireball((Fireball) event.getDamager())) return;

    for (EntityDamageByEntityEvent.DamageModifier modifier : EntityDamageByEntityEvent.DamageModifier.values())
        if (event.isApplicable(modifier))
            event.setDamage(modifier, 0);

    event.setDamage(EntityDamageEvent.DamageModifier.BASE, 2);

    removeFireball((Fireball) event.getDamager());

}
 
Example 3
Source File: Attack.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onAttack(EntityDamageByEntityEvent event, ItemStack inHand) {
	AttributeUtil attrs = new AttributeUtil(inHand);
	double dmg = this.getDouble("value");
			
	double extraAtt = 0.0;
	for (LoreEnhancement enh : attrs.getEnhancements()) {
		if (enh instanceof LoreEnhancementAttack) {
			extraAtt +=  ((LoreEnhancementAttack)enh).getExtraAttack(attrs);
		}
	}
	dmg += extraAtt;
	
	if (event.getDamager() instanceof Player) {
		Resident resident = CivGlobal.getResident(((Player)event.getDamager()));
		if (!resident.hasTechForItem(inHand)) {
			dmg = dmg / 2;
		}
	}
	
	if (dmg < 0.5) {
		dmg = 0.5;
	}
	
	event.setDamage(dmg);
}
 
Example 4
Source File: SentinelTrait.java    From Sentinel with MIT License 6 votes vote down vote up
/**
 * Called when this sentinel gets attacked, to correct the armor handling.
 */
public void whenAttacksAreHappeningToMe(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (!getNPC().isSpawned()) {
        return;
    }
    double armorLevel = getArmor(getLivingEntity());
    if (hitIsBlocked(event.getDamager())) {
        armorLevel = (1.0 - armorLevel) * 0.5 + armorLevel;
    }
    if (!event.isApplicable(EntityDamageEvent.DamageModifier.ARMOR)) {
        event.setDamage(EntityDamageEvent.DamageModifier.BASE, (1.0 - armorLevel) * event.getDamage(EntityDamageEvent.DamageModifier.BASE));
    }
    else {
        event.setDamage(EntityDamageEvent.DamageModifier.ARMOR, -armorLevel * event.getDamage(EntityDamageEvent.DamageModifier.BASE));
    }
    for (EntityDamageEvent.DamageModifier modifier : modifiersToZero) {
        if (event.isApplicable(modifier)) {
            event.setDamage(modifier, 0);
        }
    }
}
 
Example 5
Source File: QAListener.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler
public void onHit(EntityDamageByEntityEvent e) {
	if (e.isCancelled())
		return;
	if (e.getDamager() instanceof Player) {
		Player d = (Player) e.getDamager();
		if ((e.getCause() == DamageCause.ENTITY_ATTACK || e.getCause() == DamageCause.ENTITY_SWEEP_ATTACK)) {
			if (QualityArmory.isMisc(d.getItemInHand())) {
				CustomBaseObject aa = QualityArmory.getMisc(d.getItemInHand());
				if (aa instanceof MeleeItems) {
					DEBUG("Setting damage for " + aa.getName() + " to be equal to " + ((MeleeItems) aa).getDamage()
							+ ". was " + e.getDamage());
					e.setDamage(((MeleeItems) aa).getDamage());
				}
				if (aa.getSoundOnHit() != null) {
					e.getEntity().getWorld().playSound(e.getEntity().getLocation(), aa.getSoundOnHit(), 1, 1);
				}
			}
		}
		if (QualityArmory.isGun(d.getItemInHand())
				|| QualityArmory.isIronSights(d.getItemInHand()))
			DEBUG("The player " + e.getEntity().getName() + " was Hit with a gun! Damage=" + e.getDamage());
	}
}
 
Example 6
Source File: Piercing.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
    if (e instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
        LivingEntity target = (LivingEntity) event.getEntity();

        int armorCounter = 0;
        for (ItemStack piece : target.getEquipment().getArmorContents())
            if (!piece.getType().equals(Material.AIR))
                armorCounter++;

        if (armorCounter == 0)
            return;
        
        event.setDamage(DamageModifier.ARMOR, 0); //Completely remove effects of Armor
        target.getWorld().playEffect(target.getLocation(), Effect.ZOMBIE_DESTROY_DOOR, 10);
    }
}
 
Example 7
Source File: ProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onProjectileHurtEvent(EntityDamageByEntityEvent event) {
  if (!(event.getEntity() instanceof LivingEntity)) return;
  LivingEntity damagedEntity = (LivingEntity) event.getEntity();

  ProjectileDefinition projectileDefinition =
      ProjectileMatchModule.getProjectileDefinition(event.getDamager());

  if (projectileDefinition != null) {
    if (!projectileDefinition.potion.isEmpty()) {
      damagedEntity.addPotionEffects(projectileDefinition.potion);
    }

    if (projectileDefinition.damage != null) {
      event.setDamage(projectileDefinition.damage);
    }
  }
}
 
Example 8
Source File: MutationModules.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerDamage(EntityDamageByEntityEvent event) {
    Entity damager = event.getDamager();
    if ((damager instanceof Player && ItemUtils.isWeapon(((Player) damager).getItemInHand())) ||
        (damager instanceof Projectile && ((Projectile) damager).getShooter() instanceof Player)) {
        event.setDamage(DAMAGE);
    }
}
 
Example 9
Source File: Aerial.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Player player = (Player) event.getDamager();
	if(player.getVelocity().getY() == -0.0784000015258789) //Constant velocity for not moving in y direction (Gravity)
		return;
	
	event.setDamage(event.getDamage() * (1 + DamageIncreasePercentage * level));

	EffectManager.playSound(player.getLocation(), "ENTITY_BAT_TAKEOFF", 0.1f, 0.1f);
}
 
Example 10
Source File: Pyroaxe.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean effect(Event event, Player player) {
	
	EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
	Entity entity = e.getEntity();
	

	if(e.getDamager() == player && entity.getFireTicks() > 0) {
		e.setDamage(damageMultiplier * e.getDamage());
		entity.getWorld().playEffect(entity.getLocation(), Effect.ZOMBIE_DESTROY_DOOR, 10);
		EffectManager.playSound(entity.getLocation(), "BLOCK_ANVIL_LAND", 1f, 0.001f);
		return true;
	}
	return false;
}
 
Example 11
Source File: KillerProjectiles.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onEntityDamagePlayer(EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Projectile && event.getEntity() instanceof Player) {

        Projectile projectile = (Projectile) event.getDamager();
        Region r = RedProtect.get().rm.getTopRegion(projectile.getLocation());

        if (r != null && r.getFlagBool("killer-projectiles") && getConfig().getStringList("allowed-types").contains(projectile.getType().name())) {
            // Set damage from projectile to 0
            event.setDamage(0);
        }
    }
}
 
Example 12
Source File: Rage.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Player) {
        ItemStack item = ((Player) event.getDamager()).getItemInHand();
        if (item != null) {
            if (item.containsEnchantment(Enchantment.DAMAGE_ALL) && item.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 1) {
                event.setDamage(1000);
            }
        }
    } else if (event.getDamager() instanceof Arrow) {
        if (event.getDamager().hasMetadata("rage")) {
            event.setDamage(1000);
        }
    }
}
 
Example 13
Source File: SentinelTrait.java    From Sentinel with MIT License 5 votes vote down vote up
/**
 * Called when this sentinel attacks something, to correct damage handling.
 */
public void whenAttacksAreHappeningFromMe(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (!npc.isSpawned()) {
        return;
    }
    if (SentinelPlugin.instance.alternateDamage) {
        if (canEnforce) {
            canEnforce = false;
            whenAttacksHappened(event);
            if (!event.isCancelled()) {
                ((LivingEntity) event.getEntity()).damage(event.getFinalDamage());
                if (event.getEntity() instanceof LivingEntity) {
                    weaponHelper.knockback((LivingEntity) event.getEntity());
                }
            }
            if (SentinelPlugin.debugMe) {
                debug("enforce damage value to " + event.getFinalDamage());
            }
        }
        else {
            if (SentinelPlugin.debugMe) {
                debug("refuse damage enforcement");
            }
        }
        event.setDamage(0);
        event.setCancelled(true);
        return;
    }
    event.setDamage(EntityDamageEvent.DamageModifier.BASE, getDamage(false));
}
 
Example 14
Source File: Defense.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onDefense(EntityDamageByEntityEvent event, ItemStack stack) {
	double defValue = this.getDouble("value");
	
	/* Try to get any extra defense enhancements from this item. */
	LoreCraftableMaterial craftMat = LoreCraftableMaterial.getCraftMaterial(stack);
	if (craftMat == null) {
		return;
	}
			
	double extraDef = 0;
	AttributeUtil attrs = new AttributeUtil(stack);
	
	for (LoreEnhancement enh : attrs.getEnhancements()) {
		if (enh instanceof LoreEnhancementDefense) {
			extraDef +=  ((LoreEnhancementDefense)enh).getExtraDefense(attrs);
		}
	}
	
	defValue += extraDef;		
	double damage = event.getDamage();
	
	if (event.getEntity() instanceof Player) {
		Resident resident = CivGlobal.getResident(((Player)event.getEntity()));
		if (!resident.hasTechForItem(stack)) {
			defValue = defValue / 2;
		}
	}
	
	damage -= defValue;
	if (damage < 0.5) {
		/* Always do at least 0.5 damage. */
		damage = 0.5;
	}
	
	event.setDamage(damage);
}
 
Example 15
Source File: Archer.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST,ignoreCancelled = true)
public void damageListener(final EntityDamageByEntityEvent event)
{
	if(event.getDamager().getType() == EntityType.ARROW)
	{
		ProjectileSource s = ((Projectile)event.getDamager()).getShooter();
		if(s instanceof Player)
		{
			AnniPlayer shooter = AnniPlayer.getPlayer(((Player) s).getUniqueId());
			if(shooter != null && shooter.getKit() != null && shooter.getKit().equals(this))
				event.setDamage(event.getDamage()+1);
		}
	}
}
 
Example 16
Source File: Berserker.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void damageListener(final EntityDamageByEntityEvent event)
{
	if(event.getEntity().getType() == EntityType.PLAYER && event.getDamager().getType() == EntityType.PLAYER)
	{
		Player one = (Player)event.getDamager();
		AnniPlayer p = AnniPlayer.getPlayer(one.getUniqueId());
		if(p != null && p.getKit().equals(this))
		{
			if((one.getHealth() / one.getMaxHealth()) <= .42)
				event.setDamage(event.getDamage()+1);
		}
	}
}
 
Example 17
Source File: ModifyBowProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void fixEntityDamage(EntityDamageByEntityEvent event) {
  Entity projectile = event.getDamager();
  if (projectile.hasMetadata("customProjectile")) {

    // If the custom projectile replaced an arrow, recreate some effects specific to arrows
    if (projectile.hasMetadata("damage")) {
      boolean critical = projectile.getMetadata("critical").get(0).asBoolean();
      int knockback = projectile.getMetadata("knockback").get(0).asInt();
      double damage = projectile.getMetadata("damage").get(0).asDouble();
      double speed = projectile.getVelocity().length();

      // Reproduce the damage calculation from nms.EntityArrow with the addition of our modifier
      int finalDamage = (int) Math.ceil(speed * damage * this.velocityMod);
      if (critical) {
        finalDamage += match.getRandom().nextInt(finalDamage / 2 + 2);
      }
      event.setDamage(finalDamage);

      // Flame arrows - target burns for 5 seconds always
      if (projectile.getFireTicks() > 0) {
        event.getEntity().setFireTicks(100);
      }

      // Reproduce the knockback calculation for punch bows
      if (knockback > 0) {
        Vector projectileVelocity = projectile.getVelocity();
        double horizontalSpeed =
            Math.sqrt(
                projectileVelocity.getX() * projectileVelocity.getX()
                    + projectileVelocity.getZ() * projectileVelocity.getZ());
        Vector velocity = event.getEntity().getVelocity();
        velocity.setX(
            velocity.getX() + projectileVelocity.getX() * knockback * 0.6 / horizontalSpeed);
        velocity.setY(velocity.getY() + 0.1);
        velocity.setZ(
            velocity.getZ() + projectileVelocity.getZ() * knockback * 0.6 / horizontalSpeed);
        event.getEntity().setVelocity(velocity);
      }

      // If the projectile is not an arrow, play an impact sound.
      if (event.getEntity() instanceof Player
          && (projectile instanceof Projectile && !(projectile instanceof Arrow))) {
        Projectile customProjectile = (Projectile) projectile;
        if (customProjectile.getShooter() instanceof Player) {
          Player bukkitShooter = (Player) customProjectile.getShooter();
          MatchPlayer shooter = match.getPlayer(bukkitShooter);
          if (shooter != null && event.getEntity() != null) {
            shooter.playSound(PROJECTILE_SOUND);
          }
        }
      }
    }

    // Apply any potion effects attached to the projectile
    if (event.getEntity() instanceof LivingEntity) {
      for (PotionEffect potionEffect : this.potionEffects) {
        ((LivingEntity) event.getEntity()).addPotionEffect(potionEffect);
      }
    }
  }
}
 
Example 18
Source File: SentinelTrait.java    From Sentinel with MIT License 4 votes vote down vote up
/**
 * Called when this sentinel attacks something with a projectile, to correct damage handling.
 */
public void whenAttacksAreHappeningFromMyArrow(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (!npc.isSpawned()) {
        return;
    }
    if (SentinelPlugin.instance.alternateDamage) {
        if (canEnforce) {
            canEnforce = false;
            whenAttacksHappened(event);
            if (!event.isCancelled()) {
                ((LivingEntity) event.getEntity()).damage(getDamage(true));
                if (event.getEntity() instanceof LivingEntity) {
                    weaponHelper.knockback((LivingEntity) event.getEntity());
                }
            }
            if (SentinelPlugin.debugMe) {
                debug("enforce damage value to " + getDamage(true));
            }
        }
        else {
            if (SentinelPlugin.debugMe) {
                debug("refuse damage enforcement");
            }
        }
        event.setDamage(0);
        event.setCancelled(true);
        return;
    }
    double dam = getDamage(true);
    double modder = event.getDamage(EntityDamageEvent.DamageModifier.BASE);
    double rel = modder == 0.0 ? 1.0 : dam / modder;
    event.setDamage(EntityDamageEvent.DamageModifier.BASE, dam);
    for (EntityDamageEvent.DamageModifier mod : EntityDamageEvent.DamageModifier.values()) {
        if (mod != EntityDamageEvent.DamageModifier.BASE && event.isApplicable(mod)) {
            event.setDamage(mod, event.getDamage(mod) * rel);
            if (SentinelPlugin.debugMe) {
                debug("Set damage for " + mod + " to " + event.getDamage(mod));
            }
        }
    }
}
 
Example 19
Source File: RageMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void handlePlayerDamage(EntityDamageByEntityEvent event) {
  if (isRage(event.getDamager())) {
    event.setDamage(1000);
  }
}
 
Example 20
Source File: CombatSystem.java    From EliteMobs with GNU General Public License v3.0 2 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void eliteMobDamageHandler(EntityDamageByEntityEvent event) {

    if (event.isCancelled()) return;
    if (!(event.getEntity() instanceof Player)) return;

    LivingEntity damager = EntityFinder.getRealDamager(event);
    if (damager == null) return;

    if (!EntityTracker.isEliteMob(event.getDamager()))
        return;

    EliteMobEntity eliteMobEntity = EntityTracker.getEliteMobEntity(damager);
    if (eliteMobEntity == null) return;

    //From this point on, the damage event is fully altered by Elite Mobs

    //Get rid of all vanilla armor reduction
    for (EntityDamageEvent.DamageModifier modifier : EntityDamageEvent.DamageModifier.values())
        if (event.isApplicable(modifier))
            event.setDamage(modifier, 0);

    Player player = (Player) event.getEntity();

    //Determine tiers
    double eliteTier = MobTierFinder.findMobTier(eliteMobEntity);
    double playerTier = ItemTierFinder.findArmorSetTier(player);

    double newDamage = eliteToPlayerDamageFormula(eliteTier, playerTier, player, event);

    //Prevent untouchable armor and 1-shots

    if (newDamage < 1) newDamage = 1;
    if (newDamage > 19) newDamage = 19;

    //Set the final damage value
    event.setDamage(EntityDamageEvent.DamageModifier.BASE, newDamage);

    //Deal with the player getting killed
    if (player.getHealth() - event.getDamage() <= 0)
        PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.intializeDeathMessage(player, damager));

}