From aff206ef815b55710ef59fae5d776fd182714bfb Mon Sep 17 00:00:00 2001
From: Spottedleaf <spottedleaf@spottedleaf.dev>
Date: Sun, 10 May 2020 22:49:58 -0400
Subject: [PATCH] Optimize WorldBorder collision checks and air

Port part of leafs changes from Tuinity, will worry about rest later with larger changes coming
---
 ...ptimize-Collision-to-not-load-chunks.patch |  2 +-
 ...WorldBorder-collision-checks-and-air.patch | 61 +++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 Spigot-Server-Patches/Optimize-WorldBorder-collision-checks-and-air.patch

diff --git a/Spigot-Server-Patches/Optimize-Collision-to-not-load-chunks.patch b/Spigot-Server-Patches/Optimize-Collision-to-not-load-chunks.patch
index 7d651a904d..60b93b77a2 100644
--- a/Spigot-Server-Patches/Optimize-Collision-to-not-load-chunks.patch
+++ b/Spigot-Server-Patches/Optimize-Collision-to-not-load-chunks.patch
@@ -40,7 +40,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +                        // Paper start - ensure we don't load chunks
 +                        //int k2 = k1 >> 4;
 +                        //int l2 = i2 >> 4;
-+                        boolean far = entity != null && MCUtil.distanceSq(entity.locX(), y, entity.locZ(), x, y, z) > 8;
++                        boolean far = entity != null && MCUtil.distanceSq(entity.locX(), y, entity.locZ(), x, y, z) > 14;
 +                        blockposition_mutableblockposition.setValues(x, y, z);
 +
 +                        boolean isRegionLimited = ICollisionAccess.this instanceof RegionLimitedWorldAccess;
diff --git a/Spigot-Server-Patches/Optimize-WorldBorder-collision-checks-and-air.patch b/Spigot-Server-Patches/Optimize-WorldBorder-collision-checks-and-air.patch
new file mode 100644
index 0000000000..a56e8b94c7
--- /dev/null
+++ b/Spigot-Server-Patches/Optimize-WorldBorder-collision-checks-and-air.patch
@@ -0,0 +1,61 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Spottedleaf <Spottedleaf@users.noreply.github.com>
+Date: Sun, 10 May 2020 22:49:05 -0400
+Subject: [PATCH] Optimize WorldBorder collision checks and air
+
+
+diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/server/Entity.java
++++ b/src/main/java/net/minecraft/server/Entity.java
+@@ -0,0 +0,0 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
+         AxisAlignedBB axisalignedbb = this.getBoundingBox();
+         VoxelShapeCollision voxelshapecollision = VoxelShapeCollision.a(this);
+         VoxelShape voxelshape = this.world.getWorldBorder().a();
+-        Stream<VoxelShape> stream = VoxelShapes.c(voxelshape, VoxelShapes.a(axisalignedbb.shrink(1.0E-7D)), OperatorBoolean.AND) ? Stream.empty() : Stream.of(voxelshape);
++        Stream<VoxelShape> stream = !this.world.getWorldBorder().isInBounds(axisalignedbb) ? Stream.empty() : Stream.of(this.world.getWorldBorder().a()); // Paper
+         Stream<VoxelShape> stream1 = this.world.b(this, axisalignedbb.a(vec3d), (Set) ImmutableSet.of());
+         StreamAccumulator<VoxelShape> streamaccumulator = new StreamAccumulator<>(Stream.concat(stream1, stream));
+         Vec3D vec3d1 = vec3d.g() == 0.0D ? vec3d : a(this, vec3d, axisalignedbb, this.world, voxelshapecollision, streamaccumulator);
+diff --git a/src/main/java/net/minecraft/server/ICollisionAccess.java b/src/main/java/net/minecraft/server/ICollisionAccess.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/server/ICollisionAccess.java
++++ b/src/main/java/net/minecraft/server/ICollisionAccess.java
+@@ -0,0 +0,0 @@ public interface ICollisionAccess extends IBlockAccess {
+             if (true) { //public boolean tryAdvance(Consumer<? super VoxelShape> consumer) {*/ // Paper
+                 if (entity != null) {
+                     // Paper end
+-                    VoxelShape voxelshape1 = ICollisionAccess.this.getWorldBorder().a();
+-                    boolean flag = VoxelShapes.c(voxelshape1, VoxelShapes.a(entity.getBoundingBox().shrink(1.0E-7D)), OperatorBoolean.AND);
+-                    boolean flag1 = VoxelShapes.c(voxelshape1, VoxelShapes.a(entity.getBoundingBox().g(1.0E-7D)), OperatorBoolean.AND);
++                    //VoxelShape voxelshape1 = ICollisionAccess.this.getWorldBorder().a(); // Paper - only make if collides
++                    boolean flag = !ICollisionAccess.this.getWorldBorder().isInBounds(entity.getBoundingBox().shrink(1.0E-7D)); // Paper
++                    boolean flag1 = !ICollisionAccess.this.getWorldBorder().isInBounds(entity.getBoundingBox().g(1.0E-7D)); // Paper
+ 
+                     if (!flag && flag1) {
+-                        collisions.add(voxelshape1);// Paper
++                        collisions.add(ICollisionAccess.this.getWorldBorder().a());// Paper
+                         if (returnFast) return collisions;
+                     }
+                 }
+@@ -0,0 +0,0 @@ public interface ICollisionAccess extends IBlockAccess {
+                             //IBlockData iblockdata = iblockaccess.getType(blockposition_mutableblockposition); // moved up
+                             // Paper end
+ 
+-                            if ((j2 != 1 || iblockdata.f()) && (j2 != 2 || iblockdata.getBlock() == Blocks.MOVING_PISTON)) {
++                            if (!iblockdata.isAir() && (j2 != 1 || iblockdata.f()) && (j2 != 2 || iblockdata.getBlock() == Blocks.MOVING_PISTON)) { // Paper - fast track air
+                                 VoxelShape voxelshape2 = iblockdata.b((IBlockAccess) ICollisionAccess.this, blockposition_mutableblockposition, voxelshapecollision);
+ 
+                                 // Paper start - Lithium Collision Optimizations
+diff --git a/src/main/java/net/minecraft/server/WorldBorder.java b/src/main/java/net/minecraft/server/WorldBorder.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/server/WorldBorder.java
++++ b/src/main/java/net/minecraft/server/WorldBorder.java
+@@ -0,0 +0,0 @@ public class WorldBorder {
+         return (double) chunkcoordintpair.f() > this.c() && (double) chunkcoordintpair.d() < this.e() && (double) chunkcoordintpair.g() > this.d() && (double) chunkcoordintpair.e() < this.f();
+     }
+ 
++    public final boolean isInBounds(AxisAlignedBB aabb) { return this.a(aabb); } // Paper - OBFHELPER
+     public boolean a(AxisAlignedBB axisalignedbb) {
+         return axisalignedbb.maxX > this.c() && axisalignedbb.minX < this.e() && axisalignedbb.maxZ > this.d() && axisalignedbb.minZ < this.f();
+     }