From 7a9fff1a64792a78ed8aed9106d8288be0b444a6 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 29 Apr 2020 17:01:56 +0100 Subject: [PATCH] Fix shulkers being upside down on load (#446) * Added shulker box direction handeling * Update mappings * Added missing Object2ByteMap * Cleaned javadoc * Moved shulker box direction conversion to the mappings generator --- .../translators/block/BlockStateValues.java | 33 ++++++++-- .../ShulkerBoxBlockEntityTranslator.java | 64 +++++++++++++++++++ connector/src/main/resources/mappings | 2 +- 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/block/entity/ShulkerBoxBlockEntityTranslator.java diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockStateValues.java b/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockStateValues.java index 25d6070f..76ee5238 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockStateValues.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockStateValues.java @@ -43,6 +43,7 @@ public class BlockStateValues { private static final Object2ByteMap BED_COLORS = new Object2ByteOpenHashMap<>(); private static final Object2ByteMap SKULL_VARIANTS = new Object2ByteOpenHashMap<>(); private static final Object2ByteMap SKULL_ROTATIONS = new Object2ByteOpenHashMap<>(); + private static final Object2ByteMap SHULKERBOX_DIRECTIONS = new Object2ByteOpenHashMap<>(); /** * Determines if the block state contains Bedrock block information @@ -71,13 +72,19 @@ public class BlockStateValues { if (skullRotation != null) { BlockStateValues.SKULL_ROTATIONS.put(javaBlockState, (byte) skullRotation.intValue()); } + + JsonNode shulkerDirection = entry.getValue().get("shulker_direction"); + if (shulkerDirection != null) { + BlockStateValues.SHULKERBOX_DIRECTIONS.put(javaBlockState, (byte) shulkerDirection.intValue()); + } } /** * Banner colors are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock. * This gives an integer color that Bedrock can use. + * * @param state BlockState of the block - * @return banner color integer or -1 if no color + * @return Banner color integer or -1 if no color */ public static int getBannerColor(BlockState state) { if (BANNER_COLORS.containsKey(state)) { @@ -89,8 +96,9 @@ public class BlockStateValues { /** * Bed colors are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock. * This gives a byte color that Bedrock can use - Bedrock needs a byte in the final tag. + * * @param state BlockState of the block - * @return bed color byte or -1 if no color + * @return Bed color byte or -1 if no color */ public static byte getBedColor(BlockState state) { if (BED_COLORS.containsKey(state)) { @@ -102,8 +110,9 @@ public class BlockStateValues { /** * Skull variations are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock. * This gives a byte variant ID that Bedrock can use. + * * @param state BlockState of the block - * @return skull variant byte or -1 if no variant + * @return Skull variant byte or -1 if no variant */ public static byte getSkullVariant(BlockState state) { if (SKULL_VARIANTS.containsKey(state)) { @@ -113,9 +122,11 @@ public class BlockStateValues { } /** + * Skull rotations are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock. + * This gives a byte rotation that Bedrock can use. * * @param state BlockState of the block - * @return skull rotation value or -1 if no value + * @return Skull rotation value or -1 if no value */ public static byte getSkullRotation(BlockState state) { if (SKULL_ROTATIONS.containsKey(state)) { @@ -124,4 +135,18 @@ public class BlockStateValues { return -1; } + + /** + * Shulker box directions are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock. + * This gives a byte direction that Bedrock can use. + * + * @param state BlockState of the block + * @return Shulker direction value or -1 if no value + */ + public static byte getShulkerBoxDirection(BlockState state) { + if (SHULKERBOX_DIRECTIONS.containsKey(state)) { + return SHULKERBOX_DIRECTIONS.getByte(state); + } + return -1; + } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/ShulkerBoxBlockEntityTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/ShulkerBoxBlockEntityTranslator.java new file mode 100644 index 00000000..3d409a09 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/ShulkerBoxBlockEntityTranslator.java @@ -0,0 +1,64 @@ +/* + * 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 + */ + +package org.geysermc.connector.network.translators.block.entity; + +import com.github.steveice10.mc.protocol.data.game.world.block.BlockState; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.nukkitx.nbt.CompoundTagBuilder; +import com.nukkitx.nbt.tag.ByteTag; +import com.nukkitx.nbt.tag.Tag; +import org.geysermc.connector.network.translators.block.BlockStateValues; + +import java.util.ArrayList; +import java.util.List; + +@BlockEntity(name = "ShulkerBox", delay = false, regex = "shulker_box") +public class ShulkerBoxBlockEntityTranslator extends BlockEntityTranslator { + + @Override + public List> translateTag(CompoundTag tag, BlockState blockState) { + List> tags = new ArrayList<>(); + + byte direction = BlockStateValues.getShulkerBoxDirection(blockState); + // Just in case... + if (direction == -1) direction = 1; + tags.add(new ByteTag("facing", direction)); + + return tags; + } + + @Override + public CompoundTag getDefaultJavaTag(String javaId, int x, int y, int z) { + return null; + } + + @Override + public com.nukkitx.nbt.tag.CompoundTag getDefaultBedrockTag(String bedrockId, int x, int y, int z) { + CompoundTagBuilder tagBuilder = getConstantBedrockTag(bedrockId, x, y, z).toBuilder(); + tagBuilder.byteTag("facing", (byte)1); + return tagBuilder.buildRootTag(); + } +} diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index 0af94e03..4e80d3b8 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit 0af94e03efaa7e21651d7cabcef05b1927795cf5 +Subproject commit 4e80d3b8e489b12a93b1b98b7bbaf8fa91a7974a