2019-05-05 13:32:20 +02:00
|
|
|
From 9c821668fb448e021731f6a8975fc7aa7ba0d6b3 Mon Sep 17 00:00:00 2001
|
2018-08-23 22:44:41 +02:00
|
|
|
From: miclebrick <miclebrick@outlook.com>
|
|
|
|
Date: Thu, 23 Aug 2018 11:45:32 -0400
|
2018-08-25 06:20:58 +02:00
|
|
|
Subject: [PATCH] Optimize CraftBlockData Creation
|
2018-08-23 22:44:41 +02:00
|
|
|
|
2018-08-25 06:20:58 +02:00
|
|
|
Avoids a hashmap lookup by cacheing a reference to the CraftBlockData
|
|
|
|
and cloning it when one is needed.
|
2018-08-23 22:44:41 +02:00
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/BlockData.java b/src/main/java/net/minecraft/server/BlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
new file mode 100644
|
|
|
|
index 000000000..1b226a77e
|
|
|
|
--- /dev/null
|
2018-08-23 22:44:41 +02:00
|
|
|
+++ b/src/main/java/net/minecraft/server/BlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
@@ -0,0 +1,25 @@
|
|
|
|
+package net.minecraft.server;
|
|
|
|
+
|
|
|
|
+import com.google.common.collect.ImmutableMap;
|
2018-08-23 22:44:41 +02:00
|
|
|
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
|
2019-05-05 10:33:44 +02:00
|
|
|
+
|
|
|
|
+public class BlockData extends BlockDataAbstract<Block, IBlockData> implements IBlockData {
|
|
|
|
+
|
|
|
|
+ public BlockData(Block block, ImmutableMap<IBlockState<?>, Comparable<?>> immutablemap) {
|
|
|
|
+ super(block, immutablemap);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Block getBlock() {
|
|
|
|
+ return (Block) this.e_;
|
|
|
|
+ }
|
2018-08-23 22:44:41 +02: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
|
2019-05-05 10:33:44 +02:00
|
|
|
+}
|
2018-08-23 22:44:41 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/IBlockData.java b/src/main/java/net/minecraft/server/IBlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
index e8ae4b5e4..5413fe5b1 100644
|
2018-08-23 22:44:41 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/IBlockData.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/IBlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
@@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.mojang.datafixers.Dynamic;
|
|
|
|
import com.mojang.datafixers.types.DynamicOps;
|
|
|
|
import com.mojang.datafixers.util.Pair;
|
|
|
|
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
|
|
|
|
+
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
@@ -38,6 +40,16 @@ public class IBlockData extends BlockDataAbstract<Block, IBlockData> implements
|
|
|
|
return (Block) this.a;
|
|
|
|
}
|
2018-08-23 22:44:41 +02:00
|
|
|
|
2019-05-05 10:33:44 +02: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
|
2018-08-23 22:44:41 +02:00
|
|
|
+
|
2019-05-05 10:33:44 +02:00
|
|
|
public Material getMaterial() {
|
|
|
|
return this.getBlock().l(this);
|
2018-08-23 22:44:41 +02:00
|
|
|
}
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
index c1666a9ba..3c29abd52 100644
|
2018-08-23 22:44:41 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
2019-05-05 10:33:44 +02:00
|
|
|
@@ -539,7 +539,17 @@ public class CraftBlockData implements BlockData {
|
2018-08-23 22:44:41 +02:00
|
|
|
return craft;
|
|
|
|
}
|
|
|
|
|
2018-08-25 06:20:58 +02: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 22:44:41 +02:00
|
|
|
public static CraftBlockData fromData(IBlockData data) {
|
|
|
|
+ return data.createCraftBlockData();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static CraftBlockData createData(IBlockData data) {
|
|
|
|
+ // Paper end
|
2018-08-25 06:20:58 +02:00
|
|
|
return MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 22:44:41 +02:00
|
|
|
--
|
2019-05-05 13:32:20 +02:00
|
|
|
2.21.0
|
2018-08-23 22:44:41 +02:00
|
|
|
|