From 3b3cf7f53285cbf71edb95d544d0554b88db2e7c Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Wed, 5 Jul 2023 23:13:30 +0100
Subject: [PATCH] Don't allow for supporting block checks to load chunks

I cannot test this due to lack of hardware (took 30 minutes getting paper
updated and loaded into IJ on this machine...), however, this is the easiest
patch for now. Ideally, some form of distance check would probably be added to
improve invalidation of this cache.
---
 ...d-chunks-for-supporting-block-checks.patch | 19 +++++++++++++++++++
 patches/server/MC-Utils.patch                 |  1 +
 patches/server/Rewrite-chunk-system.patch     |  2 +-
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 patches/server/Don-t-load-chunks-for-supporting-block-checks.patch

diff --git a/patches/server/Don-t-load-chunks-for-supporting-block-checks.patch b/patches/server/Don-t-load-chunks-for-supporting-block-checks.patch
new file mode 100644
index 0000000000..98d9648bb1
--- /dev/null
+++ b/patches/server/Don-t-load-chunks-for-supporting-block-checks.patch
@@ -0,0 +1,19 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Shane Freeder <theboyetronic@gmail.com>
+Date: Wed, 5 Jul 2023 23:11:53 +0100
+Subject: [PATCH] Don't load chunks for supporting block checks
+
+
+diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- 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 {
+     }
+ 
+     protected BlockPos getOnPos(float offset) {
+-        if (this.mainSupportingBlockPos.isPresent()) {
++        if (this.mainSupportingBlockPos.isPresent() && this.level().getChunkIfLoadedImmediately(this.mainSupportingBlockPos.get()) != null) { // Paper - ensure no loads
+             BlockPos blockposition = (BlockPos) this.mainSupportingBlockPos.get();
+ 
+             if (offset <= 1.0E-5F) {
diff --git a/patches/server/MC-Utils.patch b/patches/server/MC-Utils.patch
index faaa2d8135..dffd4ee513 100644
--- a/patches/server/MC-Utils.patch
+++ b/patches/server/MC-Utils.patch
@@ -6935,6 +6935,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create);
  
 +    @Nullable ChunkAccess getChunkIfLoadedImmediately(int x, int z); // Paper - ifLoaded api (we need this since current impl blocks if the chunk is loading)
++    @Nullable default ChunkAccess getChunkIfLoadedImmediately(BlockPos pos) { return this.getChunkIfLoadedImmediately(pos.getX() >> 4, pos.getZ() >> 4);}
 +
      /** @deprecated */
      @Deprecated
diff --git a/patches/server/Rewrite-chunk-system.patch b/patches/server/Rewrite-chunk-system.patch
index 11291ed053..cab068f113 100644
--- a/patches/server/Rewrite-chunk-system.patch
+++ b/patches/server/Rewrite-chunk-system.patch
@@ -21216,8 +21216,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    // Paper end - rewrite chunk system
 +
      @Nullable ChunkAccess getChunkIfLoadedImmediately(int x, int z); // Paper - ifLoaded api (we need this since current impl blocks if the chunk is loading)
+     @Nullable default ChunkAccess getChunkIfLoadedImmediately(BlockPos pos) { return this.getChunkIfLoadedImmediately(pos.getX() >> 4, pos.getZ() >> 4);}
  
-     /** @deprecated */
 diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkGenerator.java b/src/main/java/net/minecraft/world/level/chunk/ChunkGenerator.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/world/level/chunk/ChunkGenerator.java