--- a/net/minecraft/server/EntityLiving.java +++ b/net/minecraft/server/EntityLiving.java @@ -13,6 +13,24 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +// CraftBukkit start +import java.util.ArrayList; +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import org.bukkit.Location; +import org.bukkit.craftbukkit.attribute.CraftAttributeMap; +import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.EntityResurrectEvent; +import org.bukkit.event.entity.EntityTeleportEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; +// CraftBukkit end + public abstract class EntityLiving extends Entity { private static final Logger a = LogManager.getLogger(); @@ -88,6 +106,15 @@ private BlockPosition bF; private DamageSource bG; private long bH; + // CraftBukkit start + public int expToDrop; + public int maxAirTicks = 300; + boolean forceDrops; + ArrayList<org.bukkit.inventory.ItemStack> drops = new ArrayList<org.bukkit.inventory.ItemStack>(); + public org.bukkit.craftbukkit.attribute.CraftAttributeMap craftAttributes; + public boolean collides = true; + public boolean canPickUpLoot; + // CraftBukkit end public void killEntity() { this.damageEntity(DamageSource.OUT_OF_WORLD, Float.MAX_VALUE); @@ -102,7 +129,8 @@ this.updateEffects = true; this.activeItem = ItemStack.a; this.initAttributes(); - this.setHealth(this.getMaxHealth()); + // CraftBukkit - setHealth(getMaxHealth()) inlined and simplified to skip the instanceof check for EntityPlayer, as getBukkitEntity() is not initialized in constructor + this.datawatcher.set(EntityLiving.HEALTH, (float) this.getAttributeInstance(GenericAttributes.maxHealth).getValue()); this.i = true; this.aM = (float) ((Math.random() + 1.0D) * 0.009999999776482582D); this.setPosition(this.locX, this.locY, this.locZ); @@ -140,7 +168,13 @@ double d1 = Math.min((double) (0.2F + f / 15.0F), 2.5D); int i = (int) (150.0D * d1); - ((WorldServer) this.world).a(EnumParticle.BLOCK_DUST, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)}); + // CraftBukkit start - visiblity api + if (this instanceof EntityPlayer) { + ((WorldServer) this.world).sendParticles((EntityPlayer) this, EnumParticle.BLOCK_DUST, false, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)}); + } else { + ((WorldServer) this.world).a(EnumParticle.BLOCK_DUST, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)}); + } + // CraftBukkit end } } @@ -202,7 +236,11 @@ this.stopRiding(); } } else { - this.setAirTicks(300); + // CraftBukkit start - Only set if needed to work around a DataWatcher inefficiency + if (this.getAirTicks() != 300) { + this.setAirTicks(maxAirTicks); + } + // CraftBukkit end } if (!this.world.isClientSide) { @@ -259,6 +297,18 @@ this.world.methodProfiler.b(); } + // CraftBukkit start + public int getExpReward() { + int exp = this.getExpValue(this.killer); + + if (!this.world.isClientSide && (this.lastDamageByPlayerTime > 0 || this.alwaysGivesExp()) && this.isDropExperience() && this.world.getGameRules().getBoolean("doMobLoot")) { + return exp; + } else { + return 0; + } + } + // CraftBukkit end + protected void b(BlockPosition blockposition) { int i = EnchantmentManager.a(Enchantments.j, this); @@ -274,19 +324,19 @@ protected void bO() { ++this.deathTicks; - if (this.deathTicks == 20) { + if (this.deathTicks >= 20 && !this.dead) { // CraftBukkit - (this.deathTicks == 20) -> (this.deathTicks >= 20 && !this.dead) int i; - if (!this.world.isClientSide && (this.alwaysGivesExp() || this.lastDamageByPlayerTime > 0 && this.isDropExperience() && this.world.getGameRules().getBoolean("doMobLoot"))) { - i = this.getExpValue(this.killer); - - while (i > 0) { - int j = EntityExperienceOrb.getOrbValue(i); + // CraftBukkit start - Update getExpReward() above if the removed if() changes! + i = this.expToDrop; + while (i > 0) { + int j = EntityExperienceOrb.getOrbValue(i); - i -= j; - this.world.addEntity(new EntityExperienceOrb(this.world, this.locX, this.locY, this.locZ, j)); - } + i -= j; + this.world.addEntity(new EntityExperienceOrb(this.world, this.locX, this.locY, this.locZ, j)); } + this.expToDrop = 0; + // CraftBukkit end this.die(); @@ -442,6 +492,17 @@ } } + // CraftBukkit start + if (nbttagcompound.hasKey("Bukkit.MaxHealth")) { + NBTBase nbtbase = nbttagcompound.get("Bukkit.MaxHealth"); + if (nbtbase.getTypeId() == 5) { + this.getAttributeInstance(GenericAttributes.maxHealth).setValue(((NBTTagFloat) nbtbase).asDouble()); + } else if (nbtbase.getTypeId() == 3) { + this.getAttributeInstance(GenericAttributes.maxHealth).setValue(((NBTTagInt) nbtbase).asDouble()); + } + } + // CraftBukkit end + if (nbttagcompound.hasKeyOfType("Health", 99)) { this.setHealth(nbttagcompound.getFloat("Health")); } @@ -464,9 +525,15 @@ } + // CraftBukkit start + private boolean isTickingEffects = false; + private List<Object> effectsToProcess = Lists.newArrayList(); + // CraftBukkit end + protected void tickPotionEffects() { Iterator iterator = this.effects.keySet().iterator(); + isTickingEffects = true; // CraftBukkit try { while (iterator.hasNext()) { MobEffectList mobeffectlist = (MobEffectList) iterator.next(); @@ -484,6 +551,17 @@ } catch (ConcurrentModificationException concurrentmodificationexception) { ; } + // CraftBukkit start + isTickingEffects = false; + for (Object e : effectsToProcess) { + if (e instanceof MobEffect) { + addEffect((MobEffect) e); + } else { + removeEffect((MobEffectList) e); + } + } + effectsToProcess.clear(); + // CraftBukkit end if (this.updateEffects) { if (!this.world.isClientSide) { @@ -585,6 +663,12 @@ } public void addEffect(MobEffect mobeffect) { + // CraftBukkit start + if (isTickingEffects) { + effectsToProcess.add(mobeffect); + return; + } + // CraftBukkit end if (this.d(mobeffect)) { MobEffect mobeffect1 = (MobEffect) this.effects.get(mobeffect.getMobEffect()); @@ -617,6 +701,12 @@ @Nullable public MobEffect c(@Nullable MobEffectList mobeffectlist) { + // CraftBukkit start + if (isTickingEffects) { + effectsToProcess.add(mobeffectlist); + return null; + } + // CraftBukkit end return (MobEffect) this.effects.remove(mobeffectlist); } @@ -656,20 +746,52 @@ } + // CraftBukkit start - Delegate so we can handle providing a reason for health being regained public void heal(float f) { + heal(f, EntityRegainHealthEvent.RegainReason.CUSTOM); + } + + public void heal(float f, EntityRegainHealthEvent.RegainReason regainReason) { float f1 = this.getHealth(); if (f1 > 0.0F) { - this.setHealth(f1 + f); + EntityRegainHealthEvent event = new EntityRegainHealthEvent(this.getBukkitEntity(), f, regainReason); + this.world.getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + this.setHealth((float) (this.getHealth() + event.getAmount())); + } + // CraftBukkit end } } public final float getHealth() { + // CraftBukkit start - Use unscaled health + if (this instanceof EntityPlayer) { + return (float) ((EntityPlayer) this).getBukkitEntity().getHealth(); + } + // CraftBukkit end return ((Float) this.datawatcher.get(EntityLiving.HEALTH)).floatValue(); } public void setHealth(float f) { + // CraftBukkit start - Handle scaled health + if (this instanceof EntityPlayer) { + org.bukkit.craftbukkit.entity.CraftPlayer player = ((EntityPlayer) this).getBukkitEntity(); + // Squeeze + if (f < 0.0F) { + player.setRealHealth(0.0D); + } else if (f > player.getMaxHealth()) { + player.setRealHealth(player.getMaxHealth()); + } else { + player.setRealHealth(f); + } + + this.datawatcher.set(EntityLiving.HEALTH, Float.valueOf(player.getScaledHealth())); + return; + } + // CraftBukkit end this.datawatcher.set(EntityLiving.HEALTH, Float.valueOf(MathHelper.a(f, 0.0F, this.getMaxHealth()))); } @@ -687,14 +809,16 @@ } else { float f1 = f; - if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && !this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { + // CraftBukkit - Moved into damageEntity0(DamageSource, float) + if (false && (damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && !this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { this.getEquipment(EnumItemSlot.HEAD).damage((int) (f * 4.0F + this.random.nextFloat() * f * 2.0F), this); f *= 0.75F; } - boolean flag = false; + boolean flag = f > 0.0F && this.applyBlockingModifier(damagesource); // Copied from below - if (f > 0.0F && this.applyBlockingModifier(damagesource)) { + // CraftBukkit - Moved into damageEntity0(DamageSource, float) + if (false && f > 0.0F && this.applyBlockingModifier(damagesource)) { this.damageShield(f); f = 0.0F; if (!damagesource.a()) { @@ -713,20 +837,39 @@ if ((float) this.noDamageTicks > (float) this.maxNoDamageTicks / 2.0F) { if (f <= this.lastDamage) { + this.forceExplosionKnockback = true; // CraftBukkit - SPIGOT-949 - for vanilla consistency, cooldown does not prevent explosion knockback return false; } - this.damageEntity0(damagesource, f - this.lastDamage); + // CraftBukkit start + if (!this.damageEntity0(damagesource, f - this.lastDamage)) { + return false; + } + // CraftBukkit end this.lastDamage = f; flag1 = false; } else { + // CraftBukkit start + if (!this.damageEntity0(damagesource, f)) { + return false; + } this.lastDamage = f; this.noDamageTicks = this.maxNoDamageTicks; - this.damageEntity0(damagesource, f); + // this.damageEntity0(damagesource, f); + // CraftBukkit end this.az = 10; this.hurtTicks = this.az; } + // CraftBukkit start + if (this instanceof EntityAnimal) { + ((EntityAnimal) this).resetLove(); + if (this instanceof EntityTameableAnimal) { + ((EntityTameableAnimal) this).getGoalSit().setSitting(false); + } + } + // CraftBukkit end + this.aA = 0.0F; Entity entity1 = damagesource.getEntity(); @@ -833,18 +976,26 @@ EnumHand[] aenumhand = EnumHand.values(); int i = aenumhand.length; + // CraftBukkit start + ItemStack itemstack1 = ItemStack.a; for (int j = 0; j < i; ++j) { EnumHand enumhand = aenumhand[j]; - ItemStack itemstack1 = this.b(enumhand); + itemstack1 = this.b(enumhand); if (itemstack1.getItem() == Items.cY) { itemstack = itemstack1.cloneItemStack(); - itemstack1.subtract(1); + // itemstack1.subtract(1); // CraftBukkit break; } } - if (itemstack != null) { + EntityResurrectEvent event = new EntityResurrectEvent((LivingEntity) this.getBukkitEntity()); + event.setCancelled(itemstack == null); + this.world.getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + itemstack1.subtract(1); + // CraftBukkit end if (this instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer) this; @@ -859,7 +1010,7 @@ this.world.broadcastEntityEffect(this, (byte) 35); } - return itemstack != null; + return !event.isCancelled(); } } @@ -944,6 +1095,12 @@ boolean flag = this.lastDamageByPlayerTime > 0; this.a(flag, i, damagesource); + // CraftBukkit start - Call death event + CraftEventFactory.callEntityDeathEvent(this, this.drops); + this.drops = new ArrayList<org.bukkit.inventory.ItemStack>(); + } else { + CraftEventFactory.callEntityDeathEvent(this); + // CraftBukkit end } } @@ -1033,8 +1190,13 @@ int i = MathHelper.f((f - 3.0F - f2) * f1); if (i > 0) { + // CraftBukkit start + if (!this.damageEntity(DamageSource.FALL, (float) i)) { + return; + } + // CraftBukkit end this.a(this.e(i), 1.0F, 1.0F); - this.damageEntity(DamageSource.FALL, (float) i); + // this.damageEntity(DamageSource.FALL, (float) i); // CraftBukkit - moved up int j = MathHelper.floor(this.locX); int k = MathHelper.floor(this.locY - 0.20000000298023224D); int l = MathHelper.floor(this.locZ); @@ -1061,7 +1223,7 @@ protected float applyArmorModifier(DamageSource damagesource, float f) { if (!damagesource.ignoresArmor()) { - this.damageArmor(f); + // this.damageArmor(f); // CraftBukkit - Moved into damageEntity0(DamageSource, float) f = CombatMath.a(f, (float) this.getArmorStrength(), (float) this.getAttributeInstance(GenericAttributes.i).getValue()); } @@ -1074,7 +1236,8 @@ } else { int i; - if (this.hasEffect(MobEffects.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { + // CraftBukkit - Moved to damageEntity0(DamageSource, float) + if (false && this.hasEffect(MobEffects.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { i = (this.getEffect(MobEffects.RESISTANCE).getAmplifier() + 1) * 5; int j = 25 - i; float f1 = f * (float) j; @@ -1095,22 +1258,142 @@ } } - protected void damageEntity0(DamageSource damagesource, float f) { - if (!this.isInvulnerable(damagesource)) { - f = this.applyArmorModifier(damagesource, f); - f = this.applyMagicModifier(damagesource, f); - float f1 = f; - - f = Math.max(f - this.getAbsorptionHearts(), 0.0F); - this.setAbsorptionHearts(this.getAbsorptionHearts() - (f1 - f)); - if (f != 0.0F) { + // CraftBukkit start + protected boolean damageEntity0(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(EnumItemSlot.HEAD).isEmpty()) { + 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) { + return -((EntityLiving.this.applyBlockingModifier(damagesource)) ? f : 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(MobEffects.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { + int i = (EntityLiving.this.getEffect(MobEffects.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(EnumItemSlot.HEAD) != null) { + this.getEquipment(EnumItemSlot.HEAD).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); + } + + // Apply blocking code // PAIL: steal from above + if (event.getDamage(DamageModifier.BLOCKING) < 0) { + this.damageShield((float) -event.getDamage(DamageModifier.BLOCKING)); + Entity entity = damagesource.i(); + + if (entity instanceof EntityLiving) { + this.c((EntityLiving) entity); + } + } + + absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION); + this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F)); + if (f > 0 || !human) { + 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.z, Math.round(f * 10.0F)); + } + } + // CraftBukkit end float f2 = this.getHealth(); this.setHealth(f2 - f); this.getCombatTracker().trackDamage(damagesource, f2, f); - this.setAbsorptionHearts(this.getAbsorptionHearts() - f); + // CraftBukkit start + if (!human) { + this.setAbsorptionHearts(this.getAbsorptionHearts() - f); + } + + return true; + } else { + // Duplicate triggers if blocking + if (event.getDamage(DamageModifier.BLOCKING) < 0) { + if (this instanceof EntityPlayer) { + CriterionTriggers.h.a((EntityPlayer) this, damagesource, f, originalDamage, true); + } + + if (damagesource.getEntity() instanceof EntityPlayer) { + CriterionTriggers.g.a((EntityPlayer) damagesource.getEntity(), this, damagesource, f, originalDamage, true); + } + + return false; + } else { + return originalDamage > 0; + } + // CraftBukkit end } } + return false; // CraftBukkit } public CombatTracker getCombatTracker() { @@ -1177,6 +1460,7 @@ public AttributeMapBase getAttributeMap() { if (this.attributeMap == null) { this.attributeMap = new AttributeMapServer(); + this.craftAttributes = new CraftAttributeMap(attributeMap); // CraftBukkit } return this.attributeMap; @@ -1469,6 +1753,7 @@ } if (this.onGround && !this.world.isClientSide) { + if (getFlag(7) && !CraftEventFactory.callToggleGlideEvent(this, false).isCancelled()) // CraftBukkit this.setFlag(7, false); } } else { @@ -1838,6 +2123,7 @@ } if (!this.world.isClientSide) { + if (flag != this.getFlag(7) && !CraftEventFactory.callToggleGlideEvent(this, flag).isCancelled()) // CraftBukkit this.setFlag(7, flag); } @@ -1931,11 +2217,11 @@ } public boolean isInteractable() { - return !this.dead; + return !this.dead && this.collides; // CraftBukkit } public boolean isCollidable() { - return this.isAlive() && !this.m_(); + return this.isAlive() && !this.m_() && this.collides; // CraftBukkit } protected void ax() { @@ -2072,7 +2358,27 @@ protected void v() { if (!this.activeItem.isEmpty() && this.isHandRaised()) { this.b(this.activeItem, 16); - this.a(this.cH(), this.activeItem.a(this.world, this)); + // CraftBukkit start - fire PlayerItemConsumeEvent + ItemStack itemstack; + if (this instanceof EntityPlayer) { + org.bukkit.inventory.ItemStack craftItem = CraftItemStack.asBukkitCopy(this.activeItem); + PlayerItemConsumeEvent event = new PlayerItemConsumeEvent((Player) this.getBukkitEntity(), craftItem); + world.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + // Update client + ((EntityPlayer) this).getBukkitEntity().updateInventory(); + ((EntityPlayer) this).getBukkitEntity().updateScaledHealth(); + return; + } + + itemstack = (craftItem.equals(event.getItem())) ? this.activeItem.a(this.world, this) : CraftItemStack.asNMSCopy(event.getItem()).a(world, this); + } else { + itemstack = this.activeItem.a(this.world, this); + } + + this.a(this.cH(), itemstack); + // CraftBukkit end this.cN(); } @@ -2151,10 +2457,18 @@ } if (flag1) { - this.enderTeleportTo(this.locX, this.locY, this.locZ); - if (world.getCubes(this, this.getBoundingBox()).isEmpty() && !world.containsLiquid(this.getBoundingBox())) { - flag = true; + // CraftBukkit start - Teleport event + // this.enderTeleportTo(this.locX, this.locY, this.locZ); + EntityTeleportEvent teleport = new EntityTeleportEvent(this.getBukkitEntity(), new Location(this.world.getWorld(), d3, d4, d5), new Location(this.world.getWorld(), this.locX, this.locY, this.locZ)); + this.world.getServer().getPluginManager().callEvent(teleport); + if (!teleport.isCancelled()) { + Location to = teleport.getTo(); + this.enderTeleportTo(to.getX(), to.getY(), to.getZ()); + if (world.getCubes(this, this.getBoundingBox()).isEmpty() && !world.containsLiquid(this.getBoundingBox())) { + flag = true; + } } + // CraftBukkit end } }