From f4f21a2bf2efa168bcb7a92cd3f6051cc031af40 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 8 Nov 2018 22:43:38 -0500
Subject: [PATCH] Improve Activation Range by running target rules

This restores monster aggression range back to normal when
Activation Range is less than the monsters aggression range.

This allows an entity to try to acquire targets still during
inactive ticks, which will also then let it go into immunity stage.
---
 .../Activation-Range-Improvements.patch       | 90 +++++++++++++++++--
 .../Add-sun-related-API.patch                 |  2 +-
 ...unk-Unloads-based-on-Player-Movement.patch |  2 +-
 ...spawn-if-entity-is-in-a-chunk-schedu.patch |  2 +-
 .../Item-canEntityPickup.patch                |  4 +-
 5 files changed, 86 insertions(+), 14 deletions(-)

diff --git a/Spigot-Server-Patches/Activation-Range-Improvements.patch b/Spigot-Server-Patches/Activation-Range-Improvements.patch
index 342524ef35..d5f549f844 100644
--- a/Spigot-Server-Patches/Activation-Range-Improvements.patch
+++ b/Spigot-Server-Patches/Activation-Range-Improvements.patch
@@ -10,7 +10,7 @@ Fixes and adds new Immunities to improve gameplay behavior
 Adds water Mobs to activation range config and nerfs fish
 
 diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index d81e2dc1c5..7bd51b3578 100644
 --- a/src/main/java/net/minecraft/server/BlockPosition.java
 +++ b/src/main/java/net/minecraft/server/BlockPosition.java
 @@ -0,0 +0,0 @@ public class BlockPosition extends BaseBlockPosition {
@@ -22,7 +22,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
              return new BlockPosition(this);
          }
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index e7f9e490bf..68ceee5f08 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
@@ -34,7 +34,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
  
                  if (i != this.aM) {
 diff --git a/src/main/java/net/minecraft/server/EntityCreature.java b/src/main/java/net/minecraft/server/EntityCreature.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index 20b7c2c6dc..d659c57dbe 100644
 --- a/src/main/java/net/minecraft/server/EntityCreature.java
 +++ b/src/main/java/net/minecraft/server/EntityCreature.java
 @@ -0,0 +0,0 @@ import org.bukkit.event.entity.EntityUnleashEvent;
@@ -45,8 +45,30 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
      private BlockPosition a;
      private float b;
  
+diff --git a/src/main/java/net/minecraft/server/EntityInsentient.java b/src/main/java/net/minecraft/server/EntityInsentient.java
+index c87304d477..41a4962e94 100644
+--- a/src/main/java/net/minecraft/server/EntityInsentient.java
++++ b/src/main/java/net/minecraft/server/EntityInsentient.java
+@@ -0,0 +0,0 @@ public abstract class EntityInsentient extends EntityLiving {
+         return this.lookController;
+     }
+ 
++    // Paper start
++    @Override
++    public void inactiveTick() {
++        super.inactiveTick();
++        this.goalSelector.inactiveTick();
++        if (this.targetSelector.inactiveTick()) {
++            this.targetSelector.doTick();
++        }
++    }
++    // Paper end
++
+     public ControllerMove getControllerMove() {
+         return this.moveController;
+     }
 diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index 334580a8b7..b2b575d0e8 100644
 --- a/src/main/java/net/minecraft/server/EntityLiving.java
 +++ b/src/main/java/net/minecraft/server/EntityLiving.java
 @@ -0,0 +0,0 @@ public abstract class EntityLiving extends Entity {
@@ -59,7 +81,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
      protected int ticksFarFromPlayer;
      protected float aZ;
 diff --git a/src/main/java/net/minecraft/server/EntityLlama.java b/src/main/java/net/minecraft/server/EntityLlama.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index c66d8b68f0..774e4d6414 100644
 --- a/src/main/java/net/minecraft/server/EntityLlama.java
 +++ b/src/main/java/net/minecraft/server/EntityLlama.java
 @@ -0,0 +0,0 @@ public class EntityLlama extends EntityHorseChestedAbstract implements IRangedEn
@@ -71,7 +93,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
          return this.bQ != null;
      }
 diff --git a/src/main/java/net/minecraft/server/PathfinderGoal.java b/src/main/java/net/minecraft/server/PathfinderGoal.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index acc099e955..339c78eec9 100644
 --- a/src/main/java/net/minecraft/server/PathfinderGoal.java
 +++ b/src/main/java/net/minecraft/server/PathfinderGoal.java
 @@ -0,0 +0,0 @@ public abstract class PathfinderGoal {
@@ -85,7 +107,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
      public void e() {
      }
 diff --git a/src/main/java/net/minecraft/server/PathfinderGoalGotoTarget.java b/src/main/java/net/minecraft/server/PathfinderGoalGotoTarget.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index 9a75cb63ba..cf10605aaa 100644
 --- a/src/main/java/net/minecraft/server/PathfinderGoalGotoTarget.java
 +++ b/src/main/java/net/minecraft/server/PathfinderGoalGotoTarget.java
 @@ -0,0 +0,0 @@
@@ -125,8 +147,47 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
                              return true;
                          }
                      }
