2019-07-11 22:39:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2019-07-11 22:39:28 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-12-30 23:33:27 +00:00
|
|
|
import com.nukkitx.nbt.CompoundTagBuilder;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
|
|
|
import com.nukkitx.nbt.stream.NBTInputStream;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
|
|
|
import com.nukkitx.nbt.tag.ListTag;
|
2019-07-17 01:05:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
2019-12-30 23:33:27 +00:00
|
|
|
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;
|
2019-11-30 19:26:51 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2019-08-10 03:16:34 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2019-07-17 01:05:10 +00:00
|
|
|
import java.util.*;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
public class Toolbox {
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
public static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
|
|
|
|
|
2019-11-06 00:55:59 +00:00
|
|
|
public static final Collection<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
|
2019-12-30 23:33:27 +00:00
|
|
|
public static final ListTag<CompoundTag> BLOCKS;
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-11-30 19:26:51 +00:00
|
|
|
public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>();
|
|
|
|
public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>();
|
2019-12-30 23:33:27 +00:00
|
|
|
public static final Int2IntMap JAVA_TO_BEDROCK_BLOCK_MAP = new Int2IntOpenHashMap();
|
|
|
|
public static final Int2IntMap JAVA_TO_BEDROCK_LIQUID_MAP = new Int2IntOpenHashMap();
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
static {
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
/* Load block palette */
|
|
|
|
InputStream stream = getResource("bedrock/runtime_block_states.dat");
|
2019-08-10 03:16:34 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
ListTag<CompoundTag> blocksTag;
|
|
|
|
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
|
2019-11-02 20:50:04 +00:00
|
|
|
blocksTag = (ListTag<CompoundTag>) nbtInputStream.readTag();
|
2019-12-30 23:33:27 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to get blocks from runtime block states", e);
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
Map<CompoundTag, CompoundTag> blockStateMap = new HashMap<>();
|
|
|
|
|
|
|
|
for (CompoundTag tag : blocksTag.getValue()) {
|
|
|
|
if (blockStateMap.putIfAbsent(tag.getAsCompound("block"), tag) != null) {
|
|
|
|
throw new AssertionError("Duplicate block states in Bedrock palette");
|
|
|
|
}
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
stream = getResource("mappings/blocks.json");
|
|
|
|
JsonNode blocks;
|
2019-07-17 01:05:10 +00:00
|
|
|
try {
|
2019-12-30 23:33:27 +00:00
|
|
|
blocks = JSON_MAPPER.readTree(stream);
|
2019-07-17 01:05:10 +00:00
|
|
|
} catch (Exception e) {
|
2019-12-30 23:33:27 +00:00
|
|
|
throw new AssertionError("Unable to load Java block mappings", e);
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
2019-12-30 23:37:30 +00:00
|
|
|
TObjectIntMap<CompoundTag> addedStatesMap = new TObjectIntHashMap<>(512, 0.5f, -1);
|
2019-12-30 23:33:27 +00:00
|
|
|
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) {
|
2019-12-30 23:37:30 +00:00
|
|
|
addedStatesMap.put(blockTag, bedrockRuntimeId);
|
2019-12-30 23:33:27 +00:00
|
|
|
paletteList.add(runtimeTag);
|
|
|
|
} else {
|
2019-12-30 23:37:30 +00:00
|
|
|
int duplicateRuntimeId = addedStatesMap.get(blockTag);
|
2019-12-30 23:33:27 +00:00
|
|
|
if (duplicateRuntimeId == -1) {
|
|
|
|
GeyserLogger.DEFAULT.debug("Mapping " + javaId + " was not found for bedrock edition!");
|
|
|
|
} else {
|
2019-12-30 23:35:28 +00:00
|
|
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, duplicateRuntimeId);
|
2019-12-30 23:33:27 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, bedrockRuntimeId);
|
2019-07-17 01:05:10 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
bedrockRuntimeId++;
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
BLOCKS = new ListTag<>("", CompoundTag.class, paletteList);
|
|
|
|
|
|
|
|
/* Load item palette */
|
|
|
|
stream = getResource("bedrock/items.json");
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
TypeReference<List<JsonNode>> itemEntriesType = new TypeReference<List<JsonNode>>() {
|
|
|
|
};
|
|
|
|
|
|
|
|
List<JsonNode> itemEntries;
|
2019-07-29 22:20:48 +00:00
|
|
|
try {
|
2019-12-30 23:33:27 +00:00
|
|
|
itemEntries = JSON_MAPPER.readValue(stream, itemEntriesType);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Bedrock runtime item IDs", e);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
for (JsonNode entry : itemEntries) {
|
|
|
|
ITEMS.add(new StartGamePacket.ItemEntry(entry.get("name").textValue(), (short) entry.get("id").intValue()));
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
stream = getResource("mappings/items.json");
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
JsonNode items;
|
2019-08-07 23:08:48 +00:00
|
|
|
try {
|
2019-12-30 23:33:27 +00:00
|
|
|
items = JSON_MAPPER.readTree(stream);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Java runtime item IDs", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
int itemIndex = 0;
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> iterator = items.fields();
|
|
|
|
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++;
|
2019-08-07 23:08:48 +00:00
|
|
|
}
|
2019-12-30 23:33:27 +00:00
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
private static CompoundTag buildBedrockState(JsonNode node) {
|
|
|
|
CompoundTagBuilder tagBuilder = CompoundTag.builder();
|
|
|
|
tagBuilder.stringTag("name", node.get("bedrock_identifier").textValue());
|
|
|
|
|
|
|
|
// check for states
|
|
|
|
if (node.has("bedrock_states")) {
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> statesIterator = node.get("bedrock_states").fields();
|
|
|
|
|
|
|
|
while (statesIterator.hasNext()) {
|
|
|
|
Map.Entry<String, JsonNode> stateEntry = statesIterator.next();
|
|
|
|
JsonNode stateValue = stateEntry.getValue();
|
|
|
|
switch (stateValue.getNodeType()) {
|
|
|
|
case BOOLEAN:
|
|
|
|
tagBuilder.booleanTag(stateEntry.getKey(), stateValue.booleanValue());
|
|
|
|
continue;
|
|
|
|
case STRING:
|
|
|
|
tagBuilder.stringTag(stateEntry.getKey(), stateValue.textValue());
|
|
|
|
continue;
|
|
|
|
case NUMBER:
|
|
|
|
tagBuilder.intTag(stateEntry.getKey(), stateValue.intValue());
|
2019-12-21 05:05:20 +00:00
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 23:33:27 +00:00
|
|
|
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
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
}
|