Переписал логику runtimeId
This commit is contained in:
@@ -1,18 +1,23 @@
|
|||||||
package cn.nukkit.level;
|
package cn.nukkit.level;
|
||||||
|
|
||||||
import cn.nukkit.Server;
|
import cn.nukkit.Server;
|
||||||
|
import cn.nukkit.block.Block;
|
||||||
import cn.nukkit.nbt.NBTIO;
|
import cn.nukkit.nbt.NBTIO;
|
||||||
import cn.nukkit.nbt.tag.CompoundTag;
|
import cn.nukkit.nbt.tag.CompoundTag;
|
||||||
import cn.nukkit.nbt.tag.ListTag;
|
import cn.nukkit.nbt.tag.ListTag;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
import com.reider745.InnerCoreServer;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
@@ -23,11 +28,14 @@ public class GlobalBlockPalette {
|
|||||||
private static final Int2IntMap runtimeIdToLegacy = new Int2IntOpenHashMap();
|
private static final Int2IntMap runtimeIdToLegacy = new Int2IntOpenHashMap();
|
||||||
private static final AtomicInteger runtimeIdAllocator = new AtomicInteger(0);
|
private static final AtomicInteger runtimeIdAllocator = new AtomicInteger(0);
|
||||||
|
|
||||||
|
|
||||||
|
private static final HashMap<Long, Integer> runtimeIds = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
legacyToRuntimeId.defaultReturnValue(-1);
|
legacyToRuntimeId.defaultReturnValue(-1);
|
||||||
runtimeIdToLegacy.defaultReturnValue(-1);
|
runtimeIdToLegacy.defaultReturnValue(-1);
|
||||||
|
|
||||||
ListTag<CompoundTag> tag;
|
/*ListTag<CompoundTag> tag;
|
||||||
try (InputStream stream = Server.class.getClassLoader().getResourceAsStream("runtime_block_states.dat")) {
|
try (InputStream stream = Server.class.getClassLoader().getResourceAsStream("runtime_block_states.dat")) {
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
throw new AssertionError("Unable to locate block state nbt");
|
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");
|
int legacyId = legacyState.getInt("id") << 6 | legacyState.getShort("val");
|
||||||
legacyToRuntimeId.put(legacyId, runtimeId);
|
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) {
|
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);
|
int runtimeId = legacyToRuntimeId.get(legacyId);
|
||||||
if (runtimeId == -1) {
|
if (runtimeId == -1) {
|
||||||
runtimeId = legacyToRuntimeId.get(id << 6);
|
runtimeId = legacyToRuntimeId.get(id << 6);
|
||||||
if (runtimeId == -1) {
|
if (runtimeId == -1) {
|
||||||
log.info("Creating new runtime ID for unknown block {}", id);
|
|
||||||
runtimeId = runtimeIdAllocator.getAndIncrement();
|
runtimeId = runtimeIdAllocator.getAndIncrement();
|
||||||
legacyToRuntimeId.put(id << 6, runtimeId);
|
legacyToRuntimeId.put(id << 6, runtimeId);
|
||||||
runtimeIdToLegacy.put(runtimeId, id << 6);
|
runtimeIdToLegacy.put(runtimeId, id << 6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return runtimeId;
|
return runtimeId;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getOrCreateRuntimeId(int legacyId) throws NoSuchElementException {
|
public static int getOrCreateRuntimeId(int legacyId) throws NoSuchElementException {
|
||||||
|
|||||||
@@ -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;
|
long hash = (long) id;
|
||||||
hash = hash * 314159L + (long) data;
|
hash = hash * 314159L + (long) data;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user