From 72f5e76f9bd66bdabf49f45def187f29ec096af0 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 18 Nov 2023 15:59:12 -0800
Subject: [PATCH] Fix spawners checking max nearby entities with correct type
 (#8945)

Co-authored-by: Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
---
 .../server/Fix-a-bunch-of-vanilla-bugs.patch  | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/patches/server/Fix-a-bunch-of-vanilla-bugs.patch b/patches/server/Fix-a-bunch-of-vanilla-bugs.patch
index 8a64648985..84736c4f22 100644
--- a/patches/server/Fix-a-bunch-of-vanilla-bugs.patch
+++ b/patches/server/Fix-a-bunch-of-vanilla-bugs.patch
@@ -395,6 +395,42 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
                      entity.level().gameEvent(entity, GameEvent.EQUIP, entity.position());
                      stack.shrink(1);
                  }
+diff --git a/src/main/java/net/minecraft/world/level/BaseSpawner.java b/src/main/java/net/minecraft/world/level/BaseSpawner.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/world/level/BaseSpawner.java
++++ b/src/main/java/net/minecraft/world/level/BaseSpawner.java
+@@ -0,0 +0,0 @@ public abstract class BaseSpawner {
+     public int requiredPlayerRange = 16;
+     public int spawnRange = 4;
+     private int tickDelay = 0; // Paper
++    // Paper start - ported from 1.20.3 Fix MC-259321
++    static <B, T extends B> net.minecraft.world.level.entity.EntityTypeTest<B, T> forExactClass(Class<T> clazz) {
++        return new net.minecraft.world.level.entity.EntityTypeTest<>() {
++            @Nullable
++            @Override
++            public T tryCast(B clazz) {
++                return (T)(clazz.equals(clazz.getClass()) ? clazz : null);
++            }
++
++            @Override
++            public Class<? extends B> getBaseClass() {
++                return clazz;
++            }
++        };
++    }
++    // Paper end
+ 
+     public BaseSpawner() {}
+ 
+@@ -0,0 +0,0 @@ public abstract class BaseSpawner {
+                             return;
+                         }
+ 
+-                        int k = world.getEntitiesOfClass(entity.getClass(), (new AABB((double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), (double) (pos.getX() + 1), (double) (pos.getY() + 1), (double) (pos.getZ() + 1))).inflate((double) this.spawnRange)).size();
++                        int k = world.getEntities(forExactClass(entity.getClass()), (new AABB((double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), (double) (pos.getX() + 1), (double) (pos.getY() + 1), (double) (pos.getZ() + 1))).inflate((double) this.spawnRange), net.minecraft.world.entity.EntitySelector.NO_SPECTATORS).size(); // Paper - Fix MC-259321 (only count exact entity types for nearby checks)
+ 
+                         if (k >= this.maxNearbyEntities) {
+                             this.delay(world, pos);
 diff --git a/src/main/java/net/minecraft/world/level/block/AzaleaBlock.java b/src/main/java/net/minecraft/world/level/block/AzaleaBlock.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/world/level/block/AzaleaBlock.java