diff --git a/src/main/java/cn/nukkit/level/GlobalBlockPalette.java b/src/main/java/cn/nukkit/level/GlobalBlockPalette.java index df6cc99a..5a46513f 100644 --- a/src/main/java/cn/nukkit/level/GlobalBlockPalette.java +++ b/src/main/java/cn/nukkit/level/GlobalBlockPalette.java @@ -1,18 +1,23 @@ package cn.nukkit.level; import cn.nukkit.Server; +import cn.nukkit.block.Block; import cn.nukkit.nbt.NBTIO; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.nbt.tag.ListTag; import com.google.common.io.ByteStreams; +import com.reider745.InnerCoreServer; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import lombok.extern.log4j.Log4j2; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteOrder; +import java.util.HashMap; import java.util.List; import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicInteger; @@ -23,11 +28,14 @@ public class GlobalBlockPalette { private static final Int2IntMap runtimeIdToLegacy = new Int2IntOpenHashMap(); private static final AtomicInteger runtimeIdAllocator = new AtomicInteger(0); + + private static final HashMap runtimeIds = new HashMap<>(); + static { legacyToRuntimeId.defaultReturnValue(-1); runtimeIdToLegacy.defaultReturnValue(-1); - ListTag tag; + /*ListTag tag; try (InputStream stream = Server.class.getClassLoader().getResourceAsStream("runtime_block_states.dat")) { if (stream == null) { throw new AssertionError("Unable to locate block state nbt"); @@ -52,22 +60,41 @@ public class GlobalBlockPalette { int legacyId = legacyState.getInt("id") << 6 | legacyState.getShort("val"); legacyToRuntimeId.put(legacyId, runtimeId); } - } + }*/ + try { + JSONArray jsonArray = new JSONArray(new String(ByteStreams.toByteArray(Server.class.getClassLoader().getResourceAsStream("network-id-dump.json")))); + jsonArray.forEach((element) -> { + JSONObject value = (JSONObject) element; + addRuntimeId(value.getInt("newId"), value.getInt("data"), value.getInt("dbgStateId")); + }); + }catch (Exception e){log.error(e.getMessage());} + + } + + public static void addRuntimeId(int id, int data, Integer runtimeId){ + runtimeIds.put(InnerCoreServer.hash(id, data, new int[] {}), runtimeId); } public static int getOrCreateRuntimeId(int id, int meta) { - int legacyId = id << 6 | meta; + Long key = InnerCoreServer.hash(id, meta, new int[] {}); + Integer runtimeId = runtimeIds.get(key); + if(runtimeId == null){ + runtimeId = runtimeIdAllocator.getAndIncrement(); + log.info("Creating new runtime ID for unknown block {}, {}", id, meta); + addRuntimeId(id, meta, runtimeId); + } + return runtimeId; + /*int legacyId = id << 6 | meta; int runtimeId = legacyToRuntimeId.get(legacyId); if (runtimeId == -1) { runtimeId = legacyToRuntimeId.get(id << 6); if (runtimeId == -1) { - log.info("Creating new runtime ID for unknown block {}", id); runtimeId = runtimeIdAllocator.getAndIncrement(); legacyToRuntimeId.put(id << 6, runtimeId); runtimeIdToLegacy.put(runtimeId, id << 6); } } - return runtimeId; + return runtimeId;*/ } public static int getOrCreateRuntimeId(int legacyId) throws NoSuchElementException { diff --git a/src/main/java/com/reider745/InnerCoreServer.java b/src/main/java/com/reider745/InnerCoreServer.java index 5d8d3202..3a6fe621 100644 --- a/src/main/java/com/reider745/InnerCoreServer.java +++ b/src/main/java/com/reider745/InnerCoreServer.java @@ -13,7 +13,7 @@ public class InnerCoreServer { } - private static long hash(int id, int data, int[] allStatesSorted) { + public static long hash(int id, int data, int[] allStatesSorted) { long hash = (long) id; hash = hash * 314159L + (long) data;