From 02b2ed1db45d3ed520968e71f4d5483bfcc11b6a Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Sat, 11 May 2024 19:30:44 -0400 Subject: [PATCH 1/3] Use UpdateSubChunkBlocksPacket --- .../geyser/session/cache/WorldCache.java | 6 + .../JavaSectionBlocksUpdateTranslator.java | 173 +++++++++++++++++- 2 files changed, 178 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/session/cache/WorldCache.java b/core/src/main/java/org/geysermc/geyser/session/cache/WorldCache.java index c8412660837..fadd78cbefc 100644 --- a/core/src/main/java/org/geysermc/geyser/session/cache/WorldCache.java +++ b/core/src/main/java/org/geysermc/geyser/session/cache/WorldCache.java @@ -168,6 +168,12 @@ public void updateServerCorrectBlockState(Vector3i position, int blockState) { ChunkUtils.updateBlock(session, blockState, position); } + public void removePrediction(Vector3i position) { + if (!this.unverifiedPredictions.isEmpty()) { + this.unverifiedPredictions.removeInt(position); + } + } + public void endPredictionsUpTo(int sequence) { if (this.unverifiedPredictions.isEmpty()) { return; diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java index a52bb33b0ed..6ebc446f821 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java @@ -25,19 +25,190 @@ package org.geysermc.geyser.translator.protocol.java.level; +import org.cloudburstmc.math.vector.Vector3i; +import org.cloudburstmc.protocol.bedrock.data.definitions.BlockDefinition; +import org.cloudburstmc.protocol.bedrock.packet.UpdateBlockPacket; +import org.cloudburstmc.protocol.bedrock.packet.UpdateSubChunkBlocksPacket; +import org.geysermc.erosion.util.BlockPositionIterator; +import org.geysermc.geyser.entity.type.ItemFrameEntity; +import org.geysermc.geyser.level.block.BlockStateValues; +import org.geysermc.geyser.registry.BlockRegistries; +import org.geysermc.geyser.session.cache.SkullCache; +import org.geysermc.geyser.translator.level.block.entity.BedrockOnlyBlockEntity; +import org.geysermc.geyser.util.BlockEntityUtils; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockChangeEntry; import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level.ClientboundSectionBlocksUpdatePacket; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.Translator; +import java.util.BitSet; + +import static org.geysermc.geyser.level.block.BlockStateValues.JAVA_AIR_ID; + @Translator(packet = ClientboundSectionBlocksUpdatePacket.class) public class JavaSectionBlocksUpdateTranslator extends PacketTranslator { + private static final int NEIGHBORS_NETWORK_FLAG = (1 << UpdateBlockPacket.Flag.NEIGHBORS.ordinal()) | (1 << UpdateBlockPacket.Flag.NETWORK.ordinal()); + private static final int NETWORK_FLAG = (1 << UpdateBlockPacket.Flag.NETWORK.ordinal()); @Override public void translate(GeyserSession session, ClientboundSectionBlocksUpdatePacket packet) { + // Send normal block updates if not many changes + if (packet.getEntries().length < 32) { + for (BlockChangeEntry entry : packet.getEntries()) { + session.getWorldCache().updateServerCorrectBlockState(entry.getPosition(), entry.getBlock()); + } + return; + } + + UpdateSubChunkBlocksPacket subChunkBlocksPacket = new UpdateSubChunkBlocksPacket(); + subChunkBlocksPacket.setChunkX(packet.getChunkX()); + subChunkBlocksPacket.setChunkY(packet.getChunkY()); + subChunkBlocksPacket.setChunkZ(packet.getChunkZ()); + + // If the entire section is updated, this might be a legacy non-full chunk update + // which can contain thousands of unchanged blocks + if (packet.getEntries().length == 4096) { + // hack - bedrock might ignore the block updates if the chunk was still loading. + // sending an UpdateBlockPacket seems to force it + BlockChangeEntry firstEntry = packet.getEntries()[0]; + UpdateBlockPacket blockPacket = new UpdateBlockPacket(); + blockPacket.setBlockPosition(firstEntry.getPosition()); + blockPacket.setDefinition(session.getBlockMappings().getBedrockBlock(firstEntry.getBlock())); + blockPacket.setDataLayer(0); + session.sendUpstreamPacket(blockPacket); + + // Filter out unchanged blocks + Vector3i offset = Vector3i.from(packet.getChunkX() << 4, packet.getChunkY() << 4, packet.getChunkZ() << 4); + BlockPositionIterator blockIter = BlockPositionIterator.fromMinMax( + offset.getX(), offset.getY(), offset.getZ(), + offset.getX() + 15, offset.getY() + 15, offset.getZ() + 15 + ); + + int[] sectionBlocks = session.getGeyser().getWorldManager().getBlocksAt(session, blockIter); + BitSet waterlogged = BlockRegistries.WATERLOGGED.get(); + for (BlockChangeEntry entry : packet.getEntries()) { + Vector3i pos = entry.getPosition().sub(offset); + int index = pos.getZ() + pos.getX() * 16 + pos.getY() * 256; + int oldBlockState = sectionBlocks[index]; + if (oldBlockState != entry.getBlock()) { + // Avoid sending unnecessary waterlogged updates + boolean updateWaterlogged = waterlogged.get(oldBlockState) != waterlogged.get(entry.getBlock()); + applyEntry(session, entry, subChunkBlocksPacket, updateWaterlogged); + } + } + } else { + for (BlockChangeEntry entry : packet.getEntries()) { + applyEntry(session, entry, subChunkBlocksPacket, true); + } + } + + session.sendUpstreamPacket(subChunkBlocksPacket); + + // Post block update for (BlockChangeEntry entry : packet.getEntries()) { - session.getWorldCache().updateServerCorrectBlockState(entry.getPosition(), entry.getBlock()); + session.getWorldCache().removePrediction(entry.getPosition()); + BlockStateValues.getLecternBookStates().handleBlockChange(session, entry.getBlock(), entry.getPosition()); + + // Iterates through all Bedrock-only block entity translators and determines if a manual block entity packet + // needs to be sent + for (BedrockOnlyBlockEntity bedrockOnlyBlockEntity : BlockEntityUtils.BEDROCK_ONLY_BLOCK_ENTITIES) { + if (bedrockOnlyBlockEntity.isBlock(entry.getBlock())) { + // Flower pots are block entities only in Bedrock and are not updated anywhere else like note blocks + bedrockOnlyBlockEntity.updateBlock(session, entry.getBlock(), entry.getPosition()); + break; //No block will be a part of two classes + } + } + } + } + + // Modified version of ChunkUtils#updateBlockClientSide + private static void applyEntry(GeyserSession session, BlockChangeEntry entry, UpdateSubChunkBlocksPacket subChunkBlocksPacket, boolean updateWaterlogged) { + Vector3i position = entry.getPosition(); + int blockState = entry.getBlock(); + + session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState); + + // Checks for item frames so they aren't tripped up and removed + ItemFrameEntity itemFrameEntity = ItemFrameEntity.getItemFrameEntity(session, position); + if (itemFrameEntity != null) { + if (blockState == JAVA_AIR_ID) { // Item frame is still present and no block overrides that; refresh it + itemFrameEntity.updateBlock(true); + // Still update the chunk cache with the new block if updateBlock is called + return; + } + // Otherwise, let's still store our reference to the item frame, but let the new block take precedence for now + } + + BlockDefinition definition = session.getBlockMappings().getBedrockBlock(blockState); + + int skullVariant = BlockStateValues.getSkullVariant(blockState); + if (skullVariant == -1) { + // Skull is gone + session.getSkullCache().removeSkull(position); + } else if (skullVariant == 3) { + // The changed block was a player skull so check if a custom block was defined for this skull + SkullCache.Skull skull = session.getSkullCache().updateSkull(position, blockState); + if (skull != null && skull.getBlockDefinition() != null) { + definition = skull.getBlockDefinition(); + } + } + + // Prevent moving_piston from being placed + // It's used for extending piston heads, but it isn't needed on Bedrock and causes pistons to flicker + if (!BlockStateValues.isMovingPiston(blockState)) { + subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( + position, + definition, + NEIGHBORS_NETWORK_FLAG, + -1, + org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + )); + + if (updateWaterlogged) { + BlockDefinition waterDefinition = BlockRegistries.WATERLOGGED.get().get(blockState) ? + session.getBlockMappings().getBedrockWater() : session.getBlockMappings().getBedrockAir(); + subChunkBlocksPacket.getExtraBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( + position, + waterDefinition, + 0, + -1, + org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + )); + } + } + + // Extended collision boxes for custom blocks + if (!session.getBlockMappings().getExtendedCollisionBoxes().isEmpty()) { + int aboveBlock = session.getGeyser().getWorldManager().getBlockAt(session, position.getX(), position.getY() + 1, position.getZ()); + BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes().get(blockState); + int belowBlock = session.getGeyser().getWorldManager().getBlockAt(session, position.getX(), position.getY() - 1, position.getZ()); + BlockDefinition belowBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes().get(belowBlock); + if (belowBedrockExtendedCollisionDefinition != null && blockState == BlockStateValues.JAVA_AIR_ID) { + subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( + position, + belowBedrockExtendedCollisionDefinition, + NETWORK_FLAG, + -1, + org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + )); + } else if (aboveBedrockExtendedCollisionDefinition != null && aboveBlock == BlockStateValues.JAVA_AIR_ID) { + subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( + position.add(0, 1, 0), + aboveBedrockExtendedCollisionDefinition, + NETWORK_FLAG, + -1, + org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + )); + } else if (aboveBlock == BlockStateValues.JAVA_AIR_ID) { + subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( + position.add(0, 1, 0), + session.getBlockMappings().getBedrockAir(), + NETWORK_FLAG, + -1, + org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + )); + } } } } From 1c851b2a6dfded9d712e557e5f56a0010cbeacd6 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Sat, 11 May 2024 20:57:32 -0400 Subject: [PATCH 2/3] Use static import for MessageType --- .../java/level/JavaSectionBlocksUpdateTranslator.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java index 6ebc446f821..d97598181b5 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java @@ -44,6 +44,7 @@ import java.util.BitSet; +import static org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType; import static org.geysermc.geyser.level.block.BlockStateValues.JAVA_AIR_ID; @Translator(packet = ClientboundSectionBlocksUpdatePacket.class) @@ -162,7 +163,7 @@ private static void applyEntry(GeyserSession session, BlockChangeEntry entry, Up definition, NEIGHBORS_NETWORK_FLAG, -1, - org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + MessageType.NONE )); if (updateWaterlogged) { @@ -173,7 +174,7 @@ private static void applyEntry(GeyserSession session, BlockChangeEntry entry, Up waterDefinition, 0, -1, - org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + MessageType.NONE )); } } @@ -190,7 +191,7 @@ private static void applyEntry(GeyserSession session, BlockChangeEntry entry, Up belowBedrockExtendedCollisionDefinition, NETWORK_FLAG, -1, - org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + MessageType.NONE )); } else if (aboveBedrockExtendedCollisionDefinition != null && aboveBlock == BlockStateValues.JAVA_AIR_ID) { subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( @@ -198,7 +199,7 @@ private static void applyEntry(GeyserSession session, BlockChangeEntry entry, Up aboveBedrockExtendedCollisionDefinition, NETWORK_FLAG, -1, - org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + MessageType.NONE )); } else if (aboveBlock == BlockStateValues.JAVA_AIR_ID) { subChunkBlocksPacket.getStandardBlocks().add(new org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry( @@ -206,7 +207,7 @@ private static void applyEntry(GeyserSession session, BlockChangeEntry entry, Up session.getBlockMappings().getBedrockAir(), NETWORK_FLAG, -1, - org.cloudburstmc.protocol.bedrock.data.BlockChangeEntry.MessageType.NONE + MessageType.NONE )); } } From 62e7d438196261f71b24fc31ed69091a3faea10b Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Sun, 12 May 2024 03:53:23 -0400 Subject: [PATCH 3/3] Add check for chunk cache Temporary. Will need to find another fix :( --- .../protocol/java/level/JavaSectionBlocksUpdateTranslator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java index d97598181b5..eb25cbd9cda 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSectionBlocksUpdateTranslator.java @@ -69,7 +69,7 @@ public void translate(GeyserSession session, ClientboundSectionBlocksUpdatePacke // If the entire section is updated, this might be a legacy non-full chunk update // which can contain thousands of unchanged blocks - if (packet.getEntries().length == 4096) { + if (packet.getEntries().length == 4096 && !session.getGeyser().getWorldManager().hasOwnChunkCache()) { // hack - bedrock might ignore the block updates if the chunk was still loading. // sending an UpdateBlockPacket seems to force it BlockChangeEntry firstEntry = packet.getEntries()[0];