From fbe8a9314c7f11d043b22d6476de38c19847da5b Mon Sep 17 00:00:00 2001 From: OfficialKris <37947442+OfficialKris@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:53:26 -0800 Subject: [PATCH] Added basic chest opening and closing --- .../src/client/play/c_block_event.rs | 31 +++++++++++++++++++ pumpkin-protocol/src/client/play/mod.rs | 2 ++ pumpkin/src/block/blocks/chest.rs | 17 ++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 pumpkin-protocol/src/client/play/c_block_event.rs diff --git a/pumpkin-protocol/src/client/play/c_block_event.rs b/pumpkin-protocol/src/client/play/c_block_event.rs new file mode 100644 index 00000000..c1b2e6e0 --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_block_event.rs @@ -0,0 +1,31 @@ +use pumpkin_core::math::position::WorldPosition; + +use pumpkin_macros::client_packet; +use serde::Serialize; + +use crate::VarInt; + +#[derive(Serialize)] +#[client_packet("play:block_event")] +pub struct CBlockAction<'a> { + location: &'a WorldPosition, + action_id: u8, + action_parameter: u8, + block_type: VarInt, +} + +impl<'a> CBlockAction<'a> { + pub fn new( + location: &'a WorldPosition, + action_id: u8, + action_parameter: u8, + block_type: VarInt, + ) -> Self { + Self { + location, + action_id, + action_parameter, + block_type, + } + } +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 79f7e3ea..68a7cc57 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -2,6 +2,7 @@ mod bossevent_action; mod c_acknowledge_block; mod c_actionbar; mod c_block_destroy_stage; +mod c_block_event; mod c_block_update; mod c_boss_event; mod c_center_chunk; @@ -72,6 +73,7 @@ pub use bossevent_action::*; pub use c_acknowledge_block::*; pub use c_actionbar::*; pub use c_block_destroy_stage::*; +pub use c_block_event::*; pub use c_block_update::*; pub use c_boss_event::*; pub use c_center_chunk::*; diff --git a/pumpkin/src/block/blocks/chest.rs b/pumpkin/src/block/blocks/chest.rs index 1600bb09..470c4610 100644 --- a/pumpkin/src/block/blocks/chest.rs +++ b/pumpkin/src/block/blocks/chest.rs @@ -2,6 +2,7 @@ use async_trait::async_trait; use pumpkin_core::math::position::WorldPosition; use pumpkin_inventory::{Chest, OpenContainer, WindowType}; use pumpkin_macros::{pumpkin_block, sound}; +use pumpkin_protocol::{client::play::CBlockAction, codec::var_int::VarInt}; use pumpkin_world::{block::block_registry::get_block, item::item_registry::Item}; use crate::{ @@ -34,11 +35,17 @@ impl PumpkinBlock for ChestBlock { BlockActionResult::Consume } - async fn on_close<'a>(&self, player: &Player, _location: WorldPosition, _server: &Server) { + async fn on_close<'a>(&self, player: &Player, location: WorldPosition, server: &Server) { player .world() - .play_block_sound(sound!("block.chest.close"), _location) + .play_block_sound(sound!("block.chest.close"), location) .await; + + if let Some(e) = get_block("minecraft:chest").cloned() { + server + .broadcast_packet_all(&CBlockAction::new(&location, 1, 0, VarInt(e.id.into()))) + .await; + } } } @@ -66,5 +73,11 @@ impl ChestBlock { } } player.open_container(server, WindowType::Generic9x3).await; + + if let Some(e) = get_block("minecraft:chest").cloned() { + server + .broadcast_packet_all(&CBlockAction::new(&location, 1, 1, VarInt(e.id.into()))) + .await; + } } }