From c8457716c48ee62f2b75480f42ba038bdb2843d8 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 28 Nov 2024 14:22:32 -0800
Subject: [PATCH] Use Player view distance for
 PlayerNaturallySpawnCreaturesEvent

The spigot view distance may not be correct, as the player may
have a specific view distance configured.
---
 patches/server/MC-Utils.patch                   |  6 +++---
 .../server/Moonrise-optimisation-patches.patch  | 10 +++++-----
 .../PlayerNaturallySpawnCreaturesEvent.patch    | 17 +++++++++--------
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/patches/server/MC-Utils.patch b/patches/server/MC-Utils.patch
index 5c2c40f084..d01fb2d06c 100644
--- a/patches/server/MC-Utils.patch
+++ b/patches/server/MC-Utils.patch
@@ -3582,15 +3582,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
 +    public static int getSendViewDistance(final ServerPlayer player) {
-+        return getLoadViewDistance(player);
++        return getLoadViewDistance(player) - 1;
 +    }
 +
 +    public static int getLoadViewDistance(final ServerPlayer player) {
 +        final ServerLevel level = player.serverLevel();
 +        if (level == null) {
-+            return org.bukkit.Bukkit.getViewDistance();
++            return org.bukkit.Bukkit.getViewDistance() + 1;
 +        }
-+        return level.chunkSource.chunkMap.getPlayerViewDistance(player);
++        return level.chunkSource.chunkMap.serverViewDistance + 1;
 +    }
 +
 +    public static int getTickViewDistance(final ServerPlayer player) {
diff --git a/patches/server/Moonrise-optimisation-patches.patch b/patches/server/Moonrise-optimisation-patches.patch
index 336efd567a..ee162ef920 100644
--- a/patches/server/Moonrise-optimisation-patches.patch
+++ b/patches/server/Moonrise-optimisation-patches.patch
@@ -330,16 +330,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
  
      public static int getSendViewDistance(final ServerPlayer player) {
--        return getLoadViewDistance(player);
+-        return getLoadViewDistance(player) - 1;
 +        return RegionizedPlayerChunkLoader.getAPISendViewDistance(player);
      }
  
      public static int getLoadViewDistance(final ServerPlayer player) {
 -        final ServerLevel level = player.serverLevel();
 -        if (level == null) {
--            return org.bukkit.Bukkit.getViewDistance();
+-            return org.bukkit.Bukkit.getViewDistance() + 1;
 -        }
--        return level.chunkSource.chunkMap.getPlayerViewDistance(player);
+-        return level.chunkSource.chunkMap.serverViewDistance + 1;
 +        return RegionizedPlayerChunkLoader.getLoadViewDistance(player);
      }
  
@@ -25029,8 +25029,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
  
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-         //double blockRange = (reducedRange) ? Math.pow(chunkRange << 4, 2) : 16384.0D; // Paper - use from event
-         double blockRange = 16384.0D; // Paper
+     private boolean anyPlayerCloseEnoughForSpawningInternal(ChunkPos chunkcoordintpair, boolean reducedRange) {
+         double blockRange; // Paper - use from event
          // Spigot end
 -        Iterator iterator = this.playerMap.getAllPlayers().iterator();
 -
diff --git a/patches/server/PlayerNaturallySpawnCreaturesEvent.patch b/patches/server/PlayerNaturallySpawnCreaturesEvent.patch
index 0dcc5f402d..de0bddb918 100644
--- a/patches/server/PlayerNaturallySpawnCreaturesEvent.patch
+++ b/patches/server/PlayerNaturallySpawnCreaturesEvent.patch
@@ -13,13 +13,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/net/minecraft/server/level/ChunkMap.java
 +++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-         chunkRange = (chunkRange > this.level.spigotConfig.viewDistance) ? (byte) this.level.spigotConfig.viewDistance : chunkRange;
-         chunkRange = (chunkRange > 8) ? 8 : chunkRange;
+     }
  
+     private boolean anyPlayerCloseEnoughForSpawningInternal(ChunkPos chunkcoordintpair, boolean reducedRange) {
+-        int chunkRange = this.level.spigotConfig.mobSpawnRange;
+-        chunkRange = (chunkRange > this.level.spigotConfig.viewDistance) ? (byte) this.level.spigotConfig.viewDistance : chunkRange;
+-        chunkRange = (chunkRange > 8) ? 8 : chunkRange;
+-
 -        double blockRange = (reducedRange) ? Math.pow(chunkRange << 4, 2) : 16384.0D;
-+        final int finalChunkRange = chunkRange; // Paper for lambda below
-+        //double blockRange = (reducedRange) ? Math.pow(chunkRange << 4, 2) : 16384.0D; // Paper - use from event
-+        double blockRange = 16384.0D; // Paper
++        double blockRange; // Paper - use from event
          // Spigot end
          Iterator iterator = this.playerMap.getAllPlayers().iterator();
  
@@ -48,10 +50,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
          if (flag && (this.spawnEnemies || this.spawnFriendlies)) {
 +            // Paper start - PlayerNaturallySpawnCreaturesEvent
-+            int chunkRange = level.spigotConfig.mobSpawnRange;
-+            chunkRange = (chunkRange > level.spigotConfig.viewDistance) ? (byte) level.spigotConfig.viewDistance : chunkRange;
-+            chunkRange = Math.min(chunkRange, 8);
 +            for (ServerPlayer entityPlayer : this.level.players()) {
++                int chunkRange = Math.min(level.spigotConfig.mobSpawnRange, entityPlayer.getBukkitEntity().getViewDistance());
++                chunkRange = Math.min(chunkRange, 8);
 +                entityPlayer.playerNaturallySpawnedEvent = new com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent(entityPlayer.getBukkitEntity(), (byte) chunkRange);
 +                entityPlayer.playerNaturallySpawnedEvent.callEvent();
 +            }