From c3a22e7850f7b6c41163676a154b22c61952b2a9 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Tue, 28 Jul 2020 19:05:03 +1000
Subject: [PATCH] SPIGOT-6032: Add best effort target events for new entities

---
 nms-patches/BehaviorAttackTargetForget.patch | 32 +++++++++++++++++
 nms-patches/BehaviorAttackTargetSet.patch    | 38 ++++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 nms-patches/BehaviorAttackTargetForget.patch
 create mode 100644 nms-patches/BehaviorAttackTargetSet.patch

diff --git a/nms-patches/BehaviorAttackTargetForget.patch b/nms-patches/BehaviorAttackTargetForget.patch
new file mode 100644
index 0000000000..45eff8c895
--- /dev/null
+++ b/nms-patches/BehaviorAttackTargetForget.patch
@@ -0,0 +1,32 @@
+--- a/net/minecraft/server/BehaviorAttackTargetForget.java
++++ b/net/minecraft/server/BehaviorAttackTargetForget.java
+@@ -3,6 +3,11 @@
+ import com.google.common.collect.ImmutableMap;
+ import java.util.Optional;
+ import java.util.function.Predicate;
++// CraftBukkit start
++import org.bukkit.craftbukkit.entity.CraftLivingEntity;
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTargetEvent;
++// CraftBukkit end
+ 
+ public class BehaviorAttackTargetForget<E extends EntityInsentient> extends Behavior<E> {
+ 
+@@ -54,6 +59,17 @@
+     }
+ 
+     private void d(E e0) {
++        // CraftBukkit start
++        EntityLiving old = e0.getBehaviorController().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
++        EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(e0, old, (old != null && !old.isAlive()) ? EntityTargetEvent.TargetReason.TARGET_DIED : EntityTargetEvent.TargetReason.FORGOT_TARGET);
++        if (event.isCancelled()) {
++            return;
++        }
++        if (event.getTarget() != null) {
++            e0.getBehaviorController().setMemory(MemoryModuleType.ATTACK_TARGET, ((CraftLivingEntity) event.getTarget()).getHandle());
++            return;
++        }
++        // CraftBukkit end
+         e0.getBehaviorController().removeMemory(MemoryModuleType.ATTACK_TARGET);
+     }
+ }
diff --git a/nms-patches/BehaviorAttackTargetSet.patch b/nms-patches/BehaviorAttackTargetSet.patch
new file mode 100644
index 0000000000..c2476ef323
--- /dev/null
+++ b/nms-patches/BehaviorAttackTargetSet.patch
@@ -0,0 +1,38 @@
+--- a/net/minecraft/server/BehaviorAttackTargetSet.java
++++ b/net/minecraft/server/BehaviorAttackTargetSet.java
+@@ -4,6 +4,11 @@
+ import java.util.Optional;
+ import java.util.function.Function;
+ import java.util.function.Predicate;
++// CraftBukkit start
++import org.bukkit.craftbukkit.entity.CraftLivingEntity;
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTargetEvent;
++// CraftBukkit end
+ 
+ public class BehaviorAttackTargetSet<E extends EntityInsentient> extends Behavior<E> {
+ 
+@@ -33,13 +38,21 @@
+     }
+ 
+     protected void a(WorldServer worldserver, E e0, long i) {
+-        ((Optional) this.c.apply(e0)).ifPresent((entityliving) -> {
++        (this.c.apply(e0)).ifPresent((entityliving) -> { // CraftBukkit - decompile error
+             this.a(e0, entityliving);
+         });
+     }
+ 
+     private void a(E e0, EntityLiving entityliving) {
+-        e0.getBehaviorController().setMemory(MemoryModuleType.ATTACK_TARGET, (Object) entityliving);
++        // CraftBukkit start
++        EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(e0, entityliving, (entityliving instanceof EntityPlayer) ? EntityTargetEvent.TargetReason.CLOSEST_PLAYER : EntityTargetEvent.TargetReason.CLOSEST_ENTITY);
++        if (event.isCancelled()) {
++            return;
++        }
++        entityliving = (event.getTarget() != null) ? ((CraftLivingEntity) event.getTarget()).getHandle() : null;
++        // CraftBukkit end
++
++        e0.getBehaviorController().setMemory(MemoryModuleType.ATTACK_TARGET, entityliving); // CraftBukkit - decompile error
+         e0.getBehaviorController().removeMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
+     }
+ }