From 08eb99fa9492b455e8286c804b3d31383b50ad2a Mon Sep 17 00:00:00 2001 From: OnlyBMan <27742182+OnlyBMan@users.noreply.github.com> Date: Tue, 14 Apr 2020 18:33:55 -0400 Subject: [PATCH] Implement skulls and heads --- .../translators/block/BlockTranslator.java | 180 ++++++++++++++++++ .../entity/SkullBlockEntityTranslator.java | 49 +++++ .../java/world/JavaChunkDataTranslator.java | 4 + .../geysermc/connector/utils/ChunkUtils.java | 8 + 4 files changed, 241 insertions(+) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/block/entity/SkullBlockEntityTranslator.java diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockTranslator.java index 1ffaa3bd7..14a821c0d 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/block/BlockTranslator.java @@ -67,6 +67,8 @@ public class BlockTranslator { private static final Map JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>(); private static final Object2ByteMap BED_COLORS = new Object2ByteOpenHashMap<>(); + private static final Object2ByteMap SKULL_VARIANTS = new Object2ByteOpenHashMap<>(); + private static final Object2ByteMap SKULL_ROTATIONS = new Object2ByteOpenHashMap<>(); public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap(); public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap(); @@ -147,6 +149,170 @@ public class BlockTranslator { JAVA_ID_TO_BLOCK_ENTITY_MAP.put(javaBlockState, javaId); } + int lastIndex = javaId.length() -1; + byte rotation = 0; + if (javaId.contains("skeleton_skull[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 0); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("skeleton_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 0); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + + if (javaId.contains("wither_skeleton_skull[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 1); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("wither_skeleton_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 1); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + + if (javaId.contains("zombie_head[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 2); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("zombie_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 2); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + + if (javaId.contains("player_head[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 3); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("player_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 3); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + + if (javaId.contains("creeper_head[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 4); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("creeper_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 4); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + + if (javaId.contains("dragon_head[")) { + if (javaId.indexOf("=") == lastIndex - 2) { + rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex)); + } + else { + rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex)); + } + SKULL_VARIANTS.put(javaBlockState, (byte) 5); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + else if (javaId.contains("dragon_wall")) { + if (javaId.contains("north")) { + rotation = 0; + } + else if (javaId.contains("east")) { + rotation = 4; + } + else if (javaId.contains("south")) { + rotation = 8; + } + else if(javaId.contains("west")) { + rotation = 12; + } + SKULL_VARIANTS.put(javaBlockState, (byte) 5); + SKULL_ROTATIONS.put(javaBlockState, rotation); + } + // If the Java ID is bed, signal that it needs a tag to show color // The color is in the namespace ID in Java Edition but it's a tag in Bedrock. JsonNode bedColor = entry.getValue().get("bed_color"); @@ -268,6 +434,20 @@ public class BlockTranslator { return -1; } + public static byte getSkullVariant(BlockState state) { + if (SKULL_VARIANTS.containsKey(state)) { + return SKULL_VARIANTS.getByte(state); + } + return 0; + } + + public static byte getSkullRotation(BlockState state) { + if (SKULL_ROTATIONS.containsKey(state)) { + return SKULL_ROTATIONS.getByte(state); + } + return 0; + } + public static BlockState getJavaWaterloggedState(int bedrockId) { return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/SkullBlockEntityTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/SkullBlockEntityTranslator.java new file mode 100644 index 000000000..e4ace6e35 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/SkullBlockEntityTranslator.java @@ -0,0 +1,49 @@ +package org.geysermc.connector.network.translators.block.entity; + +import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.data.game.world.block.BlockState; +import com.github.steveice10.mc.protocol.data.message.Message; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.nukkitx.math.vector.Vector3i; +import com.nukkitx.nbt.CompoundTagBuilder; +import com.nukkitx.nbt.tag.StringTag; +import com.nukkitx.nbt.tag.Tag; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.block.BlockTranslator; +import org.geysermc.connector.utils.BlockEntityUtils; +import org.geysermc.connector.utils.MessageUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class SkullBlockEntityTranslator { + + public static void checkForSkullVariant(GeyserSession session, BlockState blockState, Vector3i position) { + byte skullVariant = BlockTranslator.getSkullVariant(blockState); + byte rotation = BlockTranslator.getSkullRotation(blockState); + // If Bed Color is not -1 then it is indeed a bed with a color. + if (skullVariant > 0) { + Position pos = new Position(position.getX(), position.getY(), position.getZ()); + com.nukkitx.nbt.tag.CompoundTag finalSkullTag = getSkullTag(skullVariant, pos, rotation); + // Delay needed, otherwise newly placed beds will not get their color + // Delay is not needed for beds already placed on login + session.getConnector().getGeneralThreadPool().schedule(() -> + BlockEntityUtils.updateBlockEntity(session, finalSkullTag, pos), + 500, + TimeUnit.MILLISECONDS + ); + } + } + + public static com.nukkitx.nbt.tag.CompoundTag getSkullTag(byte skullvariant, Position pos, byte rotation) { + CompoundTagBuilder tagBuilder = CompoundTagBuilder.builder() + .intTag("x", pos.getX()) + .intTag("y", pos.getY()) + .intTag("z", pos.getZ()) + .stringTag("id", "Skull") + .floatTag("Rotation", rotation * 22.5f); + tagBuilder.byteTag("SkullType", skullvariant); + return tagBuilder.buildRootTag(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaChunkDataTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaChunkDataTranslator.java index 4f6d1bc6b..9309ca758 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaChunkDataTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaChunkDataTranslator.java @@ -120,9 +120,13 @@ public class JavaChunkDataTranslator extends PacketTranslator blockEntityEntry: chunkData.beds.entrySet()) { ChunkUtils.updateBlock(session, blockEntityEntry.getValue(), blockEntityEntry.getKey()); } + for (Map.Entry blockEntityEntry: chunkData.skulls.entrySet()) { + ChunkUtils.updateBlock(session, blockEntityEntry.getValue(), blockEntityEntry.getKey()); + } chunkData.signs.clear(); chunkData.gateways.clear(); chunkData.beds.clear(); + chunkData.skulls.clear(); } catch (Exception ex) { ex.printStackTrace(); } diff --git a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java index c6851770c..737dfd738 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java @@ -42,6 +42,7 @@ import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.block.entity.BlockEntityTranslator; +import org.geysermc.connector.network.translators.block.entity.SkullBlockEntityTranslator; import org.geysermc.connector.world.chunk.ChunkPosition; import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.block.BlockTranslator; @@ -86,6 +87,11 @@ public class ChunkUtils { // Beds need to be updated separately to add the bed color tag // Previously this was done by matching block state but this resulted in only one bed per color+orientation showing chunkData.beds.put(pos, blockState); + } else if (BlockTranslator.getSkullVariant(blockState) > 0) { + Position pos = new ChunkPosition(column.getX(), column.getZ()).getBlock(x, (chunkY << 4) + y, z); + // Beds need to be updated separately to add the bed color tag + // Previously this was done by matching block state but this resulted in only one bed per color+orientation showing + chunkData.skulls.put(pos, blockState); } else { section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id); } @@ -160,6 +166,7 @@ public class ChunkUtils { // Since Java stores bed colors as part of the namespaced ID and Bedrock stores it as a tag // This is the only place I could find that interacts with the Java block state and block updates BedBlockEntityTranslator.checkForBedColor(session, blockState, position); + SkullBlockEntityTranslator.checkForSkullVariant(session, blockState, position); } public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) { @@ -194,5 +201,6 @@ public class ChunkUtils { public Object2IntMap signs = new Object2IntOpenHashMap<>(); public Object2IntMap gateways = new Object2IntOpenHashMap<>(); public Map beds = new HashMap<>(); + public Map skulls = new HashMap<>(); } }