--- a/net/minecraft/world/entity/ai/behavior/PrepareRamNearestTarget.java
+++ b/net/minecraft/world/entity/ai/behavior/PrepareRamNearestTarget.java
@@ -3,17 +3,23 @@
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import java.util.Comparator;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Function;
 import java.util.function.ToIntFunction;
+import java.util.stream.Stream;
 import net.minecraft.core.BlockPos;
 import net.minecraft.core.Direction;
 import net.minecraft.server.level.ServerLevel;
+import net.minecraft.server.level.ServerPlayer;
 import net.minecraft.sounds.SoundEvent;
 import net.minecraft.sounds.SoundSource;
 import net.minecraft.util.Mth;
+import net.minecraft.world.entity.Entity;
 import net.minecraft.world.entity.LivingEntity;
+import net.minecraft.world.entity.Mob;
 import net.minecraft.world.entity.PathfinderMob;
 import net.minecraft.world.entity.ai.Brain;
 import net.minecraft.world.entity.ai.memory.MemoryModuleType;
@@ -21,11 +27,17 @@
 import net.minecraft.world.entity.ai.memory.WalkTarget;
 import net.minecraft.world.entity.ai.navigation.PathNavigation;
 import net.minecraft.world.entity.ai.targeting.TargetingConditions;
+import net.minecraft.world.entity.player.Player;
 import net.minecraft.world.level.pathfinder.Path;
 import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
 import net.minecraft.world.phys.Vec3;
