CavePVP-Stuff/cSpigot-master/spigot-server-Patches/0128-Optimize-block-by-id-lookups.patch

139 lines
4.4 KiB
Diff
Raw Normal View History

2023-05-01 20:59:40 +02:00
From 98a18d698994e30f791155c18131ebcd598772b2 Mon Sep 17 00:00:00 2001
From: Poweruser_rs <poweruser.rs@hotmail.com>
Date: Sun, 21 Feb 2016 15:06:52 +0100
Subject: [PATCH] Optimize block by id lookups
diff --git a/src/main/java/net/frozenorb/blocks/BlockAccessCache.java b/src/main/java/net/frozenorb/blocks/BlockAccessCache.java
new file mode 100644
index 000000000..3bfe6b1d2
--- /dev/null
+++ b/src/main/java/net/frozenorb/blocks/BlockAccessCache.java
@@ -0,0 +1,74 @@
+package net.frozenorb.blocks;
+
+import net.minecraft.server.Block;
+
+public class BlockAccessCache {
+
+ private BlockCacheEntry[] array;
+
+ public BlockAccessCache() {
+ this.array = new BlockCacheEntry[4096];
+ for(int i = 0; i < this.array.length; i++) {
+ this.array[i] = new PendingBlockEntry(this);
+ }
+ }
+
+ public Block getById(int id) {
+ if(id >= 0 && id < this.array.length) {
+ return this.array[id].getById(id);
+ } else if(id >= this.array.length) {
+ BlockCacheEntry[] arr = new BlockCacheEntry[this.array.length * 2];
+ System.arraycopy(this.array, 0, arr, 0, this.array.length);
+ for(int i = this.array.length; i < arr.length; i++) {
+ arr[i] = new PendingBlockEntry(this);
+ }
+ this.array = arr;
+ return this.getById(id);
+ }
+ return (Block) Block.REGISTRY_BLOCKS.a(id);
+ }
+
+ private interface BlockCacheEntry {
+ public Block getById(int id);
+ }
+
+ private class FinalBlockEntry implements BlockCacheEntry {
+
+ private Block block;
+
+ public FinalBlockEntry(Block block) {
+ this.block = block;
+ }
+
+ @Override
+ public Block getById(int id) {
+ return block;
+ }
+ }
+
+ private class PendingBlockEntry implements BlockCacheEntry {
+
+ private BlockAccessCache cache;
+
+ public PendingBlockEntry(BlockAccessCache cache) {
+ this.cache = cache;
+ }
+
+ @Override
+ public Block getById(int id) {
+ Object obj = Block.REGISTRY_BLOCKS.getByIdWithoutDefaulting(id);
+ if(obj != null) {
+ Block block = (Block) obj;
+ this.cache.updateCacheEntry(id, block);
+ return block;
+ }
+ return (Block) Block.REGISTRY_BLOCKS.getDefaultBlock();
+ }
+ }
+
+ private void updateCacheEntry(int id, Block block) {
+ if(id >= 0 && id < this.array.length) {
+ this.array[id] = new FinalBlockEntry(block);
+ }
+ }
+}
diff --git a/src/main/java/net/minecraft/server/Block.java b/src/main/java/net/minecraft/server/Block.java
index 24e84d627..d7a941538 100644
--- a/src/main/java/net/minecraft/server/Block.java
+++ b/src/main/java/net/minecraft/server/Block.java
@@ -4,9 +4,15 @@ import java.util.Iterator;
import java.util.List;
import java.util.Random;
+import net.frozenorb.blocks.BlockAccessCache; // Poweruser
+
public class Block {
- public static final RegistryMaterials REGISTRY = new RegistryBlocks("air");
+ // Poweruser start
+ public static final RegistryBlocks REGISTRY_BLOCKS = new RegistryBlocks("air");
+ public static final RegistryMaterials REGISTRY = REGISTRY_BLOCKS;
+ private static final BlockAccessCache blockCache = new BlockAccessCache();
+ // Poweruser end
private CreativeModeTab creativeTab;
protected String d;
public static final StepSound e = new StepSound("stone", 1.0F, 1.0F);
@@ -49,7 +55,7 @@ public class Block {
}
public static Block getById(int i) {
- return (Block) REGISTRY.a(i);
+ return blockCache.getById(i); // Poweruser
}
public static Block a(Item item) {
diff --git a/src/main/java/net/minecraft/server/RegistryBlocks.java b/src/main/java/net/minecraft/server/RegistryBlocks.java
index 43f926919..f9891be17 100644
--- a/src/main/java/net/minecraft/server/RegistryBlocks.java
+++ b/src/main/java/net/minecraft/server/RegistryBlocks.java
@@ -32,4 +32,14 @@ public class RegistryBlocks extends RegistryMaterials {
public Object get(Object object) {
return this.get((String) object);
}
+
+ // Poweruser start
+ public Object getDefaultBlock() {
+ return this.e;
+ }
+
+ public Object getByIdWithoutDefaulting(int i) {
+ return super.a(i);
+ }
+ // Poweruser end
}
--
2.13.3