Start working on direct block mapping.

This will not work
This commit is contained in:
SupremeMortal 2019-12-30 23:33:27 +00:00
parent 8ae1c4d70a
commit b96ef99beb
No known key found for this signature in database
GPG key ID: DDBB25F8EE4FA29A

View file

@ -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");
ObjectMapper blockMapper = new ObjectMapper();
Map<String, Map<String, Object>> blocks = new HashMap<>();
try {
blocks = blockMapper.readValue(blockStream, LinkedHashMap.class);
} catch (Exception ex) {
ex.printStackTrace();
} }
int javaIndex = -1; private static CompoundTag buildBedrockState(JsonNode node) {
javaLoop: CompoundTagBuilder tagBuilder = CompoundTag.builder();
for (Map.Entry<String, Map<String, Object>> javaEntry : blocks.entrySet()) { tagBuilder.stringTag("name", node.get("bedrock_identifier").textValue());
javaIndex++;
String wantedIdentifier = (String) javaEntry.getValue().get("bedrock_identifier");
Map<String, Object> wantedStates = (Map<String, Object>) javaEntry.getValue().get("bedrock_states");
int bedrockIndex = -1; // check for states
bedrockLoop: if (node.has("bedrock_states")) {
for (CompoundTag bedrockEntry : BLOCKS.getValue()) { Iterator<Map.Entry<String, JsonNode>> statesIterator = node.get("bedrock_states").fields();
bedrockIndex++;
CompoundTag blockTag = bedrockEntry.getAsCompound("block"); while (statesIterator.hasNext()) {
if (blockTag.getAsString("name").equals(wantedIdentifier)) { Map.Entry<String, JsonNode> stateEntry = statesIterator.next();
if (wantedStates != null) { JsonNode stateValue = stateEntry.getValue();
Map<String, Tag<?>> bedrockStates = blockTag.getAsCompound("states").getValue(); switch (stateValue.getNodeType()) {
for (Map.Entry<String, Object> stateEntry : wantedStates.entrySet()) { case BOOLEAN:
Tag<?> bedrockStateTag = bedrockStates.get(stateEntry.getKey()); tagBuilder.booleanTag(stateEntry.getKey(), stateValue.booleanValue());
if (bedrockStateTag == null) continue;
continue bedrockLoop; case STRING:
Object bedrockStateValue = bedrockStateTag.getValue(); tagBuilder.stringTag(stateEntry.getKey(), stateValue.textValue());
if (bedrockStateValue instanceof Byte) continue;
bedrockStateValue = ((Byte) bedrockStateValue).intValue(); case NUMBER:
if (!stateEntry.getValue().equals(bedrockStateValue)) tagBuilder.intTag(stateEntry.getKey(), stateValue.intValue());
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
}
}