+diff --git a/src/main/java/net/minecraft/server/PathfinderGoalSelector.java b/src/main/java/net/minecraft/server/PathfinderGoalSelector.java
+index be3a3c82ee..1404b629d5 100644
+--- a/src/main/java/net/minecraft/server/PathfinderGoalSelector.java
++++ b/src/main/java/net/minecraft/server/PathfinderGoalSelector.java
+@@ -0,0 +0,0 @@ import org.apache.logging.log4j.Logger;
+ public class PathfinderGoalSelector {
+     private static final Logger a = LogManager.getLogger();
+     private final Set<PathfinderGoalSelector.PathfinderGoalSelectorItem> b = Sets.newLinkedHashSet();
+-    private final Set<PathfinderGoalSelector.PathfinderGoalSelectorItem> c = Sets.newLinkedHashSet();
++    private final Set<PathfinderGoalSelector.PathfinderGoalSelectorItem> c = Sets.newLinkedHashSet();private Set<PathfinderGoalSelector.PathfinderGoalSelectorItem> getExecutingTasks() { return c; }// Paper - OBFHELPER
+     private final MethodProfiler d;
+-    private int e;
+-    private int f = 3;
++    private int e;private int getCurRate() { return e; } private void incRate() { this.e++; }// Paper - OBFHELPER
++    private int f = 3;private int getTickRate() { return f; } // Paper - OBFHELPER
+     private int g;
+ 
+     public PathfinderGoalSelector(MethodProfiler methodprofiler) {
+@@ -0,0 +0,0 @@ public class PathfinderGoalSelector {
+         this.b.add(new PathfinderGoalSelector.PathfinderGoalSelectorItem(i, pathfindergoal));
+     }
+ 
++    // Paper start
++    public boolean inactiveTick() {
++        if (getCurRate() % getTickRate() != 0) {
++            incRate();
++            return false;
++        } else {
++            return true;
++        }
++    }
++    public boolean hasTasks() {
++        return !getExecutingTasks().isEmpty();
++    }
++    // Paper end
++
+     public void a(PathfinderGoal pathfindergoal) {
+         Iterator iterator = this.b.iterator();
+ 
 diff --git a/src/main/java/org/spigotmc/ActivationRange.java b/src/main/java/org/spigotmc/ActivationRange.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index 09df00e94b..d08ef3fe10 100644
 --- a/src/main/java/org/spigotmc/ActivationRange.java
 +++ b/src/main/java/org/spigotmc/ActivationRange.java
 @@ -0,0 +0,0 @@ package org.spigotmc;
@@ -299,6 +360,17 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
              {
                  EntityAnimal animal = (EntityAnimal) entity;
 @@ -0,0 +0,0 @@ public class ActivationRange
+                     entity.activatedTick = MinecraftServer.currentTick + 20;
+                 }
+                 isActive = true;
++                // Paper start
++            } else if (entity instanceof EntityInsentient && ((EntityInsentient) entity).targetSelector.hasTasks()) {
++                isActive = true;
+             }
++            // Paper end
+             // Add a little performance juice to active entities. Skip 1/4 if not immune.
+-        } else if ( !entity.defaultActivationState && entity.ticksLived % 4 == 0 && !checkEntityImmunities( entity ) )
++        } else if ( !entity.defaultActivationState && entity.ticksLived % 4 == 0 && !(entity instanceof EntityInsentient && ((EntityInsentient) entity).targetSelector.hasTasks()) && !checkEntityImmunities( entity ) ) // Paper - add targetSelector.hasTasks
          {
              isActive = false;
          }
