2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
package org.geysermc.connector.network.translators.world.block;
|
2019-09-16 00:04:54 +00:00
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2019-09-16 00:04:54 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
|
2020-04-23 06:01:33 +00:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2019-12-31 00:14:38 +00:00
|
|
|
import com.nukkitx.nbt.CompoundTagBuilder;
|
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
|
|
|
import com.nukkitx.nbt.stream.NBTInputStream;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
|
|
|
import com.nukkitx.nbt.tag.ListTag;
|
2020-05-02 20:44:05 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.*;
|
2020-03-18 21:10:28 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
2020-02-16 19:25:37 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.entity.BlockEntity;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2020-04-21 05:32:32 +00:00
|
|
|
import org.reflections.Reflections;
|
2019-09-16 00:04:54 +00:00
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.*;
|
2019-12-21 05:05:20 +00:00
|
|
|
|
2019-09-16 00:04:54 +00:00
|
|
|
public class BlockTranslator {
|
2019-12-31 00:14:38 +00:00
|
|
|
public static final ListTag<CompoundTag> BLOCKS;
|
|
|
|
public static final BlockState AIR = new BlockState(0);
|
2020-02-09 22:06:22 +00:00
|
|
|
public static final int BEDROCK_WATER_ID;
|
2019-09-16 00:04:54 +00:00
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
private static final Int2IntMap JAVA_TO_BEDROCK_BLOCK_MAP = new Int2IntOpenHashMap();
|
|
|
|
private static final Int2ObjectMap<BlockState> BEDROCK_TO_JAVA_BLOCK_MAP = new Int2ObjectOpenHashMap<>();
|
2020-04-23 06:01:33 +00:00
|
|
|
private static final BiMap<String, BlockState> JAVA_ID_BLOCK_MAP = HashBiMap.create();
|
2020-02-09 22:06:22 +00:00
|
|
|
private static final IntSet WATERLOGGED = new IntOpenHashSet();
|
2020-05-02 20:44:05 +00:00
|
|
|
private static final Object2IntMap<CompoundTag> ITEM_FRAMES = new Object2IntOpenHashMap<>();
|
2019-12-31 00:14:38 +00:00
|
|
|
|
2020-04-08 00:06:20 +00:00
|
|
|
// Bedrock carpet ID, used in LlamaEntity.java for decoration
|
|
|
|
public static final int CARPET = 171;
|
2020-04-07 23:57:34 +00:00
|
|
|
|
2020-03-06 02:00:14 +00:00
|
|
|
private static final Map<BlockState, String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>();
|
2020-04-02 23:09:09 +00:00
|
|
|
|
2020-03-21 20:14:09 +00:00
|
|
|
public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
|
2020-03-21 21:37:55 +00:00
|
|
|
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
|
2020-03-21 20:14:09 +00:00
|
|
|
public static final Int2ObjectMap<String> JAVA_RUNTIME_ID_TO_TOOL_TYPE = new Int2ObjectOpenHashMap<>();
|
|
|
|
|
|
|
|
// For block breaking animation math
|
2020-04-02 23:09:09 +00:00
|
|
|
public static final IntSet JAVA_RUNTIME_WOOL_IDS = new IntOpenHashSet();
|
2020-03-21 20:14:09 +00:00
|
|
|
public static final int JAVA_RUNTIME_COBWEB_ID;
|
2020-03-06 02:00:14 +00:00
|
|
|
|
2019-12-31 23:24:54 +00:00
|
|
|
private static final int BLOCK_STATE_VERSION = 17760256;
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
static {
|
|
|
|
/* Load block palette */
|
|
|
|
InputStream stream = Toolbox.getResource("bedrock/runtime_block_states.dat");
|
|
|
|
|
|
|
|
ListTag<CompoundTag> blocksTag;
|
|
|
|
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
|
|
|
|
blocksTag = (ListTag<CompoundTag>) nbtInputStream.readTag();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to get blocks from runtime block states", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<CompoundTag, CompoundTag> blockStateMap = new HashMap<>();
|
|
|
|
|
|
|
|
for (CompoundTag tag : blocksTag.getValue()) {
|
2020-02-14 23:57:28 +00:00
|
|
|
if (blockStateMap.putIfAbsent(tag.getCompound("block"), tag) != null) {
|
2019-12-31 00:14:38 +00:00
|
|
|
throw new AssertionError("Duplicate block states in Bedrock palette");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = Toolbox.getResource("mappings/blocks.json");
|
|
|
|
JsonNode blocks;
|
|
|
|
try {
|
|
|
|
blocks = Toolbox.JSON_MAPPER.readTree(stream);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Java block mappings", e);
|
|
|
|
}
|
2020-03-18 21:10:28 +00:00
|
|
|
Object2IntMap<CompoundTag> addedStatesMap = new Object2IntOpenHashMap<>();
|
|
|
|
addedStatesMap.defaultReturnValue(-1);
|
2019-12-31 00:14:38 +00:00
|
|
|
List<CompoundTag> paletteList = new ArrayList<>();
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
Reflections ref = new Reflections("org.geysermc.connector.network.translators.world.block.entity");
|
2020-04-21 05:32:32 +00:00
|
|
|
ref.getTypesAnnotatedWith(BlockEntity.class);
|
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
int waterRuntimeId = -1;
|
2019-12-31 00:14:38 +00:00
|
|
|
int javaRuntimeId = -1;
|
|
|
|
int bedrockRuntimeId = 0;
|
2020-03-21 20:14:09 +00:00
|
|
|
int cobwebRuntimeId = -1;
|
2019-12-31 00:14:38 +00:00
|
|
|
Iterator<Map.Entry<String, JsonNode>> blocksIterator = blocks.fields();
|
|
|
|
while (blocksIterator.hasNext()) {
|
|
|
|
javaRuntimeId++;
|
|
|
|
Map.Entry<String, JsonNode> entry = blocksIterator.next();
|
|
|
|
String javaId = entry.getKey();
|
2020-02-13 07:29:44 +00:00
|
|
|
BlockState javaBlockState = new BlockState(javaRuntimeId);
|
2019-12-31 00:14:38 +00:00
|
|
|
CompoundTag blockTag = buildBedrockState(entry.getValue());
|
|
|
|
|
2020-03-21 20:14:09 +00:00
|
|
|
// TODO fix this, (no block should have a null hardness)
|
|
|
|
JsonNode hardnessNode = entry.getValue().get("block_hardness");
|
|
|
|
if (hardnessNode != null) {
|
|
|
|
JAVA_RUNTIME_ID_TO_HARDNESS.put(javaRuntimeId, hardnessNode.doubleValue());
|
|
|
|
}
|
|
|
|
|
2020-03-21 21:37:55 +00:00
|
|
|
JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND.put(javaRuntimeId, entry.getValue().get("can_break_with_hand").booleanValue());
|
2020-03-21 20:14:09 +00:00
|
|
|
|
|
|
|
JsonNode toolTypeNode = entry.getValue().get("tool_type");
|
|
|
|
if (toolTypeNode != null) {
|
|
|
|
JAVA_RUNTIME_ID_TO_TOOL_TYPE.put(javaRuntimeId, toolTypeNode.textValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (javaId.contains("wool")) {
|
|
|
|
JAVA_RUNTIME_WOOL_IDS.add(javaRuntimeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (javaId.contains("cobweb")) {
|
|
|
|
cobwebRuntimeId = javaRuntimeId;
|
|
|
|
}
|
|
|
|
|
2020-02-13 07:29:44 +00:00
|
|
|
JAVA_ID_BLOCK_MAP.put(javaId, javaBlockState);
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
// Used for adding all "special" Java block states to block state map
|
|
|
|
String identifier;
|
|
|
|
String bedrock_identifer = entry.getValue().get("bedrock_identifier").asText();
|
|
|
|
for (Class<?> clazz : ref.getTypesAnnotatedWith(BlockEntity.class)) {
|
|
|
|
identifier = clazz.getAnnotation(BlockEntity.class).regex();
|
|
|
|
// Endswith, or else the block bedrock gets picked up for bed
|
|
|
|
if (bedrock_identifer.endsWith(identifier) && !identifier.equals("")) {
|
|
|
|
JAVA_ID_TO_BLOCK_ENTITY_MAP.put(javaBlockState, clazz.getAnnotation(BlockEntity.class).name());
|
|
|
|
break;
|
|
|
|
}
|
2020-04-14 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
BlockStateValues.storeBlockStateValues(entry, javaBlockState);
|
2020-03-26 02:03:46 +00:00
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
if ("minecraft:water[level=0]".equals(javaId)) {
|
|
|
|
waterRuntimeId = bedrockRuntimeId;
|
|
|
|
}
|
2020-03-27 23:23:26 +00:00
|
|
|
boolean waterlogged = entry.getKey().contains("waterlogged=true")
|
|
|
|
|| javaId.contains("minecraft:bubble_column") || javaId.contains("minecraft:kelp") || javaId.contains("seagrass");
|
2020-02-09 22:06:22 +00:00
|
|
|
|
|
|
|
if (waterlogged) {
|
2020-02-13 07:29:44 +00:00
|
|
|
BEDROCK_TO_JAVA_BLOCK_MAP.putIfAbsent(bedrockRuntimeId | 1 << 31, javaBlockState);
|
2020-02-09 22:06:22 +00:00
|
|
|
WATERLOGGED.add(javaRuntimeId);
|
|
|
|
} else {
|
2020-02-13 07:29:44 +00:00
|
|
|
BEDROCK_TO_JAVA_BLOCK_MAP.putIfAbsent(bedrockRuntimeId, javaBlockState);
|
2020-02-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
CompoundTag runtimeTag = blockStateMap.remove(blockTag);
|
|
|
|
if (runtimeTag != null) {
|
|
|
|
addedStatesMap.put(blockTag, bedrockRuntimeId);
|
|
|
|
paletteList.add(runtimeTag);
|
|
|
|
} else {
|
2020-04-21 05:32:32 +00:00
|
|
|
int duplicateRuntimeId = addedStatesMap.getOrDefault(blockTag, -1);
|
2019-12-31 00:14:38 +00:00
|
|
|
if (duplicateRuntimeId == -1) {
|
2020-02-16 19:25:37 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().debug("Mapping " + javaId + " was not found for bedrock edition!");
|
2019-12-31 00:14:38 +00:00
|
|
|
} else {
|
|
|
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, duplicateRuntimeId);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
JAVA_TO_BEDROCK_BLOCK_MAP.put(javaRuntimeId, bedrockRuntimeId);
|
|
|
|
|
|
|
|
bedrockRuntimeId++;
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:14:09 +00:00
|
|
|
if (cobwebRuntimeId == -1) {
|
|
|
|
throw new AssertionError("Unable to find cobwebs in palette");
|
|
|
|
}
|
|
|
|
JAVA_RUNTIME_COBWEB_ID = cobwebRuntimeId;
|
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
if (waterRuntimeId == -1) {
|
|
|
|
throw new AssertionError("Unable to find water in palette");
|
|
|
|
}
|
|
|
|
BEDROCK_WATER_ID = waterRuntimeId;
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
paletteList.addAll(blockStateMap.values()); // Add any missing mappings that could crash the client
|
|
|
|
|
2020-05-02 20:44:05 +00:00
|
|
|
// Loop around again to find all item frame runtime IDs
|
|
|
|
int frameRuntimeId = 0;
|
|
|
|
for (CompoundTag tag : paletteList) {
|
|
|
|
CompoundTag blockTag = tag.getCompound("block");
|
|
|
|
if (blockTag.getString("name").equals("minecraft:frame")) {
|
|
|
|
ITEM_FRAMES.put(tag, frameRuntimeId);
|
|
|
|
}
|
|
|
|
frameRuntimeId++;
|
|
|
|
}
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
BLOCKS = new ListTag<>("", CompoundTag.class, paletteList);
|
|
|
|
}
|
|
|
|
|
|
|
|
private BlockTranslator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void init() {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
private static CompoundTag buildBedrockState(JsonNode node) {
|
|
|
|
CompoundTagBuilder tagBuilder = CompoundTag.builder();
|
2019-12-31 23:24:54 +00:00
|
|
|
tagBuilder.stringTag("name", node.get("bedrock_identifier").textValue())
|
|
|
|
.intTag("version", BlockTranslator.BLOCK_STATE_VERSION);
|
|
|
|
|
|
|
|
CompoundTagBuilder statesBuilder = CompoundTag.builder();
|
2019-12-31 00:14:38 +00:00
|
|
|
|
|
|
|
// 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:
|
2019-12-31 23:24:54 +00:00
|
|
|
statesBuilder.booleanTag(stateEntry.getKey(), stateValue.booleanValue());
|
2019-12-31 00:14:38 +00:00
|
|
|
continue;
|
|
|
|
case STRING:
|
2019-12-31 23:24:54 +00:00
|
|
|
statesBuilder.stringTag(stateEntry.getKey(), stateValue.textValue());
|
2019-12-31 00:14:38 +00:00
|
|
|
continue;
|
|
|
|
case NUMBER:
|
2019-12-31 23:24:54 +00:00
|
|
|
statesBuilder.intTag(stateEntry.getKey(), stateValue.intValue());
|
2019-12-31 00:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 23:24:54 +00:00
|
|
|
return tagBuilder.tag(statesBuilder.build("states")).build("block");
|
2019-12-31 00:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int getBedrockBlockId(BlockState state) {
|
|
|
|
return JAVA_TO_BEDROCK_BLOCK_MAP.get(state.getId());
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:45:59 +00:00
|
|
|
public static int getBedrockBlockId(int javaId) {
|
|
|
|
return JAVA_TO_BEDROCK_BLOCK_MAP.get(javaId);
|
|
|
|
}
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
public static BlockState getJavaBlockState(int bedrockId) {
|
|
|
|
return BEDROCK_TO_JAVA_BLOCK_MAP.get(bedrockId);
|
|
|
|
}
|
|
|
|
|
2020-05-02 20:44:05 +00:00
|
|
|
public static int getItemFrame(CompoundTag tag) {
|
|
|
|
return ITEM_FRAMES.getOrDefault(tag, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isItemFrame(int bedrockBlockRuntimeId) {
|
|
|
|
return ITEM_FRAMES.values().contains(bedrockBlockRuntimeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getBlockStateVersion() {
|
|
|
|
return BLOCK_STATE_VERSION;
|
|
|
|
}
|
|
|
|
|
2020-02-13 07:29:44 +00:00
|
|
|
public static BlockState getJavaBlockState(String javaId) {
|
|
|
|
return JAVA_ID_BLOCK_MAP.get(javaId);
|
|
|
|
}
|
|
|
|
|
2020-03-06 02:00:14 +00:00
|
|
|
public static String getBlockEntityString(BlockState javaId) {
|
|
|
|
return JAVA_ID_TO_BLOCK_ENTITY_MAP.get(javaId);
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
public static boolean isWaterlogged(BlockState state) {
|
|
|
|
return WATERLOGGED.contains(state.getId());
|
2019-09-16 00:04:54 +00:00
|
|
|
}
|
2019-12-21 05:05:20 +00:00
|
|
|
|
2020-04-23 06:01:33 +00:00
|
|
|
public static BiMap<String, BlockState> getJavaIdBlockMap() {
|
|
|
|
return JAVA_ID_BLOCK_MAP;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
public static BlockState getJavaWaterloggedState(int bedrockId) {
|
|
|
|
return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId);
|
2019-12-21 05:05:20 +00:00
|
|
|
}
|
2019-09-16 00:04:54 +00:00
|
|
|
}
|