From d96359286981fff5f085e9e449f7881043c3c485 Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Thu, 3 Jun 2021 06:13:18 +0100
Subject: [PATCH] Do not tick Chunk TickS for every chunk

---
 ...-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch |  6 +++---
 ...-PlayerChunkMap-memory-use-for-visibleChunks.patch |  2 +-
 ...Optimize-isOutsideRange-to-use-distance-maps.patch |  2 +-
 .../PlayerNaturallySpawnCreaturesEvent.patch          |  2 +-
 Spigot-Server-Patches/Timings-v2.patch                | 11 ++++++++---
 5 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/Spigot-Server-Patches/Mid-Tick-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch b/Spigot-Server-Patches/Mid-Tick-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch
index 7de6b8efd3..05158f57e5 100644
--- a/Spigot-Server-Patches/Mid-Tick-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch
+++ b/Spigot-Server-Patches/Mid-Tick-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch
@@ -175,18 +175,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          this.world.getMethodProfiler().exit();
          this.clearCache();
 @@ -0,0 +0,0 @@ public class ChunkProviderServer extends IChunkProvider {
-                 entityPlayer.playerNaturallySpawnedEvent.callEvent();
              };
              // Paper end
+             this.world.timings.chunkTicks.startTiming(); // Paper
 -            this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
 +            final int[] chunksTicked = {0}; this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
                  Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
  
                  if (optional.isPresent()) {
 @@ -0,0 +0,0 @@ public class ChunkProviderServer extends IChunkProvider {
-                             this.world.timings.chunkTicks.startTiming(); // Spigot // Paper
+                             //this.world.timings.chunkTicks.startTiming(); // Spigot // Paper
                              this.world.a(chunk, k);
-                             this.world.timings.chunkTicks.stopTiming(); // Spigot // Paper
+                             //this.world.timings.chunkTicks.stopTiming(); // Spigot // Paper
 +                            if (chunksTicked[0]++ % 10 == 0) this.world.getMinecraftServer().midTickLoadChunks(); // Paper
                          }
                      }
diff --git a/Spigot-Server-Patches/Optimize-PlayerChunkMap-memory-use-for-visibleChunks.patch b/Spigot-Server-Patches/Optimize-PlayerChunkMap-memory-use-for-visibleChunks.patch
index 8b1334db81..4a251f3698 100644
--- a/Spigot-Server-Patches/Optimize-PlayerChunkMap-memory-use-for-visibleChunks.patch
+++ b/Spigot-Server-Patches/Optimize-PlayerChunkMap-memory-use-for-visibleChunks.patch
@@ -74,9 +74,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
 +++ b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
 @@ -0,0 +0,0 @@ public class ChunkProviderServer extends IChunkProvider {
-                 entityPlayer.playerNaturallySpawnedEvent.callEvent();
              };
              // Paper end
+             this.world.timings.chunkTicks.startTiming(); // Paper
 -            this.playerChunkMap.f().forEach((playerchunk) -> { // Paper - no... just no...
 +            this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
                  Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
diff --git a/Spigot-Server-Patches/Optimize-isOutsideRange-to-use-distance-maps.patch b/Spigot-Server-Patches/Optimize-isOutsideRange-to-use-distance-maps.patch
index ca93ed2fb0..31c7f24b30 100644
--- a/Spigot-Server-Patches/Optimize-isOutsideRange-to-use-distance-maps.patch
+++ b/Spigot-Server-Patches/Optimize-isOutsideRange-to-use-distance-maps.patch
@@ -132,9 +132,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 -            };
 -            // Paper end
 +            // Paper - moved up
+             this.world.timings.chunkTicks.startTiming(); // Paper
              final int[] chunksTicked = {0}; this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
                  Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
- 
 @@ -0,0 +0,0 @@ public class ChunkProviderServer extends IChunkProvider {
                          Chunk chunk = (Chunk) optional1.get();
                          ChunkCoordIntPair chunkcoordintpair = playerchunk.i();
diff --git a/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch b/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
index b9cb94235e..ce5144dabf 100644
--- a/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
+++ b/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
@@ -25,9 +25,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +                entityPlayer.playerNaturallySpawnedEvent.callEvent();
 +            };
 +            // Paper end
+             this.world.timings.chunkTicks.startTiming(); // Paper
              this.playerChunkMap.f().forEach((playerchunk) -> { // Paper - no... just no...
                  Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
- 
 diff --git a/src/main/java/net/minecraft/server/level/EntityPlayer.java b/src/main/java/net/minecraft/server/level/EntityPlayer.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/level/EntityPlayer.java
diff --git a/Spigot-Server-Patches/Timings-v2.patch b/Spigot-Server-Patches/Timings-v2.patch
index 506ebab012..8f3f81270f 100644
--- a/Spigot-Server-Patches/Timings-v2.patch
+++ b/Spigot-Server-Patches/Timings-v2.patch
@@ -1113,7 +1113,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
              this.p = spawnercreature_d;
              this.world.getMethodProfiler().exit();
-@@ -0,0 +0,0 @@ public class ChunkProviderServer extends IChunkProvider {
+             //List<PlayerChunk> list = Lists.newArrayList(this.playerChunkMap.f()); // Paper
+             //Collections.shuffle(list); // Paper
++            this.world.timings.chunkTicks.startTiming(); // Paper
+             this.playerChunkMap.f().forEach((playerchunk) -> { // Paper - no... just no...
+                 Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
  
                  if (optional.isPresent()) {
                      this.world.getMethodProfiler().enter("broadcast");
@@ -1128,14 +1132,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
                              }
  
 -                            this.world.timings.doTickTiles.startTiming(); // Spigot
-+                            this.world.timings.chunkTicks.startTiming(); // Spigot // Paper
++                            //this.world.timings.chunkTicks.startTiming(); // Spigot // Paper
                              this.world.a(chunk, k);
 -                            this.world.timings.doTickTiles.stopTiming(); // Spigot
-+                            this.world.timings.chunkTicks.stopTiming(); // Spigot // Paper
++                            //this.world.timings.chunkTicks.stopTiming(); // Spigot // Paper
                          }
                      }
                  }
              });
++            this.world.timings.chunkTicks.stopTiming(); // Paper
              this.world.getMethodProfiler().enter("customSpawners");
              if (flag1) {
 +                try (co.aikar.timings.Timing ignored = this.world.timings.miscMobSpawning.startTiming()) { // Paper - timings