use of org.bukkit.event.entity.EntityDamageEvent in project Denizen-For-Bukkit by DenizenScript.
the class EntityDeathScriptEvent method onEntityDeath.
@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity livingEntity = event.getEntity();
dEntity.rememberEntity(livingEntity);
entity = new dEntity(livingEntity);
dPlayer player = null;
if (entity.isPlayer()) {
player = entity.getDenizenPlayer();
}
damager = null;
EntityDamageEvent lastDamage = entity.getBukkitEntity().getLastDamageCause();
if (lastDamage != null) {
if (lastDamage instanceof EntityDamageByEntityEvent) {
damager = new dEntity(((EntityDamageByEntityEvent) lastDamage).getDamager()).getDenizenObject();
}
}
message = null;
inventory = null;
PlayerDeathEvent subEvent = null;
if (event instanceof PlayerDeathEvent) {
subEvent = (PlayerDeathEvent) event;
message = new Element(subEvent.getDeathMessage());
// Null check to prevent NPCs from causing an NPE
if (player != null) {
inventory = player.getInventory();
}
keep_inv = subEvent.getKeepInventory();
keep_level = subEvent.getKeepLevel();
}
cause = null;
if (event.getEntity().getLastDamageCause() != null) {
cause = new Element(event.getEntity().getLastDamageCause().getCause().toString());
}
drops = new dList();
for (ItemStack stack : event.getDrops()) {
if (stack == null) {
drops.add("i@air");
} else {
drops.add(new dItem(stack).identify());
}
}
changed_drops = false;
xp = event.getDroppedExp();
this.event = event;
fire();
event.setDroppedExp(xp);
if (changed_drops) {
event.getDrops().clear();
for (String drop : drops) {
dItem item = dItem.valueOf(drop);
if (item != null) {
event.getDrops().add(item.getItemStack());
}
}
}
if (event instanceof PlayerDeathEvent) {
((PlayerDeathEvent) event).setKeepInventory(keep_inv);
((PlayerDeathEvent) event).setKeepLevel(keep_level);
}
if (message != null && subEvent != null) {
subEvent.setDeathMessage(message.asString());
}
if (cancelled && subEvent != null) {
subEvent.setDeathMessage(null);
}
dEntity.forgetEntity(livingEntity);
}
use of org.bukkit.event.entity.EntityDamageEvent in project Prism-Bukkit by prism.
the class EntitySerializer method serialize.
/**
* Serialize entity.
* @param entity Entity.
*/
public final void serialize(Entity entity) {
entityName = entity.getType().name().toLowerCase();
// Get custom name
customName = entity.getCustomName();
// Get animal age
if (entity instanceof Ageable) {
isAdult = ((Ageable) entity).isAdult();
}
// Owner
if (entity instanceof Tameable) {
final Tameable mob = (Tameable) entity;
if (mob.getOwner() != null) {
tamingOwner = mob.getOwner().getUniqueId().toString();
} else if (mob.isTamed()) {
tamingOwner = "-none-";
}
}
// Sitting
if (entity instanceof Sittable) {
sitting = ((Sittable) entity).isSitting();
}
EntityDamageEvent damageEvent = entity.getLastDamageCause();
// Saves us the null check
if (damageEvent instanceof EntityDamageByEntityEvent && !damageEvent.isCancelled() && damageEvent.getDamage() > ((LivingEntity) entity).getHealth()) {
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) damageEvent;
if (e.getDamager() instanceof Projectile) {
customDesc = EntityUtils.getCustomProjectileDescription((Projectile) e.getDamager());
}
}
serializer(entity);
}
use of org.bukkit.event.entity.EntityDamageEvent in project Prism-Bukkit by prism.
the class PrismEntityEvents method onEntityDeath.
/**
* EntityDeathEvent.
* @param event EntityDeathEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(final EntityDeathEvent event) {
final LivingEntity entity = event.getEntity();
// Mob Death
if (!(entity instanceof Player)) {
// Log item drops
if (Prism.getIgnore().event("item-drop", entity.getWorld())) {
String name = entity.getType().name().toLowerCase();
// Inventory
if (entity instanceof InventoryHolder) {
final InventoryHolder holder = (InventoryHolder) entity;
for (final ItemStack i : holder.getInventory().getContents()) {
if (checkNotNullorAir(i)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
}
}
}
// Equipment
EntityEquipment equipment = entity.getEquipment();
if (equipment != null) {
for (final ItemStack i : equipment.getArmorContents()) {
if (checkNotNullorAir(i)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
}
}
}
// Hand items not stored in "getArmorContents"
ItemStack main = entity.getEquipment().getItemInMainHand();
ItemStack off = entity.getEquipment().getItemInOffHand();
if (checkNotNullorAir(main)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", main, main.getAmount(), -1, null, entity.getLocation(), name));
}
if (checkNotNullorAir(off)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", off, off.getAmount(), -1, null, entity.getLocation(), name));
}
}
EntityDamageEvent damageEvent = entity.getLastDamageCause();
Entity entitySource = null;
Block blockSource = null;
// Resolve source
if (damageEvent != null && !damageEvent.isCancelled()) {
if (damageEvent instanceof EntityDamageByEntityEvent) {
entitySource = ((EntityDamageByEntityEvent) damageEvent).getDamager();
if (entitySource instanceof Projectile) {
ProjectileSource ps = ((Projectile) entitySource).getShooter();
if (ps instanceof BlockProjectileSource) {
entitySource = null;
blockSource = ((BlockProjectileSource) ps).getBlock();
} else {
entitySource = (Entity) ps;
}
}
} else if (damageEvent instanceof EntityDamageByBlockEvent) {
blockSource = ((EntityDamageByBlockEvent) damageEvent).getDamager();
}
}
// Create handlers
if (entitySource instanceof Player) {
Player player = (Player) entitySource;
if (!Prism.getIgnore().event("player-kill", player)) {
return;
}
RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player));
} else if (entitySource != null) {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = entitySource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
} else if (blockSource != null) {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = "block:" + blockSource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
} else {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = "unknown";
if (damageEvent != null && !damageEvent.isCancelled()) {
name = damageEvent.getCause().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
}
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
}
/*
* if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) { final
* EntityDamageByEntityEvent entityDamageByEntityEvent =
* (EntityDamageByEntityEvent) entity .getLastDamageCause();
*
* // Mob killed by player 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));
*
* } // Mob shot by an arrow from a player else 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));
*
* } else if (arrow.getShooter() instanceof LivingEntity) { final Entity damager
* = (Entity) arrow.getShooter(); String name = "unknown"; if (damager != null)
* { name = damager.getType().name().toLowerCase(); }
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* name)); } else if (arrow.getShooter() instanceof BlockProjectileSource) {
*
* final Block damager = (Block)
* ((BlockProjectileSource)arrow.getShooter()).getBlock();
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
*
* String name = "block:" + damager.getType().name().toLowerCase();
*
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* name)); } } else { // Mob died by another mob final Entity damager =
* entityDamageByEntityEvent.getDamager(); String name = "unknown"; if (damager
* != null) { name = damager.getType().name().toLowerCase(); }
*
* 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));
}
}
}
}
}
use of org.bukkit.event.entity.EntityDamageEvent in project MagicPlugin by elBukkit.
the class Mage method onDamage.
public void onDamage(EntityDamageEvent event) {
String damageType = currentDamageType;
currentDamageType = null;
lastDamage = event.getDamage();
LivingEntity entity = getLivingEntity();
if (entity == null) {
return;
}
// Send on to any registered spells
List<Listener> damageSpells = new ArrayList<>(damageListeners);
for (Listener listener : damageSpells) {
callEvent(listener, event);
if (event.isCancelled())
break;
}
EntityDamageEvent.DamageCause cause = event.getCause();
if (cause == EntityDamageEvent.DamageCause.FALL) {
if (fallProtectionCount > 0 && fallProtection > 0 && fallProtection > System.currentTimeMillis()) {
event.setCancelled(true);
fallProtectionCount--;
if (fallingSpell != null) {
double scale = 1;
LivingEntity li = getLivingEntity();
if (li != null) {
scale = event.getDamage() / CompatibilityLib.getCompatibilityUtils().getMaxHealth(li);
}
fallingSpell.playEffects("land", (float) scale, getLocation().getBlock().getRelative(BlockFace.DOWN));
}
if (fallProtectionCount <= 0) {
fallProtection = 0;
fallingSpell = null;
}
return;
} else {
fallingSpell = null;
}
}
if (isSuperProtected()) {
event.setCancelled(true);
if (entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
return;
}
if (event.isCancelled()) {
return;
}
// First check for damage reduction
double reduction = 0;
Double overallProtection = protection.get("overall");
if (overallProtection != null) {
reduction = overallProtection * controller.getMaxDamageReduction("overall");
}
// Apply weaknesses
double multiplier = 1;
Double overallWeakness = weakness.get("overall");
if (overallWeakness != null && overallWeakness > 0) {
double defendMultiplier = controller.getMaxDefendMultiplier("overall");
if (defendMultiplier > 1) {
defendMultiplier = 1 + (defendMultiplier - 1) * overallWeakness;
multiplier *= defendMultiplier;
}
}
if (cause == EntityDamageEvent.DamageCause.FIRE_TICK) {
// Also put out fire if they have maxed out fire protection.
double damageReductionFire = getProtection("fire");
if (damageReductionFire >= 1 && entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
}
lastDamageType = getDamageType(damageType, cause);
damageType = lastDamageType;
// Process triggers
trigger("damage");
double protection = getProtection(damageType);
double maxReduction = controller.getMaxDamageReduction(damageType);
reduction += protection * maxReduction;
if (reduction >= 1) {
event.setCancelled(true);
sendDebugMessage(ChatColor.RED + "Damage nullified by " + ChatColor.BLUE + damageType + " (" + cause + ")", 8);
return;
}
double damage = event.getDamage();
sendDebugMessage(ChatColor.RED + "Damaged by " + ChatColor.BLUE + (damageType == null ? "generic" : damageType) + " (" + cause + ")" + ChatColor.RED + " for " + ChatColor.DARK_RED + damage, 10);
if (reduction > 0) {
damage = (1.0 - reduction) * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " reduced by " + ChatColor.AQUA + reduction + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
double weakness = getWeakness(damageType);
double maxMultiplier = controller.getMaxDefendMultiplier(damageType);
if (maxMultiplier > 1 && weakness > 0) {
weakness = 1 + (maxMultiplier - 1) * weakness;
multiplier *= weakness;
}
if (multiplier > 1) {
damage = multiplier * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " multiplied by " + ChatColor.AQUA + multiplier + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
if (damage > 0) {
for (Iterator<Batch> iterator = pendingBatches.iterator(); iterator.hasNext(); ) {
Batch batch = iterator.next();
if (!(batch instanceof SpellBatch))
continue;
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
double cancelOnDamage = spell.cancelOnDamage();
if (cancelOnDamage > 0 && cancelOnDamage < damage) {
batch.cancel();
iterator.remove();
}
}
for (MageSpell spell : activeSpells) {
double cancelOnDamage = spell.cancelOnDamage();
if (cancelOnDamage > 0 && cancelOnDamage < damage) {
spell.cancel();
}
}
}
}
use of org.bukkit.event.entity.EntityDamageEvent in project MagicPlugin by elBukkit.
the class EntityController method onEntityDeath.
/**
* This death handler is for mobs and players alike
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityDeath(EntityDeathEvent event) {
Entity entity = event.getEntity();
boolean isPlayer = entity instanceof Player;
if (isPlayer) {
EntityDamageEvent.DamageCause cause = entity.getLastDamageCause() == null ? null : entity.getLastDamageCause().getCause();
controller.info("* Processing death of " + entity.getName() + " from " + cause + " with drops: " + event.getDrops().size(), 15);
}
Long spawnerId = CompatibilityLib.getEntityMetadataUtils().getLong(entity, MagicMetaKeys.AUTOMATION);
if (spawnerId != null) {
MagicBlock magicBlock = controller.getActiveAutomaton(spawnerId);
if (magicBlock != null) {
magicBlock.onSpawnDeath();
}
}
// Just don't ever clear player death drops, for real
if (!isPlayer) {
if (CompatibilityLib.getEntityMetadataUtils().getBoolean(entity, MagicMetaKeys.NO_DROPS)) {
event.setDroppedExp(0);
event.getDrops().clear();
} else {
UndoList pendingUndo = controller.getEntityUndo(entity);
if (pendingUndo != null && pendingUndo.isUndoType(entity.getType())) {
event.getDrops().clear();
}
}
} else {
// Clean up metadata that shouldn't be on players
CompatibilityLib.getEntityMetadataUtils().remove(entity, MagicMetaKeys.NO_DROPS);
}
EntityDamageEvent damageEvent = event.getEntity().getLastDamageCause();
if (damageEvent instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent dbe = (EntityDamageByEntityEvent) damageEvent;
Entity damager = dbe.getDamager();
damager = controller.getDamageSource(damager);
if (damager != null) {
Mage damagerMage = controller.getRegisteredMage(damager);
if (damagerMage != null) {
damagerMage.trigger("kill");
}
}
}
com.elmakers.mine.bukkit.magic.Mage mage = controller.getRegisteredMage(entity);
if (mage == null)
return;
mage.deactivateAllSpells();
mage.onDeath(event);
if (isPlayer) {
controller.info("* Mage class handled death, drops now: " + event.getDrops().size(), 15);
}
if (event instanceof PlayerDeathEvent) {
PlayerDeathEvent playerDeath = (PlayerDeathEvent) event;
handlePlayerDeath(playerDeath.getEntity(), mage, playerDeath.getDrops(), playerDeath.getKeepInventory());
}
}
Aggregations