--- a/net/minecraft/world/entity/ai/behavior/BehaviorAttackTargetSet.java
+++ b/net/minecraft/world/entity/ai/behavior/BehaviorAttackTargetSet.java
@@ -10,6 +10,13 @@
 import net.minecraft.world.entity.ai.memory.MemoryModuleType;
 import net.minecraft.world.entity.ai.memory.MemoryStatus;
 
+// CraftBukkit start
+import net.minecraft.server.level.EntityPlayer;
+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> {
 
     private final Predicate<E> canAttackPredicate;
@@ -38,13 +45,21 @@
     }
 
     protected void a(WorldServer worldserver, E e0, long i) {
-        ((Optional) this.targetFinderFunction.apply(e0)).ifPresent((entityliving) -> {
+        (this.targetFinderFunction.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);
     }
 }
