Skip to content

Commit

Permalink
Fix block breaking range
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Dec 15, 2020
1 parent 02e7481 commit 35b8869
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ public void translate(InventoryTransactionPacket packet, GeyserSession session)
float diffZ = playerPosition.getZ() - packet.getBlockPosition().getZ();
if (((diffX * diffX) + (diffY * diffY) + (diffZ * diffZ)) >
(session.getGameMode().equals(GameMode.CREATIVE) ? CREATIVE_EYE_HEIGHT_PLACE_DISTANCE : SURVIVAL_EYE_HEIGHT_PLACE_DISTANCE)) {
restoreCorrectBlock(session, blockPos);
restoreCorrectBlock(session, blockPos, packet);
return;
}

// Vanilla check
if (!(session.getPlayerEntity().getPosition().sub(0, EntityType.PLAYER.getOffset(), 0)
.distanceSquared(packet.getBlockPosition().toFloat().add(0.5f, 0.5f, 0.5f)) < MAXIMUM_BLOCK_PLACING_DISTANCE)) {
// The client thinks that its blocks have been successfully placed. Restore the server's blocks instead.
restoreCorrectBlock(session, blockPos);
restoreCorrectBlock(session, blockPos, packet);
return;
}
/*
Expand Down Expand Up @@ -234,11 +234,16 @@ else if (packet.getItemInHand() != null && ItemRegistry.BUCKETS.contains(packet.
session.setLastBlockPlacedId(null);
session.setLastBlockPlacePosition(null);

// Same deal with block placing as above.
// Same deal with vanilla block placing as above.
// No idea what's going on with the Y coordinate here
if (!(session.getPlayerEntity().getPosition().sub(0, (EntityType.PLAYER.getOffset() - 1.5f), 0)
.distanceSquared(packet.getBlockPosition().toFloat().add(0.5f, 0.5f, 0.5f)) < MAXIMUM_BLOCK_DESTROYING_DISTANCE)) {
restoreCorrectBlock(session, packet.getBlockPosition());
playerPosition = session.getPlayerEntity().getPosition();
Vector3f floatBlockPosition = packet.getBlockPosition().toFloat();
diffX = playerPosition.getX() - (floatBlockPosition.getX() + 0.5f);
diffY = (playerPosition.getY() - EntityType.PLAYER.getOffset()) - (floatBlockPosition.getY() + 0.5f) + 1.5f;
diffZ = playerPosition.getZ() - (floatBlockPosition.getZ() + 0.5f);
float distanceSquared = diffX * diffX + diffY * diffY + diffZ * diffZ;
if (distanceSquared > MAXIMUM_BLOCK_DESTROYING_DISTANCE) {
restoreCorrectBlock(session, packet.getBlockPosition(), packet);
return;
}

Expand Down Expand Up @@ -329,7 +334,7 @@ else if (packet.getItemInHand() != null && ItemRegistry.BUCKETS.contains(packet.
* @param session the session of the Bedrock client
* @param blockPos the block position to restore
*/
private void restoreCorrectBlock(GeyserSession session, Vector3i blockPos) {
private void restoreCorrectBlock(GeyserSession session, Vector3i blockPos, InventoryTransactionPacket packet) {
int javaBlockState = session.getConnector().getWorldManager().getBlockAt(session, blockPos);
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0);
Expand All @@ -344,5 +349,12 @@ private void restoreCorrectBlock(GeyserSession session, Vector3i blockPos) {
updateWaterPacket.setRuntimeId(BlockTranslator.isWaterlogged(javaBlockState) ? BlockTranslator.BEDROCK_WATER_ID : BlockTranslator.BEDROCK_AIR_ID);
updateWaterPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
session.sendUpstreamPacket(updateWaterPacket);

// Reset the item in hand to prevent "missing" blocks
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(ContainerId.INVENTORY);
slotPacket.setSlot(packet.getHotbarSlot());
slotPacket.setItem(packet.getItemInHand());
session.sendUpstreamPacket(slotPacket);
}
}

0 comments on commit 35b8869

Please sign in to comment.