--- a/net/minecraft/world/entity/EntityLiving.java +++ b/net/minecraft/world/entity/EntityLiving.java @@ -119,6 +119,30 @@ import net.minecraft.world.scores.ScoreboardTeam; import org.apache.logging.log4j.Logger; +// CraftBukkit start +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import com.google.common.base.Function; +import net.minecraft.nbt.NBTTagFloat; +import net.minecraft.nbt.NBTTagInt; +import net.minecraft.world.entity.animal.EntityAnimal; +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.ArrowBodyCountChangeEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; +import org.bukkit.event.entity.EntityPotionEffectEvent; +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 UUID SPEED_MODIFIER_SPRINTING_UUID = UUID.fromString("662A6B8D-DA3E-4C1C-8813-96EA6097278D"); @@ -227,6 +251,21 @@ private float swimAmount; private float swimAmountO; protected BehaviorController brain; + // CraftBukkit start + public int expToDrop; + public int maxAirTicks = 300; + public boolean forceDrops; + public ArrayList drops = new ArrayList(); + public final org.bukkit.craftbukkit.attribute.CraftAttributeMap craftAttributes; + public boolean collides = true; + public Set collidableExemptions = new HashSet<>(); + public boolean bukkitPickUpLoot; + + @Override + public float getBukkitYaw() { + return getHeadRotation(); + } + // CraftBukkit end protected EntityLiving(EntityTypes entitytypes, World world) { super(entitytypes, world); @@ -239,7 +278,9 @@ this.useItem = ItemStack.EMPTY; this.lastClimbablePos = Optional.empty(); this.attributes = new AttributeMapBase(AttributeDefaults.a(entitytypes)); - this.setHealth(this.getMaxHealth()); + this.craftAttributes = new CraftAttributeMap(attributes); // CraftBukkit + // CraftBukkit - setHealth(getMaxHealth()) inlined and simplified to skip the instanceof check for EntityPlayer, as getBukkitEntity() is not initialized in constructor + this.entityData.set(EntityLiving.DATA_HEALTH_ID, (float) this.getAttributeInstance(GenericAttributes.MAX_HEALTH).getValue()); this.blocksBuilding = true; this.rotA = (float) ((Math.random() + 1.0D) * 0.009999999776482582D); this.ah(); @@ -306,7 +347,13 @@ double d1 = Math.min((double) (0.2F + f / 15.0F), 2.5D); int i = (int) (150.0D * d1); - ((WorldServer) this.level).a(new ParticleParamBlock(Particles.BLOCK, iblockdata), this.locX(), this.locY(), this.locZ(), i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D); + // CraftBukkit start - visiblity api + if (this instanceof EntityPlayer) { + ((WorldServer) this.level).sendParticles((EntityPlayer) this, new ParticleParamBlock(Particles.BLOCK, iblockdata), this.locX(), this.locY(), this.locZ(), i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, false); + } else { + ((WorldServer) this.level).a(new ParticleParamBlock(Particles.BLOCK, iblockdata), this.locX(), this.locY(), this.locZ(), i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D); + } + // CraftBukkit end } } @@ -566,7 +613,7 @@ protected void dB() { ++this.deathTime; - if (this.deathTime == 20 && !this.level.isClientSide()) { + if (this.deathTime >= 20 && !this.isRemoved() && !this.level.isClientSide()) { // CraftBukkit - (this.deathTicks == 20) -> (this.deathTicks >= 20 && !this.dead) this.level.broadcastEntityEffect(this, (byte) 60); this.a(Entity.RemovalReason.KILLED); } @@ -658,9 +705,15 @@ } protected void playEquipSound(ItemStack itemstack) { + // CraftBukkit start + this.playEquipSound(itemstack, false); + } + + protected void playEquipSound(ItemStack itemstack, boolean silent) { SoundEffect soundeffect = itemstack.M(); - if (!itemstack.isEmpty() && soundeffect != null && !this.isSpectator()) { + if (!itemstack.isEmpty() && soundeffect != null && !this.isSpectator() && !silent) { + // CraftBukkit end this.a(GameEvent.EQUIP); this.playSound(soundeffect, 1.0F, 1.0F); } @@ -722,6 +775,17 @@ } } + // CraftBukkit start + if (nbttagcompound.hasKey("Bukkit.MaxHealth")) { + NBTBase nbtbase = nbttagcompound.get("Bukkit.MaxHealth"); + if (nbtbase.getTypeId() == 5) { + this.getAttributeInstance(GenericAttributes.MAX_HEALTH).setValue(((NBTTagFloat) nbtbase).asDouble()); + } else if (nbtbase.getTypeId() == 3) { + this.getAttributeInstance(GenericAttributes.MAX_HEALTH).setValue(((NBTTagInt) nbtbase).asDouble()); + } + } + // CraftBukkit end + if (nbttagcompound.hasKeyOfType("Health", 99)) { this.setHealth(nbttagcompound.getFloat("Health")); } @@ -759,9 +823,32 @@ } + // CraftBukkit start + private boolean isTickingEffects = false; + private List effectsToProcess = Lists.newArrayList(); + + private static class ProcessableEffect { + + private MobEffectList type; + private MobEffect effect; + private final EntityPotionEffectEvent.Cause cause; + + private ProcessableEffect(MobEffect effect, EntityPotionEffectEvent.Cause cause) { + this.effect = effect; + this.cause = cause; + } + + private ProcessableEffect(MobEffectList type, EntityPotionEffectEvent.Cause cause) { + this.type = type; + this.cause = cause; + } + } + // CraftBukkit end + protected void tickPotionEffects() { Iterator iterator = this.activeEffects.keySet().iterator(); + isTickingEffects = true; // CraftBukkit try { while (iterator.hasNext()) { MobEffectList mobeffectlist = (MobEffectList) iterator.next(); @@ -771,6 +858,12 @@ this.a(mobeffect, true, (Entity) null); })) { if (!this.level.isClientSide) { + // CraftBukkit start + EntityPotionEffectEvent event = CraftEventFactory.callEntityPotionEffectChangeEvent(this, mobeffect, null, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.EXPIRATION); + if (event.isCancelled()) { + continue; + } + // CraftBukkit end iterator.remove(); this.a(mobeffect); } @@ -781,6 +874,17 @@ } catch (ConcurrentModificationException concurrentmodificationexception) { ; } + // CraftBukkit start + isTickingEffects = false; + for (ProcessableEffect e : effectsToProcess) { + if (e.effect != null) { + addEffect(e.effect, e.cause); + } else { + removeEffect(e.type, e.cause); + } + } + effectsToProcess.clear(); + // CraftBukkit end if (this.effectsDirty) { if (!this.level.isClientSide) { @@ -907,7 +1011,13 @@ this.entityData.set(EntityLiving.DATA_EFFECT_COLOR_ID, 0); } + // CraftBukkit start public boolean removeAllEffects() { + return removeAllEffects(org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); + } + + public boolean removeAllEffects(EntityPotionEffectEvent.Cause cause) { + // CraftBukkit end if (this.level.isClientSide) { return false; } else { @@ -916,7 +1026,14 @@ boolean flag; for (flag = false; iterator.hasNext(); flag = true) { - this.a((MobEffect) iterator.next()); + // CraftBukkit start + MobEffect effect = (MobEffect) iterator.next(); + EntityPotionEffectEvent event = CraftEventFactory.callEntityPotionEffectChangeEvent(this, effect, null, cause, EntityPotionEffectEvent.Action.CLEARED); + if (event.isCancelled()) { + continue; + } + this.a(effect); + // CraftBukkit end iterator.remove(); } @@ -945,18 +1062,48 @@ return this.addEffect(mobeffect, (Entity) null); } + // CraftBukkit start + public boolean addEffect(MobEffect mobeffect, EntityPotionEffectEvent.Cause cause) { + return this.addEffect(mobeffect, (Entity) null, cause); + } + public boolean addEffect(MobEffect mobeffect, @Nullable Entity entity) { + return this.addEffect(mobeffect, entity, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); + } + + public boolean addEffect(MobEffect mobeffect, @Nullable Entity entity, EntityPotionEffectEvent.Cause cause) { + if (isTickingEffects) { + effectsToProcess.add(new ProcessableEffect(mobeffect, cause)); + return true; + } + // CraftBukkit end + if (!this.c(mobeffect)) { return false; } else { MobEffect mobeffect1 = (MobEffect) this.activeEffects.get(mobeffect.getMobEffect()); + // CraftBukkit start + boolean override = false; + if (mobeffect1 != null) { + override = new MobEffect(mobeffect1).b(mobeffect); + } + + EntityPotionEffectEvent event = CraftEventFactory.callEntityPotionEffectChangeEvent(this, mobeffect1, mobeffect, cause, override); + if (event.isCancelled()) { + return false; + } + // CraftBukkit end + if (mobeffect1 == null) { this.activeEffects.put(mobeffect.getMobEffect(), mobeffect); this.a(mobeffect, entity); return true; - } else if (mobeffect1.b(mobeffect)) { + // CraftBukkit start + } else if (event.isOverride()) { + mobeffect1.b(mobeffect); this.a(mobeffect1, true, entity); + // CraftBukkit end return true; } else { return false; @@ -993,13 +1140,39 @@ return this.getMonsterType() == EnumMonsterType.UNDEAD; } + // CraftBukkit start @Nullable public MobEffect c(@Nullable MobEffectList mobeffectlist) { + return c(mobeffectlist, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); + } + + @Nullable + public MobEffect c(@Nullable MobEffectList mobeffectlist, EntityPotionEffectEvent.Cause cause) { + if (isTickingEffects) { + effectsToProcess.add(new ProcessableEffect(mobeffectlist, cause)); + return null; + } + + MobEffect effect = this.activeEffects.get(mobeffectlist); + if (effect == null) { + return null; + } + + EntityPotionEffectEvent event = CraftEventFactory.callEntityPotionEffectChangeEvent(this, effect, null, cause); + if (event.isCancelled()) { + return null; + } + return (MobEffect) this.activeEffects.remove(mobeffectlist); } public boolean removeEffect(MobEffectList mobeffectlist) { - MobEffect mobeffect = this.c(mobeffectlist); + return removeEffect(mobeffectlist, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); + } + + public boolean removeEffect(MobEffectList mobeffectlist, EntityPotionEffectEvent.Cause cause) { + MobEffect mobeffect = this.c(mobeffectlist, cause); + // CraftBukkit end if (mobeffect != null) { this.a(mobeffect); @@ -1036,20 +1209,55 @@ } + // 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); + // Suppress during worldgen + if (this.valid) { + this.level.getServer().getPluginManager().callEvent(event); + } + + if (!event.isCancelled()) { + this.setHealth((float) (this.getHealth() + event.getAmount())); + } + // CraftBukkit end } } public float getHealth() { + // CraftBukkit start - Use unscaled health + if (this instanceof EntityPlayer) { + return (float) ((EntityPlayer) this).getBukkitEntity().getHealth(); + } + // CraftBukkit end return (Float) this.entityData.get(EntityLiving.DATA_HEALTH_ID); } 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); + } + + player.updateScaledHealth(false); + return; + } + // CraftBukkit end this.entityData.set(EntityLiving.DATA_HEALTH_ID, MathHelper.a(f, 0.0F, this.getMaxHealth())); } @@ -1063,7 +1271,7 @@ return false; } else if (this.level.isClientSide) { return false; - } else if (this.dV()) { + } else if (this.isRemoved() || this.dead || this.getHealth() <= 0.0F) { // CraftBukkit - Don't allow entities that got set to dead/killed elsewhere to get damaged and die return false; } else if (damagesource.isFire() && this.hasEffect(MobEffects.FIRE_RESISTANCE)) { return false; @@ -1074,10 +1282,11 @@ this.noActionTime = 0; float f1 = f; - boolean flag = false; + boolean flag = f > 0.0F && this.applyBlockingModifier(damagesource); // Copied from below float f2 = 0.0F; - if (f > 0.0F && this.applyBlockingModifier(damagesource)) { + // CraftBukkit - Moved into damageEntity0(DamageSource, float) + if (false && f > 0.0F && this.applyBlockingModifier(damagesource)) { this.damageShield(f); f2 = f; f = 0.0F; @@ -1095,27 +1304,47 @@ this.animationSpeed = 1.5F; boolean flag1 = true; - if ((float) this.invulnerableTime > 10.0F) { + if ((float) this.invulnerableTime > (float) this.invulnerableDuration / 2.0F) { // CraftBukkit - restore use of maxNoDamageTicks if (f <= this.lastHurt) { + this.forceExplosionKnockback = true; // CraftBukkit - SPIGOT-949 - for vanilla consistency, cooldown does not prevent explosion knockback return false; } - this.damageEntity0(damagesource, f - this.lastHurt); + // CraftBukkit start + if (!this.damageEntity0(damagesource, f - this.lastHurt)) { + return false; + } + // CraftBukkit end this.lastHurt = f; flag1 = false; } else { + // CraftBukkit start + if (!this.damageEntity0(damagesource, f)) { + return false; + } this.lastHurt = f; - this.invulnerableTime = 20; - this.damageEntity0(damagesource, f); + this.invulnerableTime = this.invulnerableDuration; // CraftBukkit - restore use of maxNoDamageTicks + // this.damageEntity0(damagesource, f); + // CraftBukkit end this.hurtDuration = 10; this.hurtTime = this.hurtDuration; } - if (damagesource.g() && !this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { + // CraftBukkit - Moved into damageEntity0(DamageSource, float) + if (false && damagesource.g() && !this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { this.damageHelmet(damagesource, f); f *= 0.75F; } + // CraftBukkit start + if (this instanceof EntityAnimal) { + ((EntityAnimal) this).resetLove(); + if (this instanceof EntityTameableAnimal) { + ((EntityTameableAnimal) this).setWillSit(false); + } + } + // CraftBukkit end + this.hurtDir = 0.0F; Entity entity1 = damagesource.getEntity(); @@ -1238,19 +1467,29 @@ EnumHand[] aenumhand = EnumHand.values(); int i = aenumhand.length; + // CraftBukkit start + ItemStack itemstack1 = ItemStack.EMPTY; for (int j = 0; j < i; ++j) { EnumHand enumhand = aenumhand[j]; - ItemStack itemstack1 = this.b(enumhand); + itemstack1 = this.b(enumhand); if (itemstack1.a(Items.TOTEM_OF_UNDYING)) { itemstack = itemstack1.cloneItemStack(); - itemstack1.subtract(1); + // itemstack1.subtract(1); // CraftBukkit break; } } - if (itemstack != null) { - if (this instanceof EntityPlayer) { + EntityResurrectEvent event = new EntityResurrectEvent((LivingEntity) this.getBukkitEntity()); + event.setCancelled(itemstack == null); + this.level.getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + if (!itemstack1.isEmpty()) { + itemstack1.subtract(1); + } + if (itemstack != null && this instanceof EntityPlayer) { + // CraftBukkit end EntityPlayer entityplayer = (EntityPlayer) this; entityplayer.b(StatisticList.ITEM_USED.b(Items.TOTEM_OF_UNDYING)); @@ -1258,14 +1497,16 @@ } this.setHealth(1.0F); - this.removeAllEffects(); - this.addEffect(new MobEffect(MobEffects.REGENERATION, 900, 1)); - this.addEffect(new MobEffect(MobEffects.ABSORPTION, 100, 1)); - this.addEffect(new MobEffect(MobEffects.FIRE_RESISTANCE, 800, 0)); + // CraftBukkit start + this.removeAllEffects(org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TOTEM); + this.addEffect(new MobEffect(MobEffects.REGENERATION, 900, 1), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TOTEM); + this.addEffect(new MobEffect(MobEffects.ABSORPTION, 100, 1), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TOTEM); + this.addEffect(new MobEffect(MobEffects.FIRE_RESISTANCE, 800, 0), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TOTEM); + // CraftBukkit end this.level.broadcastEntityEffect(this, (byte) 35); } - return itemstack != null; + return !event.isCancelled(); } } @@ -1374,6 +1615,13 @@ if (!flag) { EntityItem entityitem = new EntityItem(this.level, this.locX(), this.locY(), this.locZ(), new ItemStack(Items.WITHER_ROSE)); + // CraftBukkit start + org.bukkit.event.entity.EntityDropItemEvent event = new org.bukkit.event.entity.EntityDropItemEvent(this.getBukkitEntity(), (org.bukkit.entity.Item) entityitem.getBukkitEntity()); + CraftEventFactory.callEvent(event); + if (event.isCancelled()) { + return; + } + // CraftBukkit end this.level.addEntity(entityitem); } } @@ -1393,21 +1641,40 @@ boolean flag = this.lastHurtByPlayerTime > 0; + this.dropInventory(); // CraftBukkit - from below if (this.dD() && this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT)) { this.a(damagesource, flag); this.dropDeathLoot(damagesource, i, flag); } + // CraftBukkit start - Call death event + CraftEventFactory.callEntityDeathEvent(this, this.drops); + this.drops = new ArrayList<>(); + // CraftBukkit end - this.dropInventory(); + // this.dropInventory();// CraftBukkit - moved up this.dropExperience(); } protected void dropInventory() {} - protected void dropExperience() { + // CraftBukkit start + public int getExpReward() { if (this.level instanceof WorldServer && (this.alwaysGivesExp() || this.lastHurtByPlayerTime > 0 && this.isDropExperience() && this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT))) { - EntityExperienceOrb.a((WorldServer) this.level, this.getPositionVector(), this.getExpValue(this.lastHurtByPlayer)); + int i = this.getExpValue(this.lastHurtByPlayer); + return i; + } else { + return 0; + } + } + // CraftBukkit end + + protected void dropExperience() { + // CraftBukkit start - Update getExpReward() above if the removed if() changes! + if (true) { + EntityExperienceOrb.a((WorldServer) this.level, this.getPositionVector(), this.expToDrop); + this.expToDrop = 0; } + // CraftBukkit end } @@ -1523,9 +1790,14 @@ int i = this.d(f, f1); if (i > 0) { + // CraftBukkit start + if (!this.damageEntity(damagesource, (float) i)) { + return true; + } + // CraftBukkit end this.playSound(this.getSoundFall(i), 1.0F, 1.0F); this.playBlockStepSound(); - this.damageEntity(damagesource, (float) i); + // this.damageEntity(damagesource, (float) i); // CraftBukkit - moved up return true; } else { return flag; @@ -1574,7 +1846,7 @@ protected float applyArmorModifier(DamageSource damagesource, float f) { if (!damagesource.ignoresArmor()) { - this.damageArmor(damagesource, f); + // this.damageArmor(damagesource, f); // CraftBukkit - Moved into damageEntity0(DamageSource, float) f = CombatMath.a(f, (float) this.getArmorStrength(), (float) this.b(GenericAttributes.ARMOR_TOUGHNESS)); } @@ -1587,7 +1859,8 @@ } else { int i; - if (this.hasEffect(MobEffects.DAMAGE_RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { + // CraftBukkit - Moved to damageEntity0(DamageSource, float) + if (false && this.hasEffect(MobEffects.DAMAGE_RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { i = (this.getEffect(MobEffects.DAMAGE_RESISTANCE).getAmplifier() + 1) * 5; int j = 25 - i; float f1 = f * (float) j; @@ -1618,29 +1891,172 @@ } } - protected void damageEntity0(DamageSource damagesource, float f) { - if (!this.isInvulnerable(damagesource)) { - f = this.applyArmorModifier(damagesource, f); - f = this.applyMagicModifier(damagesource, f); - float f1 = f; + // 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 hardHat = new Function() { + @Override + public Double apply(Double f) { + if (damagesource.g() && !EntityLiving.this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { + return -(f - (f * 0.75F)); + + } + return -0.0; + } + }; + float hardHatModifier = hardHat.apply((double) f).floatValue(); + f += hardHatModifier; + + Function blocking = new Function() { + @Override + public Double apply(Double f) { + return -((EntityLiving.this.applyBlockingModifier(damagesource)) ? f : 0.0); + } + }; + float blockingModifier = blocking.apply((double) f).floatValue(); + f += blockingModifier; + + Function armor = new Function() { + @Override + public Double apply(Double f) { + return -(f - EntityLiving.this.applyArmorModifier(damagesource, f.floatValue())); + } + }; + float armorModifier = armor.apply((double) f).floatValue(); + f += armorModifier; + + Function resistance = new Function() { + @Override + public Double apply(Double f) { + if (!damagesource.isStarvation() && EntityLiving.this.hasEffect(MobEffects.DAMAGE_RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) { + int i = (EntityLiving.this.getEffect(MobEffects.DAMAGE_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 magic = new Function() { + @Override + public Double apply(Double f) { + return -(f - EntityLiving.this.applyMagicModifier(damagesource, f.floatValue())); + } + }; + float magicModifier = magic.apply((double) f).floatValue(); + f += magicModifier; + + Function absorption = new Function() { + @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 (damagesource.getEntity() instanceof EntityHuman) { + ((EntityHuman) damagesource.getEntity()).resetAttackCooldown(); // Moved from EntityHuman in order to make the cooldown reset get called after the damage event is fired + } + if (event.isCancelled()) { + return false; + } - f = Math.max(f - this.getAbsorptionHearts(), 0.0F); - this.setAbsorptionHearts(this.getAbsorptionHearts() - (f1 - f)); - float f2 = f1 - f; + f = (float) event.getFinalDamage(); + // Resistance + if (event.getDamage(DamageModifier.RESISTANCE) < 0) { + float f3 = (float) -event.getDamage(DamageModifier.RESISTANCE); + if (f3 > 0.0F && f3 < 3.4028235E37F) { + if (this instanceof EntityPlayer) { + ((EntityPlayer) this).a(StatisticList.DAMAGE_RESISTED, Math.round(f3 * 10.0F)); + } else if (damagesource.getEntity() instanceof EntityPlayer) { + ((EntityPlayer) damagesource.getEntity()).a(StatisticList.DAMAGE_DEALT_RESISTED, Math.round(f3 * 10.0F)); + } + } + } + + // Apply damage to helmet + if (damagesource.g() && !this.getEquipment(EnumItemSlot.HEAD).isEmpty()) { + this.damageHelmet(damagesource, f); + } + + // Apply damage to armor + if (!damagesource.ignoresArmor()) { + float armorDamage = (float) (event.getDamage() + event.getDamage(DamageModifier.BLOCKING) + event.getDamage(DamageModifier.HARD_HAT)); + this.damageArmor(damagesource, armorDamage); + } + + // Apply blocking code // PAIL: steal from above + if (event.getDamage(DamageModifier.BLOCKING) < 0) { + this.level.broadcastEntityEffect(this, (byte) 29); // SPIGOT-4635 - shield damage sound + this.damageShield((float) -event.getDamage(DamageModifier.BLOCKING)); + Entity entity = damagesource.k(); + + if (entity instanceof EntityLiving) { + this.shieldBlock((EntityLiving) entity); + } + } + + absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION); + this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F)); + float f2 = absorptionModifier; + + if (f2 > 0.0F && f2 < 3.4028235E37F && this instanceof EntityHuman) { + ((EntityHuman) this).a(StatisticList.DAMAGE_ABSORBED, Math.round(f2 * 10.0F)); + } if (f2 > 0.0F && f2 < 3.4028235E37F && damagesource.getEntity() instanceof EntityPlayer) { ((EntityPlayer) damagesource.getEntity()).a(StatisticList.DAMAGE_DEALT_ABSORBED, Math.round(f2 * 10.0F)); } - if (f != 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(), org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.DAMAGED); // CraftBukkit - EntityExhaustionEvent + if (f < 3.4028235E37F) { + ((EntityHuman) this).a(StatisticList.DAMAGE_TAKEN, Math.round(f * 10.0F)); + } + } + // CraftBukkit end float f3 = this.getHealth(); this.setHealth(f3 - f); this.getCombatTracker().trackDamage(damagesource, f3, f); - this.setAbsorptionHearts(this.getAbsorptionHearts() - f); + // CraftBukkit start + if (!human) { + this.setAbsorptionHearts(this.getAbsorptionHearts() - f); + } this.a(GameEvent.ENTITY_DAMAGED, damagesource.getEntity()); + + return true; + } else { + // Duplicate triggers if blocking + if (event.getDamage(DamageModifier.BLOCKING) < 0) { + if (this instanceof EntityPlayer) { + CriterionTriggers.ENTITY_HURT_PLAYER.a((EntityPlayer) this, damagesource, f, originalDamage, true); + f2 = (float) -event.getDamage(DamageModifier.BLOCKING); + if (f2 > 0.0F && f2 < 3.4028235E37F) { + ((EntityPlayer) this).a(StatisticList.DAMAGE_BLOCKED_BY_SHIELD, Math.round(originalDamage * 10.0F)); + } + } + + if (damagesource.getEntity() instanceof EntityPlayer) { + CriterionTriggers.PLAYER_HURT_ENTITY.a((EntityPlayer) damagesource.getEntity(), this, damagesource, f, originalDamage, true); + } + + return false; + } else { + return originalDamage > 0; + } + // CraftBukkit end } } + return false; // CraftBukkit } public CombatTracker getCombatTracker() { @@ -1661,8 +2077,18 @@ } public final void setArrowCount(int i) { - this.entityData.set(EntityLiving.DATA_ARROW_COUNT_ID, i); + // CraftBukkit start + setArrowCount(i, false); + } + + public final void setArrowCount(int i, boolean flag) { + ArrowBodyCountChangeEvent event = CraftEventFactory.callArrowBodyCountChangeEvent(this, getArrowCount(), i, flag); + if (event.isCancelled()) { + return; + } + this.entityData.set(EntityLiving.DATA_ARROW_COUNT_ID, event.getNewAmount()); } + // CraftBukkit end public final int eh() { return (Integer) this.entityData.get(EntityLiving.DATA_STINGER_COUNT_ID); @@ -1958,6 +2384,12 @@ public abstract ItemStack getEquipment(EnumItemSlot enumitemslot); + // CraftBukkit start + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack, boolean silent) { + this.setSlot(enumitemslot, itemstack); + } + // CraftBukkit end + @Override public abstract void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack); @@ -2202,6 +2634,7 @@ } if (this.onGround && !this.level.isClientSide) { + if (getFlag(7) && !CraftEventFactory.callToggleGlideEvent(this, false).isCancelled()) // CraftBukkit this.setFlag(7, false); } } else { @@ -2732,6 +3165,7 @@ } if (!this.level.isClientSide) { + if (flag != this.getFlag(7) && !CraftEventFactory.callToggleGlideEvent(this, flag).isCancelled()) // CraftBukkit this.setFlag(7, flag); } @@ -2899,14 +3333,21 @@ @Override public boolean isInteractable() { - return !this.isRemoved(); + return !this.isRemoved() && this.collides; // CraftBukkit } @Override public boolean isCollidable() { - return this.isAlive() && !this.isSpectator() && !this.isClimbing(); + return this.isAlive() && !this.isSpectator() && !this.isClimbing() && this.collides; // CraftBukkit } + // CraftBukkit start - collidable API + @Override + public boolean canCollideWithBukkit(Entity entity) { + return isCollidable() && this.collides != this.collidableExemptions.contains(entity.getUniqueID()); + } + // CraftBukkit end + @Override protected void velocityChanged() { this.hurtMarked = this.random.nextDouble() >= this.b(GenericAttributes.KNOCKBACK_RESISTANCE); @@ -3104,7 +3545,25 @@ } else { if (!this.useItem.isEmpty() && this.isHandRaised()) { this.b(this.useItem, 16); - ItemStack itemstack = this.useItem.a(this.level, this); + // CraftBukkit start - fire PlayerItemConsumeEvent + ItemStack itemstack; + if (this instanceof EntityPlayer) { + org.bukkit.inventory.ItemStack craftItem = CraftItemStack.asBukkitCopy(this.useItem); + PlayerItemConsumeEvent event = new PlayerItemConsumeEvent((Player) this.getBukkitEntity(), craftItem); + level.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + // Update client + ((EntityPlayer) this).getBukkitEntity().updateInventory(); + ((EntityPlayer) this).getBukkitEntity().updateScaledHealth(); + return; + } + + itemstack = (craftItem.equals(event.getItem())) ? this.useItem.a(this.level, this) : CraftItemStack.asNMSCopy(event.getItem()).a(level, this); + } else { + itemstack = this.useItem.a(this.level, this); + } + // CraftBukkit end if (itemstack != this.useItem) { this.a(enumhand, itemstack); @@ -3176,6 +3635,12 @@ } public boolean a(double d0, double d1, double d2, boolean flag) { + // CraftBukkit start + return safeTeleport(d0, d1, d2, flag, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.UNKNOWN).orElse(false); + } + + public Optional safeTeleport(double d0, double d1, double d2, boolean flag, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause) { + // CraftBukkit end double d3 = this.locX(); double d4 = this.locY(); double d5 = this.locZ(); @@ -3200,16 +3665,41 @@ } if (flag2) { - this.enderTeleportTo(d0, d6, d2); + // CraftBukkit start - Teleport event + // this.enderTeleportTo(d0, d6, d2); + + // first set position, to check if the place to teleport is valid + this.setPosition(d0, d6, d2); if (world.getCubes(this) && !world.containsLiquid(this.getBoundingBox())) { flag1 = true; } + // now revert and call event if the teleport place is valid + this.setPosition(d3, d4, d5); + + if (flag1) { + if (!(this instanceof EntityPlayer)) { + EntityTeleportEvent teleport = new EntityTeleportEvent(this.getBukkitEntity(), new Location(this.level.getWorld(), d3, d4, d5), new Location(this.level.getWorld(), d0, d6, d2)); + this.level.getServer().getPluginManager().callEvent(teleport); + if (!teleport.isCancelled()) { + Location to = teleport.getTo(); + this.enderTeleportTo(to.getX(), to.getY(), to.getZ()); + } else { + return Optional.empty(); + } + } else { + // player teleport event is called in the underlining code + if (((EntityPlayer) this).connection.a(d0, d6, d2, this.getYRot(), this.getXRot(), java.util.Collections.emptySet(), false, cause)) { + return Optional.empty(); + } + } + } + // CraftBukkit end } } if (!flag1) { - this.enderTeleportTo(d3, d4, d5); - return false; + // this.enderTeleportTo(d3, d4, d5); // CraftBukkit - already set the location back + return Optional.of(false); // CraftBukkit } else { if (flag) { world.broadcastEntityEffect(this, (byte) 46); @@ -3219,7 +3709,7 @@ ((EntityCreature) this).getNavigation().o(); } - return true; + return Optional.of(true); // CraftBukkit } } @@ -3302,7 +3792,7 @@ } public void entityWakeup() { - Optional optional = this.getBedPosition(); + Optional optional = this.getBedPosition(); // CraftBukkit - decompile error World world = this.level; java.util.Objects.requireNonNull(this.level); @@ -3334,7 +3824,7 @@ @Nullable public EnumDirection eW() { - BlockPosition blockposition = (BlockPosition) this.getBedPosition().orElse((Object) null); + BlockPosition blockposition = (BlockPosition) this.getBedPosition().orElse(null); // CraftBukkit - decompile error return blockposition != null ? BlockBed.a((IBlockAccess) this.level, blockposition) : null; } @@ -3383,7 +3873,7 @@ Pair pair = (Pair) iterator.next(); if (!world.isClientSide && pair.getFirst() != null && world.random.nextFloat() < (Float) pair.getSecond()) { - entityliving.addEffect(new MobEffect((MobEffect) pair.getFirst())); + entityliving.addEffect(new MobEffect((MobEffect) pair.getFirst()), EntityPotionEffectEvent.Cause.FOOD); // CraftBukkit } } }