aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0252-Optimize-CraftBlockData-Creation.patch
blob: a7acf9bdedb98474a1d6c5976be283c83d043ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: miclebrick <miclebrick@outlook.com>
Date: Thu, 23 Aug 2018 11:45:32 -0400
Subject: [PATCH] Optimize CraftBlockData Creation

Avoids a hashmap lookup by cacheing a reference to the CraftBlockData
and cloning it when one is needed.

diff --git a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java
index a3f44e3a44b2ec21ef41f6d598b428448c847c5f..3c6b1b92fedf9986ebb835170c070ebd461f5d25 100644
--- a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java
+++ b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java
@@ -678,6 +678,14 @@ public abstract class BlockBehaviour {
             this.hasPostProcess = blockbase_info.hasPostProcess;
             this.emissiveRendering = blockbase_info.emissiveRendering;
         }
+        // Paper start - impl cached craft block data, lazy load to fix issue with loading at the wrong time
+        private org.bukkit.craftbukkit.block.data.CraftBlockData cachedCraftBlockData;
+
+        public org.bukkit.craftbukkit.block.data.CraftBlockData createCraftBlockData() {
+            if (cachedCraftBlockData == null) cachedCraftBlockData = org.bukkit.craftbukkit.block.data.CraftBlockData.createData(asState());
+            return (org.bukkit.craftbukkit.block.data.CraftBlockData) cachedCraftBlockData.clone();
+        }
+        // Paper end
 
         // Paper start
         protected boolean shapeExceedsCube = true;
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
index c6a9ce2a67591205cbeb436b5043e737331c3527..3594f432a25b580173e8577bf324be954f5eddd1 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -527,7 +527,17 @@ public class CraftBlockData implements BlockData {
         return craft;
     }
 
+    // Paper start - optimize creating BlockData to not need a map lookup
+    static {
+        // Initialize cached data for all IBlockData instances after registration
+        Block.BLOCK_STATE_REGISTRY.iterator().forEachRemaining(BlockState::createCraftBlockData);
+    }
     public static CraftBlockData fromData(BlockState data) {
+        return data.createCraftBlockData();
+    }
+
+    public static CraftBlockData createData(BlockState data) {
+        // Paper end
         return CraftBlockData.MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
     }