2018-12-15 01:17:27 +00:00
From 682fb9d99363928b21ea5ae8596fe6352cf1386f Mon Sep 17 00:00:00 2001
2018-08-23 20:44:41 +00:00
From: miclebrick <miclebrick@outlook.com>
Date: Thu, 23 Aug 2018 11:45:32 -0400
2018-08-25 04:20:58 +00:00
Subject: [PATCH] Optimize CraftBlockData Creation
2018-08-23 20:44:41 +00:00
2018-08-25 04:20:58 +00:00
Avoids a hashmap lookup by cacheing a reference to the CraftBlockData
and cloning it when one is needed.
2018-08-23 20:44:41 +00:00
diff --git a/src/main/java/net/minecraft/server/BlockData.java b/src/main/java/net/minecraft/server/BlockData.java
2018-12-15 01:17:27 +00:00
index fbe181c1f..bd86cd2e5 100644
2018-08-23 20:44:41 +00:00
--- a/src/main/java/net/minecraft/server/BlockData.java
+++ b/src/main/java/net/minecraft/server/BlockData.java
@@ -1,6 +1,7 @@
package net.minecraft.server;
import com.google.common.collect.ImmutableMap;
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
public class BlockData extends BlockDataAbstract<Block, IBlockData> implements IBlockData {
2018-08-31 22:56:57 +00:00
public BlockData(Block block, ImmutableMap<IBlockState<?>, Comparable<?>> immutablemap) {
@@ -10,4 +11,14 @@ public class BlockData extends BlockDataAbstract<Block, IBlockData> implements I
2018-08-23 20:44:41 +00:00
public Block getBlock() {
2018-08-31 22:56:57 +00:00
return this.e_;
2018-08-23 20:44:41 +00:00
}
+
+ // Paper start - impl cached craft block data, lazy load to fix issue with loading at the wrong time
+ private CraftBlockData cachedCraftBlockData;
+
+ @Override
+ public CraftBlockData createCraftBlockData() {
+ if(cachedCraftBlockData == null) cachedCraftBlockData = CraftBlockData.createData(this);
+ return (CraftBlockData) cachedCraftBlockData.clone();
+ }
+ // Paper end
}
diff --git a/src/main/java/net/minecraft/server/IBlockData.java b/src/main/java/net/minecraft/server/IBlockData.java
2018-12-15 01:17:27 +00:00
index 1423419b9..2d855aae6 100644
2018-08-23 20:44:41 +00:00
--- a/src/main/java/net/minecraft/server/IBlockData.java
+++ b/src/main/java/net/minecraft/server/IBlockData.java
2018-08-31 22:56:57 +00:00
@@ -23,6 +23,8 @@ public interface IBlockData extends IBlockDataHolder<IBlockData> {
2018-08-23 20:44:41 +00:00
Block getBlock();
+ org.bukkit.craftbukkit.block.data.CraftBlockData createCraftBlockData(); // Paper - property for converting IBlockData to CraftBlockData, should only be used by CraftBlockData#fromData and should return a clone
+
default Material getMaterial() {
return this.getBlock().n(this);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
2018-12-15 01:17:27 +00:00
index d470e2b34..b9279d925 100644
2018-08-23 20:44:41 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
2018-08-26 18:11:49 +00:00
@@ -501,7 +501,17 @@ public class CraftBlockData implements BlockData {
2018-08-23 20:44:41 +00:00
return craft;
}
2018-08-25 04:20:58 +00:00
+ // 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);
+ }
2018-08-23 20:44:41 +00:00
public static CraftBlockData fromData(IBlockData data) {
+ return data.createCraftBlockData();
+ }
+
+ public static CraftBlockData createData(IBlockData data) {
+ // Paper end
2018-08-25 04:20:58 +00:00
return MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
}
}
2018-08-23 20:44:41 +00:00
--
2018-12-15 01:17:27 +00:00
2.20.0
2018-08-23 20:44:41 +00:00