+import org.bukkit.craftbukkit.entity.CraftLivingEntity;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityTargetEvent;
+// CraftBukkit end
 
 public class PrepareRamNearestTarget<E extends PathfinderMob> extends Behavior<E> {
+
     public static final int TIME_OUT_DURATION = 160;
     private final ToIntFunction<E> getCooldownOnFail;
     private final int minRamDistance;
@@ -37,28 +49,8 @@
     private Optional<Long> reachedRamPositionTimestamp = Optional.empty();
     private Optional<PrepareRamNearestTarget.RamCandidate> ramCandidate = Optional.empty();
 
-    public PrepareRamNearestTarget(
-        ToIntFunction<E> cooldownFactory,
-        int minDistance,
-        int maxDistance,
-        float speed,
-        TargetingConditions targetPredicate,
-        int prepareTime,
-        Function<E, SoundEvent> soundFactory
-    ) {
-        super(
-            ImmutableMap.of(
-                MemoryModuleType.LOOK_TARGET,
-                MemoryStatus.REGISTERED,
-                MemoryModuleType.RAM_COOLDOWN_TICKS,
-                MemoryStatus.VALUE_ABSENT,
-                MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
-                MemoryStatus.VALUE_PRESENT,
-                MemoryModuleType.RAM_TARGET,
-                MemoryStatus.VALUE_ABSENT
-            ),
-            160
-        );
+    public PrepareRamNearestTarget(ToIntFunction<E> cooldownFactory, int minDistance, int maxDistance, float speed, TargetingConditions targetPredicate, int prepareTime, Function<E, SoundEvent> soundFactory) {
+        super(ImmutableMap.of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, MemoryModuleType.RAM_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT, MemoryModuleType.RAM_TARGET, MemoryStatus.VALUE_ABSENT), 160);
         this.getCooldownOnFail = cooldownFactory;
         this.minRamDistance = minDistance;
         this.maxRamDistance = maxDistance;
@@ -68,108 +60,136 @@
         this.getPrepareRamSound = soundFactory;
     }
 
-    @Override
-    protected void start(ServerLevel serverLevel, PathfinderMob pathfinderMob, long l) {
-        Brain<?> brain = pathfinderMob.getBrain();
-        brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
-            .flatMap(mob -> mob.findClosest(mobx -> this.ramTargeting.test(pathfinderMob, mobx)))
-            .ifPresent(mob -> this.chooseRamPosition(pathfinderMob, mob));
+    protected void start(ServerLevel worldserver, PathfinderMob entitycreature, long i) {
+        Brain<?> behaviorcontroller = entitycreature.getBrain();
+
+        behaviorcontroller.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES).flatMap((nearestvisiblelivingentities) -> {
+            return nearestvisiblelivingentities.findClosest((entityliving) -> {
+                return this.ramTargeting.test(entitycreature, entityliving);
+            });
+        }).ifPresent((entityliving) -> {
+            // CraftBukkit start
+            EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(entitycreature, entityliving, (entityliving instanceof ServerPlayer) ? EntityTargetEvent.TargetReason.CLOSEST_PLAYER : EntityTargetEvent.TargetReason.CLOSEST_ENTITY);
+            if (event.isCancelled() || event.getTarget() == null) {
+                return;
+            }
+            entityliving = ((CraftLivingEntity) event.getTarget()).getHandle();
+            // CraftBukkit end
+            this.chooseRamPosition(entitycreature, entityliving);
+        });
     }
 
-    @Override
     protected void stop(ServerLevel world, E entity, long time) {
-        Brain<?> brain = entity.getBrain();
-        if (!brain.hasMemoryValue(MemoryModuleType.RAM_TARGET)) {
-            world.broadcastEntityEvent(entity, (byte)59);
-            brain.setMemory(MemoryModuleType.RAM_COOLDOWN_TICKS, this.getCooldownOnFail.applyAsInt(entity));
+        Brain<?> behaviorcontroller = entity.getBrain();
+
+        if (!behaviorcontroller.hasMemoryValue(MemoryModuleType.RAM_TARGET)) {
+            world.broadcastEntityEvent(entity, (byte) 59);
+            behaviorcontroller.setMemory(MemoryModuleType.RAM_COOLDOWN_TICKS, this.getCooldownOnFail.applyAsInt(entity)); // CraftBukkit - decompile error
         }
+
     }
 
-    @Override
-    protected boolean canStillUse(ServerLevel serverLevel, PathfinderMob pathfinderMob, long l) {
-        return this.ramCandidate.isPresent() && this.ramCandidate.get().getTarget().isAlive();
+    protected boolean canStillUse(ServerLevel worldserver, PathfinderMob entitycreature, long i) {
+        return this.ramCandidate.isPresent() && ((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getTarget().isAlive();
     }
 
-    @Override
-    protected void tick(ServerLevel serverLevel, E pathfinderMob, long l) {
+    protected void tick(ServerLevel worldserver, E e0, long i) {
         if (!this.ramCandidate.isEmpty()) {
-            pathfinderMob.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(this.ramCandidate.get().getStartPosition(), this.walkSpeed, 0));
-            pathfinderMob.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(this.ramCandidate.get().getTarget(), true));
-            boolean bl = !this.ramCandidate.get().getTarget().blockPosition().equals(this.ramCandidate.get().getTargetPosition());
-            if (bl) {
-                serverLevel.broadcastEntityEvent(pathfinderMob, (byte)59);
-                pathfinderMob.getNavigation().stop();
-                this.chooseRamPosition(pathfinderMob, this.ramCandidate.get().target);
+            e0.getBrain().setMemory(MemoryModuleType.WALK_TARGET, (new WalkTarget(((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getStartPosition(), this.walkSpeed, 0))); // CraftBukkit - decompile error
+            e0.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, (new EntityTracker(((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getTarget(), true))); // CraftBukkit - decompile error
+            boolean flag = !((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getTarget().blockPosition().equals(((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getTargetPosition());
+
+            if (flag) {
+                worldserver.broadcastEntityEvent(e0, (byte) 59);
+                e0.getNavigation().stop();
+                this.chooseRamPosition(e0, ((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).target);
             } else {
-                BlockPos blockPos = pathfinderMob.blockPosition();
-                if (blockPos.equals(this.ramCandidate.get().getStartPosition())) {
-                    serverLevel.broadcastEntityEvent(pathfinderMob, (byte)58);
+                BlockPos blockposition = e0.blockPosition();
+
+                if (blockposition.equals(((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getStartPosition())) {
+                    worldserver.broadcastEntityEvent(e0, (byte) 58);
                     if (this.reachedRamPositionTimestamp.isEmpty()) {
-                        this.reachedRamPositionTimestamp = Optional.of(l);
+                        this.reachedRamPositionTimestamp = Optional.of(i);
                     }
 
-                    if (l - this.reachedRamPositionTimestamp.get() >= (long)this.ramPrepareTime) {
-                        pathfinderMob.getBrain()
-                            .setMemory(MemoryModuleType.RAM_TARGET, this.getEdgeOfBlock(blockPos, this.ramCandidate.get().getTargetPosition()));
-                        serverLevel.playSound(
-                            null, pathfinderMob, this.getPrepareRamSound.apply(pathfinderMob), SoundSource.NEUTRAL, 1.0F, pathfinderMob.getVoicePitch()
-                        );
+                    if (i - (Long) this.reachedRamPositionTimestamp.get() >= (long) this.ramPrepareTime) {
+                        e0.getBrain().setMemory(MemoryModuleType.RAM_TARGET, this.getEdgeOfBlock(blockposition, ((PrepareRamNearestTarget.RamCandidate) this.ramCandidate.get()).getTargetPosition())); // CraftBukkit - decompile error
+                        worldserver.playSound((Player) null, (Entity) e0, (SoundEvent) this.getPrepareRamSound.apply(e0), SoundSource.NEUTRAL, 1.0F, e0.getVoicePitch());
                         this.ramCandidate = Optional.empty();
                     }
                 }
             }
+
         }
     }
 
     private Vec3 getEdgeOfBlock(BlockPos start, BlockPos end) {
-        double d = 0.5;
-        double e = 0.5 * (double)Mth.sign((double)(end.getX() - start.getX()));
-        double f = 0.5 * (double)Mth.sign((double)(end.getZ() - start.getZ()));
-        return Vec3.atBottomCenterOf(end).add(e, 0.0, f);
+        double d0 = 0.5D;
+        double d1 = 0.5D * (double) Mth.sign((double) (end.getX() - start.getX()));
+        double d2 = 0.5D * (double) Mth.sign((double) (end.getZ() - start.getZ()));
+
+        return Vec3.atBottomCenterOf(end).add(d1, 0.0D, d2);
     }
 
     private Optional<BlockPos> calculateRammingStartPosition(PathfinderMob entity, LivingEntity target) {
-        BlockPos blockPos = target.blockPosition();
-        if (!this.isWalkableBlock(entity, blockPos)) {
+        BlockPos blockposition = target.blockPosition();
+
+        if (!this.isWalkableBlock(entity, blockposition)) {
             return Optional.empty();
         } else {
             List<BlockPos> list = Lists.newArrayList();
-            BlockPos.MutableBlockPos mutableBlockPos = blockPos.mutable();
+            BlockPos.MutableBlockPos blockposition_mutableblockposition = blockposition.mutable();
+            Iterator iterator = Direction.Plane.HORIZONTAL.iterator();
 
-            for (Direction direction : Direction.Plane.HORIZONTAL) {
-                mutableBlockPos.set(blockPos);
+            while (iterator.hasNext()) {
+                Direction enumdirection = (Direction) iterator.next();
 
-                for (int i = 0; i < this.maxRamDistance; i++) {
-                    if (!this.isWalkableBlock(entity, mutableBlockPos.move(direction))) {
-                        mutableBlockPos.move(direction.getOpposite());
-                        break;
+                blockposition_mutableblockposition.set(blockposition);
+                int i = 0;
+
+                while (true) {
+                    if (i < this.maxRamDistance) {
+                        if (this.isWalkableBlock(entity, blockposition_mutableblockposition.move(enumdirection))) {
+                            ++i;
+                            continue;
+                        }
+
+                        blockposition_mutableblockposition.move(enumdirection.getOpposite());
                     }
-                }
 
-                if (mutableBlockPos.distManhattan(blockPos) >= this.minRamDistance) {
-                    list.add(mutableBlockPos.immutable());
+                    if (blockposition_mutableblockposition.distManhattan(blockposition) >= this.minRamDistance) {
+                        list.add(blockposition_mutableblockposition.immutable());
+                    }
+                    break;
                 }
             }
 
-            PathNavigation pathNavigation = entity.getNavigation();
-            return list.stream().sorted(Comparator.comparingDouble(entity.blockPosition()::distSqr)).filter(start -> {
-                Path path = pathNavigation.createPath(start, 0);
-                return path != null && path.canReach();
+            PathNavigation navigationabstract = entity.getNavigation();
+            Stream<BlockPos> stream = list.stream(); // CraftBukkit - decompile error
+            BlockPos blockposition1 = entity.blockPosition();
+
+            Objects.requireNonNull(blockposition1);
+            return stream.sorted(Comparator.comparingDouble(blockposition1::distSqr)).filter((blockposition2) -> {
+                Path pathentity = navigationabstract.createPath(blockposition2, 0);
+
+                return pathentity != null && pathentity.canReach();
             }).findFirst();
         }
     }
 
     private boolean isWalkableBlock(PathfinderMob entity, BlockPos target) {
-        return entity.getNavigation().isStableDestination(target) && entity.getPathfindingMalus(WalkNodeEvaluator.getPathTypeStatic(entity, target)) == 0.0F;
+        return entity.getNavigation().isStableDestination(target) && entity.getPathfindingMalus(WalkNodeEvaluator.getPathTypeStatic((Mob) entity, target)) == 0.0F;
     }
 
     private void chooseRamPosition(PathfinderMob entity, LivingEntity target) {
         this.reachedRamPositionTimestamp = Optional.empty();
-        this.ramCandidate = this.calculateRammingStartPosition(entity, target)
-            .map(start -> new PrepareRamNearestTarget.RamCandidate(start, target.blockPosition(), target));
+        this.ramCandidate = this.calculateRammingStartPosition(entity, target).map((blockposition) -> {
+            return new PrepareRamNearestTarget.RamCandidate(blockposition, target.blockPosition(), target);
+        });
     }
 
     public static class RamCandidate {
+
         private final BlockPos startPosition;
         private final BlockPos targetPosition;
         final LivingEntity target;
