From daa0ce3d657b934385d4b4339d56c5b138605a5e Mon Sep 17 00:00:00 2001
From: Thinkofdeath <thinkofdeath@spigotmc.org>
Date: Thu, 12 Feb 2015 14:00:40 +0000
Subject: [PATCH] SPIGOT-540: Fix an issue when mob effects were added during
 an event caused by a mob effect

---
 nms-patches/EntityLiving.patch | 112 +++++++++++++++++++++++++--------
 1 file changed, 86 insertions(+), 26 deletions(-)

diff --git a/nms-patches/EntityLiving.patch b/nms-patches/EntityLiving.patch
index 882b5b3fe0..1a0a628799 100644
--- a/nms-patches/EntityLiving.patch
+++ b/nms-patches/EntityLiving.patch
@@ -1,12 +1,13 @@
---- ../work/decompile-8eb82bde/net/minecraft/server/EntityLiving.java	2015-01-16 14:48:08.266797758 +1100
-+++ src/main/java/net/minecraft/server/EntityLiving.java	2015-01-16 14:48:08.270797751 +1100
-@@ -8,6 +8,18 @@
+--- ../work/decompile-8eb82bde//net/minecraft/server/EntityLiving.java	2015-02-12 14:00:06.621316929 +0000
++++ src/main/java/net/minecraft/server/EntityLiving.java	2015-02-12 14:00:06.621316929 +0000
+@@ -8,6 +8,19 @@
  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;
@@ -19,7 +20,7 @@
  public abstract class EntityLiving extends Entity {
  
      private static final UUID a = UUID.fromString("662A6B8D-DA3E-4C1C-8813-96EA6097278D");
-@@ -67,6 +79,11 @@
+@@ -67,6 +80,11 @@
      private float bk;
      private int bl;
      private float bm;
@@ -31,7 +32,7 @@
  
      public void G() {
          this.damageEntity(DamageSource.OUT_OF_WORLD, Float.MAX_VALUE);
-@@ -75,7 +92,8 @@
+@@ -75,7 +93,8 @@
      public EntityLiving(World world) {
          super(world);
          this.aW();
@@ -41,7 +42,7 @@
          this.k = true;
          this.aF = (float) ((Math.random() + 1.0D) * 0.009999999776482582D);
          this.setPosition(this.locX, this.locY, this.locZ);
-@@ -116,8 +134,14 @@
+@@ -116,8 +135,14 @@
                  }
  
                  int i = (int) (150.0D * d1);
@@ -58,7 +59,7 @@
              }
          }
  
-@@ -174,7 +198,11 @@
+@@ -174,7 +199,11 @@
                  this.mount((Entity) null);
              }
          } else {
@@ -71,7 +72,7 @@
          }
  
          if (this.isAlive() && this.U()) {
-@@ -220,6 +248,18 @@
+@@ -220,6 +249,18 @@
          this.lastPitch = this.pitch;
          this.world.methodProfiler.b();
      }
@@ -90,7 +91,7 @@
  
      public boolean isBaby() {
          return false;
-@@ -227,19 +267,18 @@
+@@ -227,19 +268,18 @@
  
      protected void aY() {
          ++this.deathTicks;
@@ -119,7 +120,7 @@
  
              this.die();
  
-@@ -375,6 +414,17 @@
+@@ -375,6 +415,17 @@
                  }
              }
          }
@@ -137,7 +138,40 @@
  
          if (nbttagcompound.hasKeyOfType("HealF", 99)) {
              this.setHealth(nbttagcompound.getFloat("HealF"));
-@@ -486,7 +536,8 @@
+@@ -395,9 +446,14 @@
+         this.hurtTimestamp = nbttagcompound.getInt("HurtByTimestamp");
+     }
+ 
++    // CraftBukkit start
++    private boolean isTickingEffects = false;
++    private List<Object> effectsToProcess = Lists.newArrayList();
++    // CraftBukkit end
++
+     protected void bh() {
+         Iterator iterator = this.effects.keySet().iterator();
+-
++        isTickingEffects = true; // CraftBukkit
+         while (iterator.hasNext()) {
+             Integer integer = (Integer) iterator.next();
+             MobEffect mobeffect = (MobEffect) this.effects.get(integer);
+@@ -411,6 +467,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.isStatic) {
+@@ -486,7 +552,8 @@
      }
  
      public boolean hasEffect(int i) {
@@ -147,7 +181,33 @@
      }
  
      public boolean hasEffect(MobEffectList mobeffectlist) {
-@@ -560,20 +611,52 @@
+@@ -498,6 +565,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);
+@@ -527,6 +600,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) {
+@@ -560,20 +639,52 @@
  
      }
  
