From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Paul Sauve <paul@technove.co>
Date: Sat, 13 Mar 2021 15:05:28 -0600
Subject: [PATCH] Early return optimization for target finding


diff --git a/src/main/java/net/minecraft/world/entity/ai/targeting/PathfinderTargetCondition.java b/src/main/java/net/minecraft/world/entity/ai/targeting/PathfinderTargetCondition.java
index 88972dd8252bd2d2d8e384d616484ff682949fa8..5e8d3b84cbad299e09e8f25bfc712d11e1d1068e 100644
--- a/src/main/java/net/minecraft/world/entity/ai/targeting/PathfinderTargetCondition.java
+++ b/src/main/java/net/minecraft/world/entity/ai/targeting/PathfinderTargetCondition.java
@@ -84,9 +84,17 @@ public class PathfinderTargetCondition {
                 }
 
                 if (this.b > 0.0D) {
+                    // Airplane start - try to return early
+                    double range = (useFollowRange ? getFollowRange(entityliving) : this.b);
+                    double d2 = entityliving.h(entityliving1.locX(), entityliving1.locY(), entityliving1.locZ()); // move up here to use early
+
+                    if (d2 > range * range) { // if they're further than the absolute furthest allowed, they don't match
+                        return false;
+                    }
+
                     double d0 = this.g ? entityliving1.A(entityliving) : 1.0D;
-                    double d1 = Math.max((useFollowRange ? getFollowRange(entityliving) : this.b) * d0, 2.0D); // Paper
-                    double d2 = entityliving.h(entityliving1.locX(), entityliving1.locY(), entityliving1.locZ());
+                    double d1 = Math.max(range * d0, 2.0D); // Paper // Airplane - reuse range calculated above
+                    // Airplane end
 
                     if (d2 > d1 * d1) {
                         return false;
