--- a/net/minecraft/server/EntityLiving.java
+++ b/net/minecraft/server/EntityLiving.java
@@ -10,12 +10,25 @@
 import java.util.Random;
 import java.util.UUID;
 
+// CraftBukkit start
+import java.util.ArrayList;
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.entity.Vehicle;
+import org.bukkit.event.entity.EntityDamageEvent;
+import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
+import org.bukkit.event.entity.EntityRegainHealthEvent;
+import org.bukkit.event.vehicle.VehicleExitEvent;
+// CraftBukkit end
+
 public abstract class EntityLiving extends Entity {
 
     private static final UUID a = UUID.fromString("662A6B8D-DA3E-4C1C-8813-96EA6097278D");
     private static final AttributeModifier b = (new AttributeModifier(EntityLiving.a, "Sprinting speed boost", 0.30000001192092896D, 2)).a(false);
     private AttributeMapBase c;
-    public final CombatTracker combatTracker = new CombatTracker(this);
+    public CombatTracker combatTracker = new CombatTracker(this);
     public final Map<Integer, MobEffect> effects = Maps.newHashMap();
     private final ItemStack[] h = new ItemStack[5];
     public boolean ar;
@@ -69,6 +82,11 @@
     private float bm;
     private int bn;
     private float bo;
+    // CraftBukkit start
+    public int expToDrop;
+    public int maxAirTicks = 300;
+    ArrayList<org.bukkit.inventory.ItemStack> drops = null;
+    // CraftBukkit end
 
     public void G() {
         this.damageEntity(DamageSource.OUT_OF_WORLD, Float.MAX_VALUE);
@@ -77,7 +95,8 @@
     public EntityLiving(World world) {
         super(world);
         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.watch(6, (float) this.getAttributeInstance(GenericAttributes.maxHealth).getValue());
         this.k = true;
         this.aH = (float) ((Math.random() + 1.0D) * 0.009999999776482582D);
         this.setPosition(this.locX, this.locY, this.locZ);
@@ -119,7 +138,13 @@
 
                 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
             }
         }
 
@@ -177,7 +202,11 @@
                     this.mount((Entity) null);
                 }
             } 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
             }
         }
 
@@ -225,25 +254,36 @@
         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.ba() && this.world.getGameRules().getBoolean("doMobLoot")) {
+            return exp;
+        } else {
+            return 0;
+        }
+    }
+    // CraftBukkit end
+
     public boolean isBaby() {
         return false;
     }
 
     protected void aZ() {
         ++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.lastDamageByPlayerTime > 0 || this.alwaysGivesExp()) && this.ba() && this.world.getGameRules().getBoolean("doMobLoot")) {
-                i = this.getExpValue(this.killer);
-
-                while (i > 0) {
-                    int j = EntityExperienceOrb.getOrbValue(i);
-
-                    i -= j;
-                    this.world.addEntity(new EntityExperienceOrb(this.world, this.locX, this.locY, this.locZ, j));
-                }
+            // 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));
             }
+            this.expToDrop = 0;
+            // CraftBukkit end
 
             this.die();
 
@@ -380,6 +420,17 @@
             }
         }
 
+        // CraftBukkit start
+        if (nbttagcompound.hasKey("Bukkit.MaxHealth")) {
+            NBTBase nbtbase = nbttagcompound.get("Bukkit.MaxHealth");
+            if (nbtbase.getTypeId() == 5) {
+                this.getAttributeInstance(GenericAttributes.maxHealth).setValue((double) ((NBTTagFloat) nbtbase).c());
+            } else if (nbtbase.getTypeId() == 3) {
+                this.getAttributeInstance(GenericAttributes.maxHealth).setValue((double) ((NBTTagInt) nbtbase).d());
+            }
+        }
+        // CraftBukkit end
+
         if (nbttagcompound.hasKeyOfType("HealF", 99)) {
             this.setHealth(nbttagcompound.getFloat("HealF"));
         } else {
@@ -399,9 +450,15 @@
         this.hurtTimestamp = nbttagcompound.getInt("HurtByTimestamp");
     }
 
