use of org.bukkit.event.entity.EntityDamageEvent in project Denizen-For-Bukkit by DenizenScript.
the class HurtCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dEntity source = (dEntity) scriptEntry.getObject("source");
Element amountElement = scriptEntry.getElement("amount");
Element cause = scriptEntry.getElement("cause");
dB.report(scriptEntry, getName(), amountElement.debug() + aH.debugList("entities", entities) + (cause == null ? "" : cause.debug()) + (source == null ? "" : source.debug()));
double amount = amountElement.asDouble();
for (dEntity entity : entities) {
if (entity.getLivingEntity() == null) {
dB.echoDebug(scriptEntry, entity + " is not a living entity!");
continue;
}
if (cause == null) {
if (source == null) {
entity.getLivingEntity().damage(amount);
} else {
entity.getLivingEntity().damage(amount, source.getBukkitEntity());
}
} else {
EntityDamageEvent ede = source == null ? new EntityDamageEvent(entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount) : new EntityDamageByEntityEvent(source.getBukkitEntity(), entity.getBukkitEntity(), EntityDamageEvent.DamageCause.valueOf(cause.asString().toUpperCase()), amount);
Bukkit.getPluginManager().callEvent(ede);
if (!ede.isCancelled()) {
if (source == null) {
entity.getLivingEntity().damage(ede.getFinalDamage());
} else {
entity.getLivingEntity().damage(ede.getFinalDamage(), source.getBukkitEntity());
}
}
}
}
}
use of org.bukkit.event.entity.EntityDamageEvent in project Glowstone by GlowstoneMC.
the class GlowArmorStand method damage.
@Override
public void damage(double amount, Entity source, @NotNull DamageCause cause) {
if (getNoDamageTicks() > 0 || health <= 0 || !canTakeDamage(cause)) {
return;
}
if (source instanceof Projectile && !(source instanceof Arrow)) {
return;
}
EntityDamageEvent event = EventFactory.getInstance().onEntityDamage(source == null ? new EntityDamageEvent(this, cause, amount) : new EntityDamageByEntityEvent(source, this, cause, amount));
if (event.isCancelled()) {
return;
}
boolean drop = false;
if (source instanceof GlowPlayer || source instanceof Arrow && ((Projectile) source).getShooter() instanceof GlowPlayer) {
GlowPlayer damager = (GlowPlayer) (source instanceof GlowPlayer ? source : ((Arrow) source).getShooter());
if (damager.getGameMode() == GameMode.ADVENTURE) {
return;
} else if (damager.getGameMode() == GameMode.CREATIVE) {
// Instantly kill the entity
amount = 2;
} else {
// Needs two hits
amount = 1;
drop = true;
}
}
setLastDamage(amount);
setHealth(health - amount, drop);
}
use of org.bukkit.event.entity.EntityDamageEvent in project HawkEye by oliverwoodings.
the class MonitorEntityListener method onEntityDeath.
/**
* Uses the lastAttacker field in the players {@link PlayerSession} to log the death and cause
*/
@HawkEvent(dataType = { DataType.PVP_DEATH, DataType.MOB_DEATH, DataType.OTHER_DEATH, DataType.ENTITY_KILL })
public void onEntityDeath(EntityDeathEvent event) {
Entity entity = event.getEntity();
if (entity instanceof Player) {
// Player death
Player victim = (Player) entity;
// Mob or PVP death
if (victim.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
Entity damager = ((EntityDamageByEntityEvent) (victim.getLastDamageCause())).getDamager();
if (damager instanceof Player) {
if (!Config.isLogged(DataType.PVP_DEATH) && !Config.LogDeathDrops)
return;
DataManager.addEntry(new DataEntry(victim, DataType.PVP_DEATH, victim.getLocation(), Util.getEntityName(damager)));
} else {
if (!Config.isLogged(DataType.MOB_DEATH) && !Config.LogDeathDrops)
return;
DataManager.addEntry(new DataEntry(victim, DataType.MOB_DEATH, victim.getLocation(), Util.getEntityName(damager)));
}
// Other death
} else {
if (!Config.isLogged(DataType.OTHER_DEATH) && !Config.LogDeathDrops)
return;
EntityDamageEvent dEvent = victim.getLastDamageCause();
String cause = dEvent == null ? "Unknown" : victim.getLastDamageCause().getCause().name();
String[] words = cause.split("_");
for (int i = 0; i < words.length; i++) words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
cause = Util.join(Arrays.asList(words), " ");
DataManager.addEntry(new DataEntry(victim, DataType.OTHER_DEATH, victim.getLocation(), cause));
}
// Log item drops
if (Config.LogDeathDrops) {
String data = null;
for (ItemStack stack : event.getDrops()) {
if (stack.getData() != null)
data = stack.getAmount() + "x " + stack.getTypeId() + ":" + stack.getData().getData();
else
data = stack.getAmount() + "x " + stack.getTypeId();
DataManager.addEntry(new DataEntry(victim, DataType.ITEM_DROP, victim.getLocation(), data));
}
}
} else {
// Mob Death
if (!Config.isLogged(DataType.ENTITY_KILL))
return;
if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
Entity damager = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
// Only interested in player kills
if (!(damager instanceof Player))
return;
Player player = (Player) damager;
DataManager.addEntry(new DataEntry(player, DataType.ENTITY_KILL, entity.getLocation(), Util.getEntityName(entity)));
}
}
}
use of org.bukkit.event.entity.EntityDamageEvent in project Prism-Bukkit by prism.
the class DeathUtils method getCauseNiceName.
/**
* Returns the name of what caused an entity to die.
*
* @return String
*/
public static String getCauseNiceName(Entity entity) {
EntityDamageEvent e = entity.getLastDamageCause();
if (e == null) {
return "unknown";
}
// Determine the root cause
DamageCause damageCause = e.getCause();
Entity killer = null;
// If was damaged by an entity
if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entity.getLastDamageCause();
// Arrow?
if (entityDamageByEntityEvent.getDamager() instanceof Arrow) {
Projectile arrow = (Arrow) entityDamageByEntityEvent.getDamager();
ProjectileSource source = arrow.getShooter();
if (source instanceof Player) {
killer = ((Player) source);
}
} else {
killer = entityDamageByEntityEvent.getDamager();
}
}
if (entity instanceof Player) {
Player player = (Player) entity;
// yourself with instant damage it doesn't show as suicide.
if (killer instanceof Player) {
// Themself
if (killer.getName().equals(player.getName())) {
return "suicide";
}
// translate bukkit events to nicer names
if ((damageCause.equals(DamageCause.ENTITY_ATTACK) || damageCause.equals(DamageCause.PROJECTILE))) {
return "pvp";
}
}
}
// Causes of death for either entities or players
if (damageCause.equals(DamageCause.ENTITY_ATTACK)) {
return "mob";
} else if (damageCause.equals(DamageCause.PROJECTILE)) {
return "skeleton";
} else if (damageCause.equals(DamageCause.ENTITY_EXPLOSION)) {
return "creeper";
} else if (damageCause.equals(DamageCause.CONTACT)) {
return "cactus";
} else if (damageCause.equals(DamageCause.BLOCK_EXPLOSION)) {
return "tnt";
} else if (damageCause.equals(DamageCause.FIRE) || damageCause.equals(DamageCause.FIRE_TICK)) {
return "fire";
} else if (damageCause.equals(DamageCause.MAGIC)) {
return "potion";
}
return damageCause.name().toLowerCase();
}
use of org.bukkit.event.entity.EntityDamageEvent in project Essentials by drtshock.
the class Commandkill method updatePlayer.
@Override
protected void updatePlayer(final Server server, final CommandSource sender, final User user, final String[] args) throws PlayerExemptException {
final Player matchPlayer = user.getBase();
if (sender.isPlayer() && user.isAuthorized("essentials.kill.exempt") && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.kill.force")) {
throw new PlayerExemptException(tl("killExempt", matchPlayer.getDisplayName()));
}
final EntityDamageEvent ede = new EntityDamageEvent(matchPlayer, sender.isPlayer() && sender.getPlayer().getName().equals(matchPlayer.getName()) ? EntityDamageEvent.DamageCause.SUICIDE : EntityDamageEvent.DamageCause.CUSTOM, Float.MAX_VALUE);
server.getPluginManager().callEvent(ede);
if (ede.isCancelled() && sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.kill.force")) {
return;
}
ede.getEntity().setLastDamageCause(ede);
matchPlayer.setHealth(0);
sender.sendMessage(tl("kill", matchPlayer.getDisplayName()));
}
Aggregations