From 494b03ea47d9ae95b1f0778931d67f7c1ac4bab3 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 8 Jun 2023 16:21:20 -0700
Subject: [PATCH] Apply last patch

---
 .../Fix-dangerous-end-portal-logic.patch      |  6 +--
 .../Optimise-nearby-player-lookups.patch      |  8 ++--
 ...erCloseEnoughForSpawning-to-use-dist.patch | 12 +++---
 ...tance-map-to-optimise-entity-tracker.patch | 40 +++++--------------
 4 files changed, 23 insertions(+), 43 deletions(-)
 rename patches/{unapplied => }/server/Use-distance-map-to-optimise-entity-tracker.patch (89%)

diff --git a/patches/server/Fix-dangerous-end-portal-logic.patch b/patches/server/Fix-dangerous-end-portal-logic.patch
index f4db15ac0b..d17c618531 100644
--- a/patches/server/Fix-dangerous-end-portal-logic.patch
+++ b/patches/server/Fix-dangerous-end-portal-logic.patch
@@ -15,9 +15,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/net/minecraft/world/entity/Entity.java
 +++ b/src/main/java/net/minecraft/world/entity/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
- 
-     public boolean updatingSectionStatus = false;
-     // Paper end
+         return chunkMap.playerEntityTrackerTrackMaps[type.ordinal()].getObjectsInRange(MCUtil.getCoordinateKey(this));
+     }
+     // Paper end - optimise entity tracking
 +    // Paper start - make end portalling safe
 +    public BlockPos portalBlock;
 +    public ServerLevel portalWorld;
diff --git a/patches/server/Optimise-nearby-player-lookups.patch b/patches/server/Optimise-nearby-player-lookups.patch
index d2b43d7adc..d16dc8c783 100644
--- a/patches/server/Optimise-nearby-player-lookups.patch
+++ b/patches/server/Optimise-nearby-player-lookups.patch
@@ -56,9 +56,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      // Paper start - distance maps
      private final com.destroystokyo.paper.util.misc.PooledLinkedHashSets<ServerPlayer> pooledLinkedPlayerHashSets = new com.destroystokyo.paper.util.misc.PooledLinkedHashSets<>();
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-             this.playerMobDistanceMap.add(player, chunkX, chunkZ, io.papermc.paper.chunk.system.ChunkSystem.getTickViewDistance(player));
+             trackMap.add(player, chunkX, chunkZ, Math.min(trackRange, io.papermc.paper.chunk.system.ChunkSystem.getSendViewDistance(player)));
          }
-         // Paper end - per player mob spawning
+         // Paper end - use distance map to optimise entity tracker
 +        this.playerGeneralAreaMap.add(player, chunkX, chunkZ, GENERAL_AREA_MAP_SQUARE_RADIUS); // Paper - optimise checkDespawn
      }
  
@@ -72,9 +72,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          if (this.playerMobDistanceMap != null) {
              this.playerMobDistanceMap.remove(player);
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-             this.playerMobDistanceMap.update(player, chunkX, chunkZ, io.papermc.paper.chunk.system.ChunkSystem.getTickViewDistance(player));
+             trackMap.update(player, chunkX, chunkZ, Math.min(trackRange, io.papermc.paper.chunk.system.ChunkSystem.getSendViewDistance(player)));
          }
-         // Paper end - per player mob spawning
+         // Paper end - use distance map to optimise entity tracker
 +        this.playerGeneralAreaMap.update(player, chunkX, chunkZ, GENERAL_AREA_MAP_SQUARE_RADIUS); // Paper - optimise checkDespawn
      }
      // Paper end
diff --git a/patches/server/Optimize-anyPlayerCloseEnoughForSpawning-to-use-dist.patch b/patches/server/Optimize-anyPlayerCloseEnoughForSpawning-to-use-dist.patch
index b69f633cf2..ef4e163d54 100644
--- a/patches/server/Optimize-anyPlayerCloseEnoughForSpawning-to-use-dist.patch
+++ b/patches/server/Optimize-anyPlayerCloseEnoughForSpawning-to-use-dist.patch
@@ -46,9 +46,9 @@ 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
- 
-     // Paper start - distance maps
-     private final com.destroystokyo.paper.util.misc.PooledLinkedHashSets<ServerPlayer> pooledLinkedPlayerHashSets = new com.destroystokyo.paper.util.misc.PooledLinkedHashSets<>();
+         return net.minecraft.server.MinecraftServer.getServer().getScaledTrackingDistance(vanilla);
+     }
+     // Paper end - use distance map to optimise tracker
 +    // Paper start - optimise ChunkMap#anyPlayerCloseEnoughForSpawning
 +    // A note about the naming used here:
 +    // Previously, mojang used a "spawn range" of 8 for controlling both ticking and
