From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 8 Mar 2015 22:55:25 -0600
Subject: [PATCH] Optimize TileEntity Ticking

ticking logic changes implemented by mojang


diff --git a/src/main/java/co/aikar/timings/TimingsExport.java b/src/main/java/co/aikar/timings/TimingsExport.java
index 94adf0275a2e7093c152cc3b8b0a5747b3a13a86..5bcf9cefc29eb20e2cfbfb49e2b2662ec394a87e 100644
--- a/src/main/java/co/aikar/timings/TimingsExport.java
+++ b/src/main/java/co/aikar/timings/TimingsExport.java
@@ -112,7 +112,7 @@ public class TimingsExport extends Thread {
             pair("end", System.currentTimeMillis() / 1000),
             pair("online-mode", Bukkit.getServer().getOnlineMode()),
             pair("sampletime", (System.currentTimeMillis() - TimingsManager.timingStart) / 1000),
-            pair("datapacks", toArrayMapper(MinecraftServer.getServer().getPackRepository().getSelectedIds(), pack -> {
+            pair("datapacks", toArrayMapper(MinecraftServer.getServer().getPackRepository().getSelectedPacks(), pack -> {
                 // Don't feel like obf helper'ing these, non fatal if its temp missed.
                 return ChatColor.stripColor(CraftChatMessage.fromComponent(pack.a(true)));
             }))
@@ -151,8 +151,8 @@ public class TimingsExport extends Thread {
         );
 
         parent.put("worlds", toObjectMapper(MinecraftServer.getServer().getAllLevels(), world -> {
-            if (world.getWorldData().getName().equals("worldeditregentempworld")) return null;
-            return pair(world.getWorldData().getName(), createObject(
+            if (world.getWorld().getName().equals("worldeditregentempworld")) return null;
+            return pair(world.getWorld().getName(), createObject(
                 pair("gamerules", toObjectMapper(world.getWorld().getGameRules(), rule -> {
                     return pair(rule, world.getWorld().getGameRuleValue(rule));
                 })),
diff --git a/src/main/java/net/minecraft/world/level/block/ChestBlock.java b/src/main/java/net/minecraft/world/level/block/ChestBlock.java
index 56656bf34db07bc717ace8ae9c1b60f9bfd7ff05..1bda9a158eb4372b9ab7cf3097732e64810aefc6 100644
--- a/src/main/java/net/minecraft/world/level/block/ChestBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/ChestBlock.java
@@ -54,8 +54,8 @@ import net.minecraft.world.phys.shapes.VoxelShape;
 public class ChestBlock extends AbstractChestBlock<ChestBlockEntity> implements SimpleWaterloggedBlock {
 
     public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
-    public static final EnumProperty<ChestType> TYPE = BlockStateProperties.CHEST_TYPE;
-    public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
+    public static final EnumProperty<ChestType> TYPE = BlockStateProperties.CHEST_TYPE; public static final EnumProperty<ChestType> CHEST_TYPE_PROPERTY = TYPE; // Paper - OBFHELPER
+    public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final BooleanProperty waterlogged() { return WATERLOGGED; } // Paper OBFHELPER
     protected static final VoxelShape NORTH_AABB = Block.box(1.0D, 0.0D, 0.0D, 15.0D, 14.0D, 15.0D);
     protected static final VoxelShape SOUTH_AABB = Block.box(1.0D, 0.0D, 1.0D, 15.0D, 14.0D, 16.0D);
     protected static final VoxelShape WEST_AABB = Block.box(0.0D, 0.0D, 1.0D, 15.0D, 14.0D, 15.0D);
diff --git a/src/main/java/net/minecraft/world/level/block/entity/ChestBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/ChestBlockEntity.java
index 7b08ee35d2d8dc3fe783d773bf6686a5197006b8..17289d28b6d0023279a573715ee3d182988dd651 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/ChestBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/ChestBlockEntity.java
@@ -8,6 +8,7 @@ import net.minecraft.core.NonNullList;
 import net.minecraft.nbt.CompoundTag;
 import net.minecraft.network.chat.Component;
 import net.minecraft.network.chat.TranslatableComponent;
+import net.minecraft.server.MCUtil;
 import net.minecraft.sounds.SoundEvent;
 import net.minecraft.sounds.SoundEvents;
 import net.minecraft.sounds.SoundSource;
@@ -32,7 +33,7 @@ import org.bukkit.craftbukkit.entity.CraftHumanEntity;
 import org.bukkit.entity.HumanEntity;
 // CraftBukkit end
 
-public class ChestBlockEntity extends RandomizableContainerBlockEntity implements TickableBlockEntity {
+public class ChestBlockEntity extends RandomizableContainerBlockEntity { // Paper - Remove ITickable
 
     private NonNullList<ItemStack> items;
     protected float openness;
@@ -110,14 +111,20 @@ public class ChestBlockEntity extends RandomizableContainerBlockEntity implement
         return tag;
     }
 
-    @Override
     public void tick() {
         int i = this.worldPosition.getX();
         int j = this.worldPosition.getY();
         int k = this.worldPosition.getZ();
 
         ++this.tickInterval;
-        this.openCount = getOpenCount(this.level, this, this.tickInterval, i, j, k, this.openCount);
+    }
+
+    public void doOpenLogic() {
+        int i = this.worldPosition.getX();
+        int j = this.worldPosition.getY();
+        int k = this.worldPosition.getZ();
+
+        //this.viewingCount = a(this.world, this, this.j, i, j, k, this.viewingCount); // Paper - check is faulty given our logic is called before active container set
         this.oOpenness = this.openness;
         float f = 0.1F;
 
@@ -131,25 +138,31 @@ public class ChestBlockEntity extends RandomizableContainerBlockEntity implement
         if (this.openCount > 0 && this.openness == 0.0F) {
             this.playSound(SoundEvents.CHEST_OPEN);
         }
+    }
 
-        if (this.openCount == 0 && this.openness > 0.0F || this.openCount > 0 && this.openness < 1.0F) {
-            float f1 = this.openness;
+    public void doCloseLogic() {
+        if (this.openCount == 0 /* && this.a > 0.0F || this.viewingCount > 0 && this.a < 1.0F */) { // Paper - disable all but player count check
+            /* // Paper - disable animation stuff
+            float f1 = this.a;
 
-            if (this.openCount > 0) {
-                this.openness += 0.1F;
+            if (this.viewingCount > 0) {
+                this.a += 0.1F;
             } else {
-                this.openness -= 0.1F;
+                this.a -= 0.1F;
             }
 
-            if (this.openness > 1.0F) {
-                this.openness = 1.0F;
+            if (this.a > 1.0F) {
+                this.a = 1.0F;
             }
 
             float f2 = 0.5F;
 
-            if (this.openness < 0.5F && f1 >= 0.5F) {
+            if (this.a < 0.5F && f1 >= 0.5F) {
+            */
+            MCUtil.scheduleTask(10, () -> {
                 this.playSound(SoundEvents.CHEST_CLOSE);
-            }
+                }, "Chest Sounds");
+            //} // Paper end
 
             if (this.openness < 0.0F) {
                 this.openness = 0.0F;
@@ -188,6 +201,7 @@ public class ChestBlockEntity extends RandomizableContainerBlockEntity implement
     }
 
     public void playSound(SoundEvent soundeffect) {
+        if (!this.getBlockState().contains(ChestBlock.CHEST_TYPE_PROPERTY)) { return; } // Paper - this can be delayed, double check exists - Fixes GH-2074
         ChestType blockpropertychesttype = (ChestType) this.getBlockState().getValue(ChestBlock.TYPE);
 
         if (blockpropertychesttype != ChestType.LEFT) {
@@ -226,6 +240,7 @@ public class ChestBlockEntity extends RandomizableContainerBlockEntity implement
 
             ++this.openCount;
             if (this.level == null) return; // CraftBukkit
+            doOpenLogic(); // Paper
 
             // CraftBukkit start - Call redstone event
             if (this.getBlockState().getBlock() == Blocks.TRAPPED_CHEST) {
@@ -248,6 +263,7 @@ public class ChestBlockEntity extends RandomizableContainerBlockEntity implement
             --this.openCount;
 
             // CraftBukkit start - Call redstone event
+            doCloseLogic(); // Paper
             if (this.getBlockState().getBlock() == Blocks.TRAPPED_CHEST) {
                 int newPower = Math.max(0, Math.min(15, this.openCount));
 
diff --git a/src/main/java/net/minecraft/world/level/block/entity/EnderChestBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/EnderChestBlockEntity.java
index b26337770e13c20f57a4e74282710ce697ac0d41..8f0477d9620ef71e10855bbca07f9b6984d5d794 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/EnderChestBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/EnderChestBlockEntity.java
@@ -1,11 +1,12 @@
 package net.minecraft.world.level.block.entity;
 
+import net.minecraft.server.MCUtil;
 import net.minecraft.sounds.SoundEvents;
 import net.minecraft.sounds.SoundSource;
 import net.minecraft.world.entity.player.Player;
 import net.minecraft.world.level.block.Blocks;
 
-public class EnderChestBlockEntity extends BlockEntity implements TickableBlockEntity {
+public class EnderChestBlockEntity extends BlockEntity { // Paper - Remove ITickable
 
     public float openness;
     public float oOpenness;
@@ -16,18 +17,28 @@ public class EnderChestBlockEntity extends BlockEntity implements TickableBlockE
         super(BlockEntityType.ENDER_CHEST);
     }
 
-    @Override
     public void tick() {
         if (++this.tickInterval % 20 * 4 == 0) {
             this.level.blockEvent(this.worldPosition, Blocks.ENDER_CHEST, 1, this.openCount);
         }
 
         this.oOpenness = this.openness;
+        /* // Paper
+        int i = this.position.getX();
+        int j = this.position.getY();
+        int k = this.position.getZ();
+        float f = 0.1F;
+        double d0;
+        // Paper start
+        */
+    }
+
+    private void doOpenLogic() {
         int i = this.worldPosition.getX();
         int j = this.worldPosition.getY();
         int k = this.worldPosition.getZ();
-        float f = 0.1F;
         double d0;
+        // Paper end
 
         if (this.openCount > 0 && this.openness == 0.0F) {
             double d1 = (double) i + 0.5D;
@@ -35,28 +46,40 @@ public class EnderChestBlockEntity extends BlockEntity implements TickableBlockE
             d0 = (double) k + 0.5D;
             this.level.playSound((Player) null, d1, (double) j + 0.5D, d0, SoundEvents.ENDER_CHEST_OPEN, SoundSource.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
         }
+        // Paper start
+    }
 
-        if (this.openCount == 0 && this.openness > 0.0F || this.openCount > 0 && this.openness < 1.0F) {
-            float f1 = this.openness;
+    private void doCloseLogic() {
+        int i = this.worldPosition.getX();
+        int j = this.worldPosition.getY();
+        int k = this.worldPosition.getZ();
+        double d0;
+
+        if (this.openCount == 0) { /* && this.a > 0.0F || this.c > 0 && this.a < 1.0F) {
+        // Paper end
+            float f1 = this.a;
 
-            if (this.openCount > 0) {
-                this.openness += 0.1F;
+            if (this.c > 0) {
+                this.a += 0.1F;
             } else {
-                this.openness -= 0.1F;
+                this.a -= 0.1F;
             }
 
-            if (this.openness > 1.0F) {
-                this.openness = 1.0F;
+            if (this.a > 1.0F) {
+                this.a = 1.0F;
             }
 
             float f2 = 0.5F;
 
-            if (this.openness < 0.5F && f1 >= 0.5F) {
+            if (this.a < 0.5F && f1 >= 0.5F) {
+            // Paper start
+            */
                 d0 = (double) i + 0.5D;
                 double d2 = (double) k + 0.5D;
 
+            MCUtil.scheduleTask(10, () -> {
                 this.level.playSound((Player) null, d0, (double) j + 0.5D, d2, SoundEvents.ENDER_CHEST_CLOSE, SoundSource.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
-            }
+            }, "Chest Sounds");
 
             if (this.openness < 0.0F) {
                 this.openness = 0.0F;
@@ -84,11 +107,13 @@ public class EnderChestBlockEntity extends BlockEntity implements TickableBlockE
     public void startOpen() {
         ++this.openCount;
         this.level.blockEvent(this.worldPosition, Blocks.ENDER_CHEST, 1, this.openCount);
+        doOpenLogic(); // Paper
     }
 
     public void stopOpen() {
         --this.openCount;
         this.level.blockEvent(this.worldPosition, Blocks.ENDER_CHEST, 1, this.openCount);
+        doCloseLogic(); // Paper
     }
 
     public boolean stillValid(Player entityhuman) {
diff --git a/src/main/java/net/minecraft/world/level/block/state/StateHolder.java b/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
index 60ce75c7f94c995d3753c40bc8d1ec09b4d37b1a..ac10fb9cd4701f0f6477a86bec73cb5ac6496725 100644
--- a/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
+++ b/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
@@ -84,6 +84,7 @@ public abstract class StateHolder<O, S> {
         return Collections.unmodifiableCollection(this.values.keySet());
     }
 
+    public <T extends Comparable<T>> boolean contains(Property<T> iblockstate) { return this.hasProperty(iblockstate); } // Paper - OBFHELPER
     public <T extends Comparable<T>> boolean hasProperty(Property<T> property) {
         return this.values.containsKey(property);
     }