From 5122097d0d60b49cac09ac12e72d9facb15bc8fc Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Tue, 20 Aug 2024 17:33:03 +0200 Subject: [PATCH] Add Clientbound Acknowledge Block Change Acknowledges a user-initiated block change. After receiving this packet, the client will display the block state sent by the server instead of the one predicted by the client. --- .../src/client/play/c_acknowledge_block.rs | 16 ++++++++++++++++ pumpkin-protocol/src/client/play/mod.rs | 2 ++ .../src/server/play/s_use_item_on.rs | 2 +- pumpkin/src/client/player_packet.rs | 12 +++++++++--- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 pumpkin-protocol/src/client/play/c_acknowledge_block.rs diff --git a/pumpkin-protocol/src/client/play/c_acknowledge_block.rs b/pumpkin-protocol/src/client/play/c_acknowledge_block.rs new file mode 100644 index 000000000..1f463ce0b --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_acknowledge_block.rs @@ -0,0 +1,16 @@ +use pumpkin_macros::packet; +use serde::Serialize; + +use crate::VarInt; + +#[derive(Serialize)] +#[packet(0x05)] +pub struct CAcknowledgeBlockChange { + sequence_id: VarInt, +} + +impl CAcknowledgeBlockChange { + pub fn new(sequence_id: VarInt) -> Self { + Self { sequence_id } + } +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 38ccd947a..1a07061f6 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -1,3 +1,4 @@ +mod c_acknowledge_block; mod c_actionbar; mod c_block_destroy_stage; mod c_block_update; @@ -32,6 +33,7 @@ mod c_update_entity_rot; mod c_worldevent; mod player_action; +pub use c_acknowledge_block::*; pub use c_actionbar::*; pub use c_block_destroy_stage::*; pub use c_block_update::*; diff --git a/pumpkin-protocol/src/server/play/s_use_item_on.rs b/pumpkin-protocol/src/server/play/s_use_item_on.rs index b69227442..b25b761db 100644 --- a/pumpkin-protocol/src/server/play/s_use_item_on.rs +++ b/pumpkin-protocol/src/server/play/s_use_item_on.rs @@ -13,5 +13,5 @@ pub struct SUseItemOn { pub cursor_pos_y: f32, pub cursor_pos_z: f32, pub inside_block: bool, - pub sequene: VarInt, + pub sequence: VarInt, } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index a53fa586b..c6fefe0d3 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -10,9 +10,9 @@ use num_traits::FromPrimitive; use pumpkin_entity::EntityId; use pumpkin_protocol::{ client::play::{ - Animation, CBlockUpdate, CEntityAnimation, CEntityVelocity, CHeadRot, CHurtAnimation, - CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot, CWorldEvent, - FilterType, + Animation, CAcknowledgeBlockChange, CBlockUpdate, CEntityAnimation, CEntityVelocity, + CHeadRot, CHurtAnimation, CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot, + CUpdateEntityRot, CWorldEvent, FilterType, }, position::WorldPosition, server::play::{ @@ -319,6 +319,7 @@ impl Client { pub fn handle_player_action(&mut self, server: &mut Server, player_action: SPlayerAction) { match Status::from_i32(player_action.status.0).unwrap() { Status::StartedDigging => { + // TODO: do validation let player = self.player.as_mut().unwrap(); // TODO: Config if player.gamemode == GameMode::Creative { @@ -335,12 +336,15 @@ impl Client { player.current_block_destroy_stage = 0; } Status::FinishedDigging => { + // TODO: do validation let location = player_action.location; // Block break & block break sound // TODO: currently this is always dirt replace it server.broadcast_packet(self, &CWorldEvent::new(2001, &location, 11, false)); // AIR server.broadcast_packet(self, &CBlockUpdate::new(location, 0.into())); + // TODO: Send this every tick + self.send_packet(&CAcknowledgeBlockChange::new(player_action.sequence)); } Status::DropItemStack => { dbg!("todo"); @@ -358,6 +362,8 @@ impl Client { } pub fn handle_use_item_on(&mut self, server: &mut Server, use_item_on: SUseItemOn) { + self.send_packet(&CAcknowledgeBlockChange::new(use_item_on.sequence)); + let location = use_item_on.location; let face = BlockFace::from_i32(use_item_on.face.0).unwrap(); let location = WorldPosition(location.0 + face.to_offset());