@@ -90,9 +90,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          if (this.playerMobDistanceMap != null) {
              this.playerMobDistanceMap.update(player, chunkX, chunkZ, io.papermc.paper.chunk.system.ChunkSystem.getTickViewDistance(player));
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-         this.regionManagers.add(this.dataRegionManager);
-         // Paper end
-         this.playerMobDistanceMap = this.level.paperConfig().entities.spawning.perPlayerMobSpawns ? new com.destroystokyo.paper.util.misc.PlayerAreaMap(this.pooledLinkedPlayerHashSets) : null; // Paper
+             this.playerEntityTrackerTrackMaps[ordinal] = new com.destroystokyo.paper.util.misc.PlayerAreaMap(this.pooledLinkedPlayerHashSets);
+         }
+         // Paper end - use distance map to optimise entity tracker
 +        // Paper start - optimise ChunkMap#anyPlayerCloseEnoughForSpawning
 +        this.playerChunkTickRangeMap = new com.destroystokyo.paper.util.misc.PlayerAreaMap(this.pooledLinkedPlayerHashSets,
 +            (ServerPlayer player, int rangeX, int rangeZ, int currPosX, int currPosZ, int prevPosX, int prevPosZ,
diff --git a/patches/unapplied/server/Use-distance-map-to-optimise-entity-tracker.patch b/patches/server/Use-distance-map-to-optimise-entity-tracker.patch
similarity index 89%
rename from patches/unapplied/server/Use-distance-map-to-optimise-entity-tracker.patch
rename to patches/server/Use-distance-map-to-optimise-entity-tracker.patch
index 9ee059fe4d..e29b38d7e5 100644
--- a/patches/unapplied/server/Use-distance-map-to-optimise-entity-tracker.patch
+++ b/patches/server/Use-distance-map-to-optimise-entity-tracker.patch
@@ -9,18 +9,10 @@ diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/j
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/level/ChunkMap.java
 +++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
-@@ -0,0 +0,0 @@ import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
- import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
- import net.minecraft.network.protocol.game.DebugPackets;
- import io.papermc.paper.util.MCUtil;
-+import net.minecraft.server.MinecraftServer;
- import net.minecraft.server.level.progress.ChunkProgressListener;
- import net.minecraft.server.network.ServerPlayerConnection;
- import net.minecraft.util.CsvOutput;
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-     public final com.destroystokyo.paper.util.misc.PlayerAreaMap playerMobSpawnMap; // this map is absent from updateMaps since it's controlled at the start of the chunkproviderserver tick
-     public final com.destroystokyo.paper.util.misc.PlayerAreaMap playerChunkTickRangeMap;
-     // Paper end - optimise ChunkMap#anyPlayerCloseEnoughForSpawning
+ 
+     // Paper start - distance maps
+     private final com.destroystokyo.paper.util.misc.PooledLinkedHashSets<ServerPlayer> pooledLinkedPlayerHashSets = new com.destroystokyo.paper.util.misc.PooledLinkedHashSets<>();
 +    // Paper start - use distance map to optimise tracker
 +    public static boolean isLegacyTrackingEntity(Entity entity) {
 +        return entity.isLegacyTrackingEntity;
@@ -35,12 +27,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
 +    private int convertSpigotRangeToVanilla(final int vanilla) {
-+        return MinecraftServer.getServer().getScaledTrackingDistance(vanilla);
++        return net.minecraft.server.MinecraftServer.getServer().getScaledTrackingDistance(vanilla);
 +    }
 +    // Paper end - use distance map to optimise tracker
  
      void addPlayerToDistanceMaps(ServerPlayer player) {
-         this.playerChunkManager.addPlayer(player); // Paper - replace chunk loader
+         this.level.playerChunkLoader.addPlayer(player); // Paper - replace chunk loader
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
              this.playerMobDistanceMap.add(player, chunkX, chunkZ, io.papermc.paper.chunk.system.ChunkSystem.getTickViewDistance(player));
          }
@@ -126,9 +118,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +            this.playerEntityTrackerTrackMaps[ordinal] = new com.destroystokyo.paper.util.misc.PlayerAreaMap(this.pooledLinkedPlayerHashSets);
 +        }
 +        // Paper end - use distance map to optimise entity tracker
-         // Paper start - optimise ChunkMap#anyPlayerCloseEnoughForSpawning
-         this.playerChunkTickRangeMap = new com.destroystokyo.paper.util.misc.PlayerAreaMap(this.pooledLinkedPlayerHashSets,
-             (ServerPlayer player, int rangeX, int rangeZ, int currPosX, int currPosZ, int prevPosX, int prevPosZ,
+     }
+ 
+     protected ChunkGenerator generator() {
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
      }
  
@@ -196,8 +188,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          List<ServerPlayer> list1 = this.level.players();
          ObjectIterator objectiterator = this.entityMap.values().iterator();
 @@ -0,0 +0,0 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
-         }));
-         // Paper end
+ 
+         player.trackChunk(chunk.getPos(), (Packet) cachedDataPacket.getValue());
          DebugPackets.sendPoiPacketsForChunk(this.level, chunk.getPos());
 -        List<Entity> list = Lists.newArrayList();
 -        List<Entity> list1 = Lists.newArrayList();
@@ -342,18 +334,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/spigotmc/TrackingRange.java
 +++ b/src/main/java/org/spigotmc/TrackingRange.java
 @@ -0,0 +0,0 @@ public class TrackingRange
-         {
-             return defaultRange;
-         }
-+        if (entity instanceof net.minecraft.world.entity.boss.enderdragon.EnderDragon) return defaultRange; // Paper - enderdragon is exempt
-         SpigotWorldConfig config = entity.level.spigotConfig;
-         if ( entity instanceof ServerPlayer )
-         {
-@@ -0,0 +0,0 @@ public class TrackingRange
-             return config.displayTrackingRange;
-         } else
-         {
--            if (entity instanceof net.minecraft.world.entity.boss.enderdragon.EnderDragon) return ((net.minecraft.server.level.ServerLevel)(entity.getCommandSenderWorld())).getChunkSource().chunkMap.getEffectiveViewDistance(); // Paper - enderdragon is exempt
              return config.otherTrackingRange;
          }
      }