+    // CraftBukkit start
+    private boolean isTickingEffects = false;
+    private List<Object> effectsToProcess = Lists.newArrayList();
+    // CraftBukkit end
+
     protected void bi() {
         Iterator iterator = this.effects.keySet().iterator();
 
+        isTickingEffects = true; // CraftBukkit
         while (iterator.hasNext()) {
             Integer integer = (Integer) iterator.next();
             MobEffect mobeffect = (MobEffect) this.effects.get(integer);
@@ -415,6 +472,16 @@
                 this.a(mobeffect, false);
             }
         }
+        // CraftBukkit start
+        isTickingEffects = false;
+        for (Object e : effectsToProcess) {
+            if (e instanceof MobEffect) {
+                addEffect((MobEffect) e);
+            } else {
+                removeEffect((Integer) e);
+            }
+        }
+        // CraftBukkit end
 
         if (this.updateEffects) {
             if (!this.world.isClientSide) {
@@ -490,7 +557,8 @@
     }
 
     public boolean hasEffect(int i) {
-        return this.effects.containsKey(Integer.valueOf(i));
+        // CraftBukkit - Add size check for efficiency
+        return this.effects.size() != 0 && this.effects.containsKey(Integer.valueOf(i));
     }
 
     public boolean hasEffect(MobEffectList mobeffectlist) {
@@ -502,6 +570,12 @@
     }
 
     public void addEffect(MobEffect mobeffect) {
+        // CraftBukkit start
+        if (isTickingEffects) {
+            effectsToProcess.add(mobeffect);
+            return;
+        }
+        // CraftBukkit end
         if (this.d(mobeffect)) {
             if (this.effects.containsKey(Integer.valueOf(mobeffect.getEffectId()))) {
                 ((MobEffect) this.effects.get(Integer.valueOf(mobeffect.getEffectId()))).a(mobeffect);
@@ -531,6 +605,12 @@
     }
 
     public void removeEffect(int i) {
+        // CraftBukkit start
+        if (isTickingEffects) {
+            effectsToProcess.add(i);
+            return;
+        }
+        // CraftBukkit end
         MobEffect mobeffect = (MobEffect) this.effects.remove(Integer.valueOf(i));
 
         if (mobeffect != null) {
@@ -564,20 +644,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 this.datawatcher.getFloat(6);
     }
 
     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.watch(6, Float.valueOf(player.getScaledHealth()));
+            return;
+        }
+        // CraftBukkit end
         this.datawatcher.watch(6, Float.valueOf(MathHelper.a(f, 0.0F, this.getMaxHealth())));
     }
 
@@ -593,7 +705,8 @@
             } else if (damagesource.o() && this.hasEffect(MobEffectList.FIRE_RESISTANCE)) {
                 return false;
             } else {
-                if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
+                // CraftBukkit - Moved into d(DamageSource, float)
+                if (false && (damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
                     this.getEquipment(4).damage((int) (f * 4.0F + this.random.nextFloat() * f * 2.0F), this);
                     f *= 0.75F;
                 }
@@ -606,16 +719,34 @@
                         return false;
                     }
 
-                    this.d(damagesource, f - this.lastDamage);
+                    // CraftBukkit start
+                    if (!this.d(damagesource, f - this.lastDamage)) {
+                        return false;
+                    }
+                    // CraftBukkit end
                     this.lastDamage = f;
                     flag = false;
                 } else {
+                    // CraftBukkit start
+                    float previousHealth = this.getHealth();
+                    if (!this.d(damagesource, f)) {
+                        return false;
+                    }
                     this.lastDamage = f;
                     this.noDamageTicks = this.maxNoDamageTicks;
-                    this.d(damagesource, f);
+                    // CraftBukkit end
                     this.hurtTicks = this.av = 10;
                 }
 
+                // CraftBukkit start
+                if(this instanceof EntityAnimal){
+                    ((EntityAnimal)this).cq();
+                    if(this instanceof EntityTameableAnimal){
+                        ((EntityTameableAnimal)this).getGoalSit().setSitting(false);
+                    }
+                }
+                // CraftBukkit end
+
                 this.aw = 0.0F;
                 Entity entity = damagesource.getEntity();
 
@@ -721,11 +852,19 @@
             }
 
             if (this.ba() && this.world.getGameRules().getBoolean("doMobLoot")) {
+                this.drops = new ArrayList<org.bukkit.inventory.ItemStack>(); // CraftBukkit - Setup drop capture
+                
                 this.dropDeathLoot(this.lastDamageByPlayerTime > 0, i);
                 this.dropEquipment(this.lastDamageByPlayerTime > 0, i);
                 if (this.lastDamageByPlayerTime > 0 && this.random.nextFloat() < 0.025F + (float) i * 0.01F) {
                     this.getRareDrop();
                 }
+                // CraftBukkit start - Call death event
+                CraftEventFactory.callEntityDeathEvent(this, this.drops);
+                this.drops = null;
+            } else {
+                CraftEventFactory.callEntityDeathEvent(this);
+                // CraftBukkit end
             }
         }
 