@@ -313,7 +385,7 @@ index 7ac07ac07ac0..7ac07ac07ac0 100644
          {
              isActive = false;
 diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java
-index 7ac07ac07ac0..7ac07ac07ac0 100644
+index 87bc8e2d9c..06d84a0b66 100644
 --- a/src/main/java/org/spigotmc/SpigotWorldConfig.java
 +++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java
 @@ -0,0 +0,0 @@ public class SpigotWorldConfig
diff --git a/Spigot-Server-Patches/Add-sun-related-API.patch b/Spigot-Server-Patches/Add-sun-related-API.patch
index cc8336e7cc..56087dc7f6 100644
--- a/Spigot-Server-Patches/Add-sun-related-API.patch
+++ b/Spigot-Server-Patches/Add-sun-related-API.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Add sun related API
 
 
 diff --git a/src/main/java/net/minecraft/server/EntityInsentient.java b/src/main/java/net/minecraft/server/EntityInsentient.java
-index 5aea6629f2..fd4a77c1a9 100644
+index db9201d292..6c9776f57e 100644
 --- a/src/main/java/net/minecraft/server/EntityInsentient.java
 +++ b/src/main/java/net/minecraft/server/EntityInsentient.java
 @@ -0,0 +0,0 @@ public abstract class EntityInsentient extends EntityLiving {
diff --git a/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch b/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
index eed8483213..cecb387175 100644
--- a/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
+++ b/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
@@ -211,7 +211,7 @@ index 24e504c279..f0e974dffc 100644
              }
  
 diff --git a/src/main/java/org/spigotmc/ActivationRange.java b/src/main/java/org/spigotmc/ActivationRange.java
-index dfa59bbddc..8f0a999b0c 100644
+index d08ef3fe10..081789a8fe 100644
 --- a/src/main/java/org/spigotmc/ActivationRange.java
 +++ b/src/main/java/org/spigotmc/ActivationRange.java
 @@ -0,0 +0,0 @@ public class ActivationRange
diff --git a/Spigot-Server-Patches/Don-t-process-despawn-if-entity-is-in-a-chunk-schedu.patch b/Spigot-Server-Patches/Don-t-process-despawn-if-entity-is-in-a-chunk-schedu.patch
index 998be4d7d2..89424c1535 100644
--- a/Spigot-Server-Patches/Don-t-process-despawn-if-entity-is-in-a-chunk-schedu.patch
+++ b/Spigot-Server-Patches/Don-t-process-despawn-if-entity-is-in-a-chunk-schedu.patch
@@ -12,7 +12,7 @@ keep it vanilla in behavior
 a player may teleport away, and trigger instant despawn
 
 diff --git a/src/main/java/net/minecraft/server/EntityInsentient.java b/src/main/java/net/minecraft/server/EntityInsentient.java
-index 04a1218ed9..141db48f16 100644
+index 200d46a8a0..db9201d292 100644
 --- a/src/main/java/net/minecraft/server/EntityInsentient.java
 +++ b/src/main/java/net/minecraft/server/EntityInsentient.java
 @@ -0,0 +0,0 @@ public abstract class EntityInsentient extends EntityLiving {
diff --git a/Spigot-Server-Patches/Item-canEntityPickup.patch b/Spigot-Server-Patches/Item-canEntityPickup.patch
index 006c7596ce..71baa48bcf 100644
--- a/Spigot-Server-Patches/Item-canEntityPickup.patch
+++ b/Spigot-Server-Patches/Item-canEntityPickup.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Item#canEntityPickup
 
 
 diff --git a/src/main/java/net/minecraft/server/EntityInsentient.java b/src/main/java/net/minecraft/server/EntityInsentient.java
-index 6f5f3f8cd0..04a1218ed9 100644
+index 41a4962e94..200d46a8a0 100644
 --- a/src/main/java/net/minecraft/server/EntityInsentient.java
 +++ b/src/main/java/net/minecraft/server/EntityInsentient.java
 @@ -0,0 +0,0 @@ public abstract class EntityInsentient extends EntityLiving {
@@ -21,7 +21,7 @@ index 6f5f3f8cd0..04a1218ed9 100644
                  }
              }
 diff --git a/src/main/java/net/minecraft/server/EntityItem.java b/src/main/java/net/minecraft/server/EntityItem.java
-index eecebafdad..3e70a06397 100644
+index 5be2d93146..5446ff3e82 100644
 --- a/src/main/java/net/minecraft/server/EntityItem.java
 +++ b/src/main/java/net/minecraft/server/EntityItem.java
 @@ -0,0 +0,0 @@ public class EntityItem extends Entity {