From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Mykyta Komarnytskyy <nkomarn@hotmail.com>
Date: Fri, 30 Oct 2020 20:13:08 -0700
Subject: [PATCH] Tile Entity optimizations

This patch includes various tile entity optimizations. Normally, these aren't going to improve performance much, but can be useful if you have loads of tile entities or they are being used as building blocks.

diff --git a/src/main/java/net/minecraft/server/TileEntityBeehive.java b/src/main/java/net/minecraft/server/TileEntityBeehive.java
index a60e0872d51aeb330bd5334e35f18ad0ed63834e..40bde90306106534e73d6845caf956514e5dcf88 100644
--- a/src/main/java/net/minecraft/server/TileEntityBeehive.java
+++ b/src/main/java/net/minecraft/server/TileEntityBeehive.java
@@ -286,6 +286,7 @@ public class TileEntityBeehive extends TileEntity implements ITickable {
 
     @Override
     public void tick() {
+        if (this.bees.size() == 0) return; // Yatopia - TE optimizations
         if (!this.world.isClientSide) {
             this.y();
             BlockPosition blockposition = this.getPosition();
diff --git a/src/main/java/net/minecraft/server/TileEntityEnchantTable.java b/src/main/java/net/minecraft/server/TileEntityEnchantTable.java
index c9066cb5f51cb2ad078aca3019e1df557062d286..46b18b6364645106b00bd4f1d721b8cd704ecabf 100644
--- a/src/main/java/net/minecraft/server/TileEntityEnchantTable.java
+++ b/src/main/java/net/minecraft/server/TileEntityEnchantTable.java
@@ -43,6 +43,7 @@ public class TileEntityEnchantTable extends TileEntity implements INamableTileEn
 
     @Override
     public void tick() {
+        if (!org.yatopiamc.yatopia.server.YatopiaConfig.shouldTickEnchantingTables) { return; } // Yatopia - TE optimizations
         this.j = this.i;
         this.l = this.k;
         EntityHuman entityhuman = this.world.a((double) this.position.getX() + 0.5D, (double) this.position.getY() + 0.5D, (double) this.position.getZ() + 0.5D, 3.0D, false);
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index c63d728082e28c13bc9724e74b154d02fbb2392e..2fedaf62905445fe91fd627420cfca337b8d37a7 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -917,13 +917,18 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
             if (!tileentity.isRemoved() && tileentity.hasWorld()) {
                 BlockPosition blockposition = tileentity.getPosition();
 
-                if (this.getChunkProvider().a(blockposition) && this.getWorldBorder().a(blockposition)) {
+                // Yatopia start - TE optimizations
+                Chunk chunk = tileentity.getCurrentChunk();
+                PlayerChunk playerchunk = chunk == null ? null : chunk.playerChunk;
+                if (playerchunk == null) continue;
+                if (playerchunk.isTickingReady() && this.getWorldBorder().a(blockposition)) {
                     try {
                         gameprofilerfiller.a(() -> {
                             return String.valueOf(TileEntityTypes.a(tileentity.getTileType()));
                         });
                         tileentity.tickTimer.startTiming(); // Spigot
-                        if (tileentity.getTileType().isValidBlock(this.getType(blockposition).getBlock())) {
+                        if (tileentity.getTileType().isValidBlock(chunk.getType(blockposition).getBlock())) {
+                            // Yatopia end
                             ((ITickable) tileentity).tick();
                         } else {
                             tileentity.w();
@@ -957,9 +962,12 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
                 this.tileEntityListTick.remove(tileTickPosition--);
                 // Spigot end
                 //this.tileEntityList.remove(tileentity); // Paper - remove unused list
-                if (this.isLoaded(tileentity.getPosition())) {
-                    this.getChunkAtWorldCoords(tileentity.getPosition()).removeTileEntity(tileentity.getPosition());
-                }
+                // Yatopia start - TE optimizations
+                Chunk chunk = tileentity.getCurrentChunk();
+                if (chunk == null) continue;
+                // if (this.isLoaded(tileentity.getPosition())) {
+                    chunk.removeTileEntity(tileentity.getPosition());
+                // } // Yatopia end
             }
         }
 
@@ -978,8 +986,11 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
                     }
                     // CraftBukkit end */
 
-                    if (this.isLoaded(tileentity1.getPosition())) {
-                        Chunk chunk = this.getChunkAtWorldCoords(tileentity1.getPosition());
+                    // Yatopia start - TE optimizations
+                    Chunk chunk = tileentity1.getCurrentChunk();
+                    if (chunk == null) continue;
+                    // if (this.isLoaded(tileentity1.getPosition())) {
+                        // Chunk chunk = this.getChunkAtWorldCoords(tileentity1.getPosition());
                         IBlockData iblockdata = chunk.getType(tileentity1.getPosition());
 
                         chunk.setTileEntity(tileentity1.getPosition(), tileentity1);
@@ -990,7 +1001,7 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
                             this.a(tileentity1);
                         }
                         // CraftBukkit end
-                    }
+                    // } // Yatopia end
                 }
             }
 
diff --git a/src/main/java/org/yatopiamc/yatopia/server/YatopiaConfig.java b/src/main/java/org/yatopiamc/yatopia/server/YatopiaConfig.java
index 34b5a087d1d5d84b193adbd756add060a2d49354..2d4fb0a4664578f8d5c23db854eb8f2764724940 100644
--- a/src/main/java/org/yatopiamc/yatopia/server/YatopiaConfig.java
+++ b/src/main/java/org/yatopiamc/yatopia/server/YatopiaConfig.java
@@ -283,4 +283,9 @@ public class YatopiaConfig {
         checkVehicleMovedQuickly = getBoolean("settings.checks.vehicle-moved-quickly", checkVehicleMovedQuickly);
         checkVehicleMovedWrongly = getBoolean("settings.checks.vehicle-moved-wrongly", checkVehicleMovedWrongly);
     }
+
+    public static boolean shouldTickEnchantingTables = false;
+    private static void tickEnchantingTables() {
+        shouldTickEnchantingTables = getBoolean("settings.tick.enchanting-tables", shouldTickEnchantingTables);
+    }
 }
