mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Start working on direct block mapping.
This will not work
This commit is contained in:
parent
8ae1c4d70a
commit
b96ef99beb
1 changed files with 124 additions and 78 deletions
|
@ -1,16 +1,21 @@
|
||||||
package org.geysermc.connector.utils;
|
package org.geysermc.connector.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.nukkitx.nbt.CompoundTagBuilder;
|
||||||
import com.nukkitx.nbt.NbtUtils;
|
import com.nukkitx.nbt.NbtUtils;
|
||||||
import com.nukkitx.nbt.stream.NBTInputStream;
|
import com.nukkitx.nbt.stream.NBTInputStream;
|
||||||
import com.nukkitx.nbt.tag.CompoundTag;
|
import com.nukkitx.nbt.tag.CompoundTag;
|
||||||
import com.nukkitx.nbt.tag.ListTag;
|
import com.nukkitx.nbt.tag.ListTag;
|
||||||
import com.nukkitx.nbt.tag.Tag;
|
|
||||||
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
||||||
|
import gnu.trove.map.TObjectIntMap;
|
||||||
|
import gnu.trove.map.hash.TObjectIntHashMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import org.geysermc.connector.GeyserConnector;
|
|
||||||
import org.geysermc.connector.console.GeyserLogger;
|
import org.geysermc.connector.console.GeyserLogger;
|
||||||
import org.geysermc.connector.network.translators.block.BlockEntry;
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
||||||
import org.geysermc.connector.network.translators.item.ItemEntry;
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
||||||
|
@ -20,105 +25,146 @@ import java.util.*;
|
||||||
|
|
||||||
public class Toolbox {
|
public class Toolbox {
|
||||||
|
|
||||||
|
public static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
|
||||||
|
|
||||||
public static final Collection<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
|
public static final Collection<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
|
||||||
public static ListTag<CompoundTag> BLOCKS;
|
public static final ListTag<CompoundTag> BLOCKS;
|
||||||
|
|
||||||
public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>();
|
public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>();
|
||||||
public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>();
|
public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>();
|
||||||
|
public static final Int2IntMap JAVA_TO_BEDROCK_BLOCK_MAP = new Int2IntOpenHashMap();
|
||||||
|
public static final Int2IntMap JAVA_TO_BEDROCK_LIQUID_MAP = new Int2IntOpenHashMap();
|
||||||
|
|
||||||
public static void init() {
|
static {
|
||||||
InputStream stream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/runtime_block_states.dat");
|
|
||||||
if (stream == null) {
|
/* Load block palette */
|
||||||
throw new AssertionError("Unable to find bedrock/runtime_block_states.dat");
|
InputStream stream = getResource("bedrock/runtime_block_states.dat");
|
||||||
}
|
|
||||||
|
|
||||||
ListTag<CompoundTag> blocksTag;
|
ListTag<CompoundTag> blocksTag;
|
||||||
|
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
|
||||||
NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream);
|
|
||||||
try {
|
|
||||||
blocksTag = (ListTag<CompoundTag>) nbtInputStream.readTag();
|
blocksTag = (ListTag<CompoundTag>) nbtInputStream.readTag();
|
||||||
nbtInputStream.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
GeyserLogger.DEFAULT.warning("Failed to get blocks from runtime block states, please report this error!");
|
|
||||||
throw new AssertionError(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
BLOCKS = blocksTag;
|
|
||||||
InputStream stream2 = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/items.json");
|
|
||||||
if (stream2 == null) {
|
|
||||||
throw new AssertionError("Items Table not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectMapper startGameItemMapper = new ObjectMapper();
|
|
||||||
List<Map> startGameItems = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
startGameItems = startGameItemMapper.readValue(stream2, ArrayList.class);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
throw new AssertionError("Unable to get blocks from runtime block states", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Map entry : startGameItems) {
|
Map<CompoundTag, CompoundTag> blockStateMap = new HashMap<>();
|
||||||
ITEMS.add(new StartGamePacket.ItemEntry((String) entry.get("name"), (short) ((int) entry.get("id"))));
|
|
||||||
|
for (CompoundTag tag : blocksTag.getValue()) {
|
||||||
|
if (blockStateMap.putIfAbsent(tag.getAsCompound("block"), tag) != null) {
|
||||||
|
throw new AssertionError("Duplicate block states in Bedrock palette");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream itemStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/items.json");
|
stream = getResource("mappings/blocks.json");
|
||||||
ObjectMapper itemMapper = new ObjectMapper();
|
JsonNode blocks;
|
||||||
Map<String, Map<String, Object>> items = new HashMap<>();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
items = itemMapper.readValue(itemStream, LinkedHashMap.class);
|
blocks = JSON_MAPPER.readTree(stream);
|
||||||
} catch (Exception ex) {
|
} catch (Exception e) {
|
||||||
ex.printStackTrace();
|
throw new AssertionError("Unable to load Java block mappings", e);
|
||||||
|
}
|
||||||
|
TObjectIntMap<CompoundTag> stateRuntimeMap = new TObjectIntHashMap<>(512, 0.5f, -1);
|
||||||
|
List<CompoundTag> paletteList = new ArrayList<>();
|
||||||
|
|
||||||
|
int javaRuntimeId = -1;
|
||||||
|
int bedrockRuntimeId = 0;
|
||||||
|
Iterator<Map.Entry<String, JsonNode>> blocksIterator = blocks.fields();
|
||||||
|
while (blocksIterator.hasNext()) {
|
||||||
|
javaRuntimeId++;
|
||||||
|
Map.Entry<String, JsonNode> entry = blocksIterator.next();
|
||||||
|
String javaId = entry.getKey();
|
||||||
|
CompoundTag blockTag = buildBedrockState(entry.getValue());
|
||||||
|
|
||||||
|
CompoundTag runtimeTag = blockStateMap.remove(blockTag);
|
||||||
|
if (runtimeTag != null) {
|
||||||
|
stateRuntimeMap.put(blockTag, javaRuntimeId);
|
||||||
|
paletteList.add(runtimeTag);
|
||||||
|
} else {
|
||||||
|
int duplicateRuntimeId = stateRuntimeMap.get(blockTag);
|
||||||
|
if (duplicateRuntimeId == -1) {
|
||||||
|
GeyserLogger.DEFAULT.debug("Mapping " + javaId + " was not found for bedrock edition!");
|
||||||
|
} else {
|
||||||
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, bedrockRuntimeId);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, bedrockRuntimeId);
|
||||||
|
|
||||||
|
bedrockRuntimeId++;
|
||||||
|
}
|
||||||
|
|
||||||
|
BLOCKS = new ListTag<>("", CompoundTag.class, paletteList);
|
||||||
|
|
||||||
|
/* Load item palette */
|
||||||
|
stream = getResource("bedrock/items.json");
|
||||||
|
|
||||||
|
TypeReference<List<JsonNode>> itemEntriesType = new TypeReference<List<JsonNode>>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
List<JsonNode> itemEntries;
|
||||||
|
try {
|
||||||
|
itemEntries = JSON_MAPPER.readValue(stream, itemEntriesType);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new AssertionError("Unable to load Bedrock runtime item IDs", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (JsonNode entry : itemEntries) {
|
||||||
|
ITEMS.add(new StartGamePacket.ItemEntry(entry.get("name").textValue(), (short) entry.get("id").intValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
stream = getResource("mappings/items.json");
|
||||||
|
|
||||||
|
JsonNode items;
|
||||||
|
try {
|
||||||
|
items = JSON_MAPPER.readTree(stream);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new AssertionError("Unable to load Java runtime item IDs", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
int itemIndex = 0;
|
int itemIndex = 0;
|
||||||
for (Map.Entry<String, Map<String, Object>> itemEntry : items.entrySet()) {
|
Iterator<Map.Entry<String, JsonNode>> iterator = items.fields();
|
||||||
ITEM_ENTRIES.put(itemIndex, new ItemEntry(itemEntry.getKey(), itemIndex, (int) itemEntry.getValue().get("bedrock_id"), (int) itemEntry.getValue().get("bedrock_data")));
|
while (iterator.hasNext()) {
|
||||||
|
Map.Entry<String, JsonNode> entry = iterator.next();
|
||||||
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry(entry.getKey(), itemIndex,
|
||||||
|
entry.getValue().get("bedrock_id").intValue(), entry.getValue().get("bedrock_data").intValue()));
|
||||||
itemIndex++;
|
itemIndex++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
InputStream blockStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/blocks.json");
|
private static CompoundTag buildBedrockState(JsonNode node) {
|
||||||
ObjectMapper blockMapper = new ObjectMapper();
|
CompoundTagBuilder tagBuilder = CompoundTag.builder();
|
||||||
Map<String, Map<String, Object>> blocks = new HashMap<>();
|
tagBuilder.stringTag("name", node.get("bedrock_identifier").textValue());
|
||||||
|
|
||||||
try {
|
// check for states
|
||||||
blocks = blockMapper.readValue(blockStream, LinkedHashMap.class);
|
if (node.has("bedrock_states")) {
|
||||||
} catch (Exception ex) {
|
Iterator<Map.Entry<String, JsonNode>> statesIterator = node.get("bedrock_states").fields();
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
int javaIndex = -1;
|
while (statesIterator.hasNext()) {
|
||||||
javaLoop:
|
Map.Entry<String, JsonNode> stateEntry = statesIterator.next();
|
||||||
for (Map.Entry<String, Map<String, Object>> javaEntry : blocks.entrySet()) {
|
JsonNode stateValue = stateEntry.getValue();
|
||||||
javaIndex++;
|
switch (stateValue.getNodeType()) {
|
||||||
String wantedIdentifier = (String) javaEntry.getValue().get("bedrock_identifier");
|
case BOOLEAN:
|
||||||
Map<String, Object> wantedStates = (Map<String, Object>) javaEntry.getValue().get("bedrock_states");
|
tagBuilder.booleanTag(stateEntry.getKey(), stateValue.booleanValue());
|
||||||
|
continue;
|
||||||
int bedrockIndex = -1;
|
case STRING:
|
||||||
bedrockLoop:
|
tagBuilder.stringTag(stateEntry.getKey(), stateValue.textValue());
|
||||||
for (CompoundTag bedrockEntry : BLOCKS.getValue()) {
|
continue;
|
||||||
bedrockIndex++;
|
case NUMBER:
|
||||||
CompoundTag blockTag = bedrockEntry.getAsCompound("block");
|
tagBuilder.intTag(stateEntry.getKey(), stateValue.intValue());
|
||||||
if (blockTag.getAsString("name").equals(wantedIdentifier)) {
|
|
||||||
if (wantedStates != null) {
|
|
||||||
Map<String, Tag<?>> bedrockStates = blockTag.getAsCompound("states").getValue();
|
|
||||||
for (Map.Entry<String, Object> stateEntry : wantedStates.entrySet()) {
|
|
||||||
Tag<?> bedrockStateTag = bedrockStates.get(stateEntry.getKey());
|
|
||||||
if (bedrockStateTag == null)
|
|
||||||
continue bedrockLoop;
|
|
||||||
Object bedrockStateValue = bedrockStateTag.getValue();
|
|
||||||
if (bedrockStateValue instanceof Byte)
|
|
||||||
bedrockStateValue = ((Byte) bedrockStateValue).intValue();
|
|
||||||
if (!stateEntry.getValue().equals(bedrockStateValue))
|
|
||||||
continue bedrockLoop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex);
|
|
||||||
BLOCK_ENTRIES.put(javaIndex, blockEntry);
|
|
||||||
continue javaLoop;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GeyserLogger.DEFAULT.debug("Mapping " + javaEntry.getKey() + " was not found for bedrock edition!");
|
|
||||||
}
|
}
|
||||||
|
return tagBuilder.build("block");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static InputStream getResource(String resource) {
|
||||||
|
InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream(resource);
|
||||||
|
if (stream == null) {
|
||||||
|
throw new AssertionError("Unable to find resource: " + resource);
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
// no-op
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue