--- a/net/minecraft/world/entity/ai/behavior/StopAttackingIfTargetInvalid.java
+++ b/net/minecraft/world/entity/ai/behavior/StopAttackingIfTargetInvalid.java
@@ -10,7 +10,14 @@
 import net.minecraft.world.entity.ai.memory.MemoryModuleType;
 import net.minecraft.world.entity.ai.memory.MemoryStatus;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.v1_19_R1.entity.CraftLivingEntity;
+import org.bukkit.craftbukkit.v1_19_R1.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityTargetEvent;
+// CraftBukkit end
+
 public class StopAttackingIfTargetInvalid<E extends Mob> extends Behavior<E> {
+
     private static final int TIMEOUT_TO_GET_WITHIN_ATTACK_RANGE = 200;
     private final Predicate<LivingEntity> stopAttackingWhen;
     private final BiConsumer<E, LivingEntity> onTargetErased;
@@ -28,36 +35,36 @@
     }
 
     public StopAttackingIfTargetInvalid(Predicate<LivingEntity> alternativePredicate) {
-        this(alternativePredicate, (mob, livingEntity) -> {
+        this(alternativePredicate, (entityinsentient, entityliving) -> {
         });
     }
 
     public StopAttackingIfTargetInvalid(BiConsumer<E, LivingEntity> forgetCallback) {
-        this((target) -> {
+        this((entityliving) -> {
             return false;
         }, forgetCallback);
     }
 
     public StopAttackingIfTargetInvalid() {
-        this((target) -> {
+        this((entityliving) -> {
             return false;
-        }, (mob, livingEntity) -> {
+        }, (entityinsentient, entityliving) -> {
         });
     }
 
-    @Override
     protected void start(ServerLevel world, E entity, long time) {
-        LivingEntity livingEntity = this.getAttackTarget(entity);
-        if (!entity.canAttack(livingEntity)) {
-            this.clearAttackTarget(entity);
-        } else if (this.canGrowTiredOfTryingToReachTarget && isTiredOfTryingToReachTarget(entity)) {
-            this.clearAttackTarget(entity);
+        LivingEntity entityliving = this.getAttackTarget(entity);
+
+        if (!entity.canAttack(entityliving)) {
+            this.clearAttackTarget(entity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_INVALID); // Paper
+        } else if (this.canGrowTiredOfTryingToReachTarget && StopAttackingIfTargetInvalid.isTiredOfTryingToReachTarget(entity)) {
+            this.clearAttackTarget(entity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.FORGOT_TARGET); // Paper
         } else if (this.isCurrentTargetDeadOrRemoved(entity)) {
-            this.clearAttackTarget(entity);
+            this.clearAttackTarget(entity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_DIED); // Paper
         } else if (this.isCurrentTargetInDifferentLevel(entity)) {
-            this.clearAttackTarget(entity);
+            this.clearAttackTarget(entity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_OTHER_LEVEL); // Paper
         } else if (this.stopAttackingWhen.test(this.getAttackTarget(entity))) {
-            this.clearAttackTarget(entity);
+            this.clearAttackTarget(entity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_INVALID); // Paper
         }
     }
 
@@ -66,20 +73,36 @@
     }
 
     private LivingEntity getAttackTarget(E entity) {
-        return entity.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
+        return (LivingEntity) entity.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
     }
 
     private static <E extends LivingEntity> boolean isTiredOfTryingToReachTarget(E entity) {
         Optional<Long> optional = entity.getBrain().getMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
-        return optional.isPresent() && entity.level.getGameTime() - optional.get() > 200L;
+
+        return optional.isPresent() && entity.level.getGameTime() - (Long) optional.get() > 200L;
     }
 
     private boolean isCurrentTargetDeadOrRemoved(E entity) {
         Optional<LivingEntity> optional = entity.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET);
-        return optional.isPresent() && !optional.get().isAlive();
+
+        return optional.isPresent() && !((LivingEntity) optional.get()).isAlive();
     }
 
-    protected void clearAttackTarget(E entity) {
+    protected void clearAttackTarget(E entity, EntityTargetEvent.TargetReason reason) {
+        // CraftBukkit start
+        // Paper start - fix this event
+        // LivingEntity old = entity.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
+        EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(entity, null, reason);
+        if (event.isCancelled()) {
+            return;
+        }
+        // comment out, bad logic - bad
+        /*if (event.getTarget() != null) {
+            entity.getBrain().setMemory(MemoryModuleType.ATTACK_TARGET, ((CraftLivingEntity) event.getTarget()).getHandle());
+            return;
+        }*/
+        // Paper end
+        // CraftBukkit end
         this.onTargetErased.accept(entity, this.getAttackTarget(entity));
         entity.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
     }
