Skip to content

Commit

Permalink
Add Clientbound Acknowledge Block Change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Snowiiii committed Aug 20, 2024
1 parent 589d258 commit 5122097
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
16 changes: 16 additions & 0 deletions pumpkin-protocol/src/client/play/c_acknowledge_block.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod c_acknowledge_block;
mod c_actionbar;
mod c_block_destroy_stage;
mod c_block_update;
Expand Down Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/server/play/s_use_item_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
12 changes: 9 additions & 3 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
Expand All @@ -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());
Expand Down

0 comments on commit 5122097

Please sign in to comment.