Skip to content

Commit

Permalink
Added basic chest opening and closing
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialKris committed Dec 27, 2024
1 parent 25f4834 commit fbe8a93
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
31 changes: 31 additions & 0 deletions pumpkin-protocol/src/client/play/c_block_event.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
17 changes: 15 additions & 2 deletions pumpkin/src/block/blocks/chest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit fbe8a93

Please sign in to comment.