aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches-Unmapped/0272-Optimize-CraftBlockData-Creation.patch
blob: 727414b2747d0c0ae1e8310e1b8b9c13988722da (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/BlockBase.java b/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
index 10f4015b8c36e4e27cf7d0745ba70b8a9e60aff3..57857cc33603cf278de424b540a3d4a5943584c9 100644
--- a/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
+++ b/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
@@ -375,6 +375,14 @@ public abstract class BlockBase {
             this.o = blockbase_info.t;
             this.p = blockbase_info.u;
         }
+        // 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(getBlockData());
+            return (org.bukkit.craftbukkit.block.data.CraftBlockData) cachedCraftBlockData.clone();
+        }
+        // Paper end
 
         public void a() {
             if (!this.getBlock().o()) {
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 76fd1fea386d0e65c63c529dba772f01d9888407..ed88da727feddc319a650fb35710d16727f6dbd7 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -509,7 +509,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.REGISTRY_ID.iterator().forEachRemaining(IBlockData::createCraftBlockData);
+    }
     public static CraftBlockData fromData(IBlockData data) {
+        return data.createCraftBlockData();
+    }
+
+    public static CraftBlockData createData(IBlockData data) {
+        // Paper end
         return MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
     }