@@ -785,8 +924,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.makeSound(this.n(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);
@@ -830,7 +974,7 @@
             int i = 25 - this.br();
             float f1 = f * (float) i;
 
-            this.damageArmor(f);
+            // this.damageArmor(f); // CraftBukkit - Moved into d(DamageSource, float)
             f = f1 / 25.0F;
         }
 
@@ -844,8 +988,9 @@
             int i;
             int j;
             float f1;
-
-            if (this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
+            
+            // CraftBukkit - Moved to d(DamageSource, float)
+            if (false && this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
                 i = (this.getEffect(MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
                 j = 25 - i;
                 f1 = f * (float) j;
@@ -871,22 +1016,121 @@
         }
     }
 
-    protected void d(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 d(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(4) != null) {
+                        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) {
+                    if (human) {
+                        if (!damagesource.ignoresArmor() && ((EntityHuman) EntityLiving.this).isBlocking() && f > 0.0F) {
+                            return -(f - ((1.0F + f) * 0.5F));
+                        }
+                    }
+                    return -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(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
+                        int i = (EntityLiving.this.getEffect(MobEffectList.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(4) != null) {
+                this.getEquipment(4).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);
+            }
 
-            f = Math.max(f - this.getAbsorptionHearts(), 0.0F);
-            this.setAbsorptionHearts(this.getAbsorptionHearts() - (f1 - f));
+            absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION);
+            this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F));
             if (f != 0.0F) {
+                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.x, Math.round(f * 10.0F));
+                    }
+                }
+                // CraftBukkit end
                 float f2 = this.getHealth();
 
                 this.setHealth(f2 - f);
                 this.bs().a(damagesource, f2, f);
+                // CraftBukkit start
+                if (human) {
+                    return true;
+                }
+                // CraftBukkit end
                 this.setAbsorptionHearts(this.getAbsorptionHearts() - f);
             }
+            return true; // CraftBukkit
         }
+        return false; // CraftBukkit
     }
 
     public CombatTracker bs() {
@@ -1240,7 +1484,8 @@
         if (f > 0.0025000002F) {
             f3 = 1.0F;
             f2 = (float) Math.sqrt((double) f) * 3.0F;
-            f1 = (float) MathHelper.b(d1, d0) * 180.0F / 3.1415927F - 90.0F;
+            // CraftBukkit - Math -> TrigMath
+            f1 = (float) org.bukkit.craftbukkit.TrigMath.atan2(d1, d0) * 180.0F / 3.1415927F - 90.0F;
         }
 
         if (this.az > 0.0F) {
@@ -1413,6 +1658,13 @@
             for (int i = 0; i < list.size(); ++i) {
                 Entity entity = (Entity) list.get(i);
 
+                // TODO better check now?
+                // CraftBukkit start - Only handle mob (non-player) collisions every other tick
+                if (entity instanceof EntityLiving && !(this instanceof EntityPlayer) && this.ticksLived % 2 == 0) {
+                    continue;
+                }
+                // CraftBukkit end
+
                 this.s(entity);
             }
         }
@@ -1425,6 +1677,18 @@
 
     public void mount(Entity entity) {
         if (this.vehicle != null && entity == null) {
+            // CraftBukkit start
+            Entity originalVehicle = this.vehicle;
+            if ((this.bukkitEntity instanceof LivingEntity) && (this.vehicle.getBukkitEntity() instanceof Vehicle)) {
+                VehicleExitEvent event = new VehicleExitEvent((Vehicle) this.vehicle.getBukkitEntity(), (LivingEntity) this.bukkitEntity);
+                getBukkitEntity().getServer().getPluginManager().callEvent(event);
+
+                if (event.isCancelled() || vehicle != originalVehicle) {
+                    return;
+                }
+            }
+            // CraftBukkit end
+            
             if (!this.world.isClientSide) {
                 this.q(this.vehicle);
             }