@@ -201,7 +261,7 @@
          this.datawatcher.watch(6, Float.valueOf(MathHelper.a(f, 0.0F, this.getMaxHealth())));
      }
  
-@@ -589,7 +672,8 @@
+@@ -589,7 +700,8 @@
              } else if (damagesource.o() && this.hasEffect(MobEffectList.FIRE_RESISTANCE)) {
                  return false;
              } else {
@@ -211,7 +271,7 @@
                      this.getEquipment(4).damage((int) (f * 4.0F + this.random.nextFloat() * f * 2.0F), this);
                      f *= 0.75F;
                  }
-@@ -602,16 +686,34 @@
+@@ -602,16 +714,34 @@
                          return false;
                      }
  
@@ -248,7 +308,7 @@
                  this.au = 0.0F;
                  Entity entity = damagesource.getEntity();
  
-@@ -717,11 +819,19 @@
+@@ -717,11 +847,19 @@
              }
  
              if (this.aZ() && this.world.getGameRules().getBoolean("doMobLoot")) {
@@ -269,7 +329,7 @@
              }
          }
  
-@@ -781,8 +891,13 @@
+@@ -781,8 +919,13 @@
          int i = MathHelper.f((f - 3.0F - f2) * f1);
  
          if (i > 0) {
@@ -284,7 +344,7 @@
              int j = MathHelper.floor(this.locX);
              int k = MathHelper.floor(this.locY - 0.20000000298023224D);
              int l = MathHelper.floor(this.locZ);
-@@ -826,7 +941,7 @@
+@@ -826,7 +969,7 @@
              int i = 25 - this.bq();
              float f1 = f * (float) i;
  
@@ -293,7 +353,7 @@
              f = f1 / 25.0F;
          }
  
-@@ -840,8 +955,9 @@
+@@ -840,8 +983,9 @@
              int i;
              int j;
              float f1;
@@ -305,7 +365,7 @@
                  i = (this.getEffect(MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
                  j = 25 - i;
                  f1 = f * (float) j;
-@@ -867,22 +983,117 @@
+@@ -867,22 +1011,117 @@
          }
      }
  
@@ -384,9 +444,7 @@
 +                }
 +            };
 +            float absorptionModifier = absorption.apply((double) f).floatValue();
- 
--            f = Math.max(f - this.getAbsorptionHearts(), 0.0F);
--            this.setAbsorptionHearts(this.getAbsorptionHearts() - (f1 - f));
++
 +            EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
 +            if (event.isCancelled()) {
 +                return false;
@@ -404,7 +462,9 @@
 +                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) {
@@ -429,7 +489,7 @@
      }
  
      public CombatTracker br() {
-@@ -1236,7 +1447,8 @@
+@@ -1236,7 +1475,8 @@
          if (f > 0.0025000002F) {
              f3 = 1.0F;
              f2 = (float) Math.sqrt((double) f) * 3.0F;
@@ -439,7 +499,7 @@
          }
  
          if (this.ax > 0.0F) {
-@@ -1400,6 +1612,13 @@
+@@ -1400,6 +1640,13 @@
          if (list != null && !list.isEmpty()) {
              for (int i = 0; i < list.size(); ++i) {
                  Entity entity = (Entity) list.get(i);
@@ -453,7 +513,7 @@
  
                  if (entity.ae()) {
                      this.s(entity);
-@@ -1415,6 +1634,18 @@
+@@ -1415,6 +1662,18 @@
  
      public void mount(Entity entity) {
          if (this.vehicle != null && entity == null) {