From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: lukas <lukasalt98@gmail.com>
Date: Sun, 27 Dec 2020 17:19:51 +0100
Subject: [PATCH] Optimized tick ready check


diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 066d5f7ee93351bff67c0d39ee9d940ac51515d8..b89cefc8890774dbc64fd6bddeb038d2ee36d485 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -854,13 +854,13 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
             if (!tileentity.isRemoved() && tileentity.hasLevel()) {
                 BlockPos blockposition = tileentity.getBlockPos();
 
-                if (this.getChunkSource().isTickingChunk(blockposition) && this.getWorldBorder().isWithinBounds(blockposition)) {
+                LevelChunk chunk; ChunkHolder playerChunk; if ((chunk = tileentity.getCurrentChunk()) != null && (playerChunk = chunk.playerChunk) != null && playerChunk.isTickingReady() && this.getWorldBorder().isInBounds(blockposition)) { // Paper - optimized tick ready check by inlining ChunkProviderServer.a(BlockPosition). Chunk lookup is no longer required and we can use the PlayerChunk directly available through the tile entity
                     try {
                         gameprofilerfiller.push(() -> {
                             return String.valueOf(BlockEntityType.getKey(tileentity.getType()));
                         });
                         tileentity.tickTimer.startTiming(); // Spigot
-                        if (tileentity.getType().isValid(this.getBlockState(blockposition).getBlock())) {
+                        if (tileentity.getType().isValid(chunk.getBlockState(blockposition).getBlock())) { // Paper - reuse the chunk from above, do not look it up again
                             ((TickableBlockEntity) tileentity).tick();
                         } else {
                             tileentity.logInvalidState();
@@ -893,9 +893,11 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
                 this.tickableBlockEntities.remove(tileTickPosition--);
                 // Spigot end
                 //this.tileEntityList.remove(tileentity); // Paper - remove unused list
-                if (this.hasChunkAt(tileentity.getBlockPos())) {
-                    this.getChunkAt(tileentity.getBlockPos()).removeBlockEntity(tileentity.getBlockPos());
+                // Paper - prevent double chunk lookups
+                LevelChunk chunk; if ((chunk = this.getChunkIfLoaded(tileentity.getBlockPos())) != null) { // inlined contents of this.isLoaded(BlockPosition). Reuse the returned chunk instead of looking it up again
+                    chunk.removeBlockEntity(tileentity.getBlockPos());
                 }
+                // Paper end
             }
         }
 
@@ -914,8 +916,8 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
                     }
                     // CraftBukkit end */
 
-                    if (this.hasChunkAt(tileentity1.getBlockPos())) {
-                        LevelChunk chunk = this.getChunkAt(tileentity1.getBlockPos());
+                    LevelChunk chunk; if ((chunk = this.getChunkIfLoaded(tileentity1.getBlockPos())) != null) {  // Paper - inlined contents of this.isLoaded(BlockPosition). Reuse the returned chunk instead of looking it up again
+                        // Chunk chunk = this.getChunkAtWorldCoords(tileentity1.getPosition()); // Paper - already computed above
                         BlockState iblockdata = chunk.getBlockState(tileentity1.getBlockPos());
 
                         chunk.setBlockEntity(tileentity1.getBlockPos(), tileentity1);