use of org.bukkit.event.entity.EntityDamageEvent.DamageCause in project Glowstone by GlowstoneMC.
the class Explosion method damageEntities.
/////////////////////////////////////////
// Damage entities
private Collection<GlowPlayer> damageEntities() {
float power = this.power;
this.power *= 2;
Collection<GlowPlayer> affectedPlayers = new ArrayList<>();
LivingEntity[] entities = getNearbyEntities();
for (LivingEntity entity : entities) {
// refine area to sphere, instead of box
if (distanceToSquared(entity) > power * power) {
continue;
}
double exposure = world.rayTrace(location, (GlowEntity) entity);
double impact = (1 - (distanceTo(entity) / power / 2)) * exposure;
double damage = (impact * impact + impact) * 8 * power + 1;
int epf = getProtectionFactor(entity);
double reduction = calculateEnchantedReduction(epf);
damage = damage * reduction;
exposure -= exposure * epf * 0.15;
DamageCause damageCause;
if (source == null || source.getType() == EntityType.PRIMED_TNT) {
damageCause = DamageCause.BLOCK_EXPLOSION;
} else {
damageCause = DamageCause.ENTITY_EXPLOSION;
}
entity.damage(damage, source, damageCause);
if (entity instanceof GlowPlayer) {
affectedPlayers.add((GlowPlayer) entity);
if (((GlowPlayer) entity).isFlying()) {
continue;
}
}
Vector rayLength = RayUtil.getVelocityRay(distanceToHead(entity));
rayLength.multiply(exposure);
Vector currentVelocity = entity.getVelocity();
currentVelocity.add(rayLength);
entity.setVelocity(currentVelocity);
}
return affectedPlayers;
}
use of org.bukkit.event.entity.EntityDamageEvent.DamageCause in project Prism-Bukkit by prism.
the class PrismEntityEvents method onEntityDeath.
/**
*
* @param event
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(final EntityDeathEvent event) {
final Entity entity = event.getEntity();
// Mob Death
if (!(entity instanceof Player)) {
if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
if (entity instanceof Horse) {
final Horse horse = (Horse) entity;
if (horse.isCarryingChest()) {
// Log item drops
if (Prism.getIgnore().event("item-drop", entity.getWorld())) {
for (final ItemStack i : horse.getInventory().getContents()) {
if (i == null)
continue;
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), "horse"));
}
}
}
}
// Mob killed by player
final EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entity.getLastDamageCause();
if (entityDamageByEntityEvent.getDamager() instanceof Player) {
final Player player = (Player) entityDamageByEntityEvent.getDamager();
if (!Prism.getIgnore().event("player-kill", player))
return;
RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player.getName()));
} else // Mob shot by an arrow from a player
if (entityDamageByEntityEvent.getDamager() instanceof Arrow) {
final Arrow arrow = (Arrow) entityDamageByEntityEvent.getDamager();
if (arrow.getShooter() instanceof Player) {
final Player player = (Player) arrow.getShooter();
if (!Prism.getIgnore().event("player-kill", player))
return;
RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player.getName()));
}
} else {
// Mob died by another mob
final Entity damager = entityDamageByEntityEvent.getDamager();
String name = "unknown";
if (damager != null) {
name = damager.getType().getName();
}
if (name == null)
name = "unknown";
if (!Prism.getIgnore().event("entity-kill", entity.getWorld()))
return;
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
}
} else {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld()))
return;
String killer = "unknown";
final EntityDamageEvent damage = entity.getLastDamageCause();
if (damage != null) {
final DamageCause cause = damage.getCause();
if (cause != null) {
killer = cause.name().toLowerCase();
}
}
// Record the death as natural
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, killer));
}
} else {
// Determine who died and what the exact cause was
final Player p = (Player) event.getEntity();
if (Prism.getIgnore().event("player-death", p)) {
final String cause = DeathUtils.getCauseNiceName(p);
String attacker = DeathUtils.getAttackerName(p);
if (attacker.equals("pvpwolf")) {
final String owner = DeathUtils.getTameWolfOwner(event);
attacker = owner + "'s wolf";
}
RecordingQueue.addToQueue(ActionFactory.createPlayerDeath("player-death", p, cause, attacker));
}
// Log item drops
if (Prism.getIgnore().event("item-drop", p)) {
if (!event.getDrops().isEmpty()) {
for (final ItemStack i : event.getDrops()) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, p.getLocation(), p.getName()));
}
}
}
}
}
use of org.bukkit.event.entity.EntityDamageEvent.DamageCause in project Essentials by drtshock.
the class EssentialsProtectEntityListener method onEntityDamage.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityDamage(final EntityDamageEvent event) {
final Entity target = event.getEntity();
if (target instanceof Villager && prot.getSettingBool(ProtectConfig.prevent_villager_death)) {
event.setCancelled(true);
return;
}
User user = null;
if (target instanceof Player) {
user = ess.getUser((Player) target);
}
final DamageCause cause = event.getCause();
if (event instanceof EntityDamageByBlockEvent) {
if (prot.getSettingBool(ProtectConfig.disable_contactdmg) && cause == DamageCause.CONTACT && !(target instanceof Player && shouldBeDamaged(user, "contact"))) {
event.setCancelled(true);
return;
}
if (prot.getSettingBool(ProtectConfig.disable_lavadmg) && cause == DamageCause.LAVA && !(target instanceof Player && shouldBeDamaged(user, "lava"))) {
event.setCancelled(true);
return;
}
if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion) && cause == DamageCause.BLOCK_EXPLOSION && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) {
event.setCancelled(true);
return;
}
}
if (event instanceof EntityDamageByEntityEvent) {
final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent) event;
final Entity eAttack = edEvent.getDamager();
User attacker = null;
if (eAttack instanceof Player) {
attacker = ess.getUser((Player) eAttack);
}
//Creeper explode prevention
if (eAttack instanceof Creeper && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) || prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg)) && !(target instanceof Player && shouldBeDamaged(user, "creeper"))) {
event.setCancelled(true);
return;
}
if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) && prot.getSettingBool(ProtectConfig.prevent_fireball_playerdmg) && !(target instanceof Player && shouldBeDamaged(user, "fireball"))) {
event.setCancelled(true);
return;
}
if (event.getEntity() instanceof WitherSkull && prot.getSettingBool(ProtectConfig.prevent_witherskull_playerdmg) && !(target instanceof Player && shouldBeDamaged(user, "witherskull"))) {
event.setCancelled(true);
return;
}
if (eAttack instanceof TNTPrimed && prot.getSettingBool(ProtectConfig.prevent_tnt_playerdmg) && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) {
event.setCancelled(true);
return;
}
if (eAttack instanceof ExplosiveMinecart && prot.getSettingBool(ProtectConfig.prevent_tntminecart_playerdmg) && !(target instanceof Player && shouldBeDamaged(user, "tnt-minecart"))) {
event.setCancelled(true);
return;
}
// PVP Settings
if (target instanceof Player && eAttack instanceof Player && prot.getSettingBool(ProtectConfig.disable_pvp) && !user.getName().equalsIgnoreCase(attacker.getName()) && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))) {
event.setCancelled(true);
return;
}
if (edEvent.getDamager() instanceof Projectile && target instanceof Player && ((prot.getSettingBool(ProtectConfig.disable_projectiles) && !shouldBeDamaged(user, "projectiles")) || (((Projectile) edEvent.getDamager()).getShooter() instanceof Player && prot.getSettingBool(ProtectConfig.disable_pvp) && (!user.isAuthorized("essentials.protect.pvp") || !ess.getUser((Player) ((Projectile) edEvent.getDamager()).getShooter()).isAuthorized("essentials.protect.pvp"))))) {
event.setCancelled(true);
return;
}
}
if (target instanceof Player) {
if (cause == DamageCause.FALL && prot.getSettingBool(ProtectConfig.disable_fall) && !shouldBeDamaged(user, "fall")) {
event.setCancelled(true);
return;
}
if (cause == DamageCause.SUFFOCATION && prot.getSettingBool(ProtectConfig.disable_suffocate) && !shouldBeDamaged(user, "suffocation")) {
event.setCancelled(true);
return;
}
if ((cause == DamageCause.FIRE || cause == DamageCause.FIRE_TICK) && prot.getSettingBool(ProtectConfig.disable_firedmg) && !shouldBeDamaged(user, "fire")) {
event.setCancelled(true);
return;
}
if (cause == DamageCause.DROWNING && prot.getSettingBool(ProtectConfig.disable_drown) && !shouldBeDamaged(user, "drowning")) {
event.setCancelled(true);
return;
}
if (cause == DamageCause.LIGHTNING && prot.getSettingBool(ProtectConfig.disable_lightning) && !shouldBeDamaged(user, "lightning")) {
event.setCancelled(true);
return;
}
if (cause == DamageCause.WITHER && prot.getSettingBool(ProtectConfig.disable_wither) && !shouldBeDamaged(user, "wither")) {
event.setCancelled(true);
}
}
}
Aggregations