From 545ab0f26894e6ea8c6cf5ece6405eba6b5a8f05 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 26 Mar 2020 10:22:44 -0400 Subject: [PATCH] Move bed-specific code to BedBlockEntityTranslator.java --- .../entity/BedBlockEntityTranslator.java | 41 +++++++++++++++++++ .../geysermc/connector/utils/ChunkUtils.java | 29 ++----------- 2 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/block/entity/BedBlockEntityTranslator.java diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/BedBlockEntityTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/BedBlockEntityTranslator.java new file mode 100644 index 000000000..42865918b --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/block/entity/BedBlockEntityTranslator.java @@ -0,0 +1,41 @@ +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.nukkitx.math.vector.Vector3i; +import com.nukkitx.nbt.CompoundTagBuilder; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.block.BlockTranslator; +import org.geysermc.connector.utils.BlockEntityUtils; + +import java.util.concurrent.TimeUnit; + +public class BedBlockEntityTranslator { + + public static void checkForBedColor(GeyserSession session, BlockState blockState, Vector3i position) { + byte bedcolor = BlockTranslator.getBedColor(blockState); + // If Bed Color is not -1 then it is indeed a bed with a color. + if (bedcolor > -1) { + Position pos = new Position(position.getX(), position.getY(), position.getZ()); + com.nukkitx.nbt.tag.CompoundTag finalbedTag = getBedTag(bedcolor, pos); + // 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, finalbedTag, pos), + 500, + TimeUnit.MILLISECONDS + ); + } + } + + public static com.nukkitx.nbt.tag.CompoundTag getBedTag(byte bedcolor, Position pos) { + CompoundTagBuilder tagBuilder = CompoundTagBuilder.builder() + .intTag("x", pos.getX()) + .intTag("y", pos.getY()) + .intTag("z", pos.getZ()) + .stringTag("id", "Bed"); + tagBuilder.byteTag("color", bedcolor); + return tagBuilder.buildRootTag(); + } + +} 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 662382637..0a3e38632 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java @@ -31,7 +31,6 @@ 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.opennbt.tag.builtin.CompoundTag; import com.nukkitx.math.vector.Vector3i; -import com.nukkitx.nbt.CompoundTagBuilder; import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket; import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket; @@ -45,8 +44,7 @@ import org.geysermc.connector.network.translators.block.entity.BlockEntityTransl import org.geysermc.connector.world.chunk.ChunkPosition; import org.geysermc.connector.network.translators.block.BlockTranslator; import org.geysermc.connector.world.chunk.ChunkSection; - -import java.util.concurrent.TimeUnit; +import org.geysermc.connector.network.translators.block.entity.BedBlockEntityTranslator; import static org.geysermc.connector.network.translators.block.BlockTranslator.BEDROCK_WATER_ID; @@ -77,7 +75,7 @@ public class ChunkUtils { chunkData.signs.put(blockState.getId(), TranslatorsInit.getBlockEntityTranslators().get("Sign").getDefaultBedrockTag("Sign", pos.getX(), pos.getY(), pos.getZ())); } else if (BlockTranslator.getBedColor(blockState) > -1) { Position pos = new ChunkPosition(column.getX(), column.getZ()).getBlock(x, (chunkY << 4) + y, z); - chunkData.beds.put(blockState.getId(), getBedTag(BlockTranslator.getBedColor(blockState), pos)); + chunkData.beds.put(blockState.getId(), BedBlockEntityTranslator.getBedTag(BlockTranslator.getBedColor(blockState), pos)); } else { section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id); } @@ -138,19 +136,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 - byte bedcolor = BlockTranslator.getBedColor(blockState); - // If Bed Color is not -1 then it is indeed a bed with a color. - if (bedcolor > -1) { - Position pos = new Position(position.getX(), position.getY(), position.getZ()); - com.nukkitx.nbt.tag.CompoundTag finalbedTag = getBedTag(bedcolor, pos); - // 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, finalbedTag, pos), - 500, - TimeUnit.MILLISECONDS - ); - } + BedBlockEntityTranslator.checkForBedColor(session, blockState, position); } @@ -186,13 +172,4 @@ public class ChunkUtils { public Int2ObjectMap signs = new Int2ObjectOpenHashMap<>(); public Int2ObjectMap beds = new Int2ObjectOpenHashMap<>(); } - public static com.nukkitx.nbt.tag.CompoundTag getBedTag(byte bedcolor, Position pos) { - CompoundTagBuilder tagBuilder = CompoundTagBuilder.builder() - .intTag("x", pos.getX()) - .intTag("y", pos.getY()) - .intTag("z", pos.getZ()) - .stringTag("id", "Bed"); - tagBuilder.byteTag("color", bedcolor); - return tagBuilder.buildRootTag(); - } }