Skip to content

Commit

Permalink
Added play chest action function
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialKris committed Dec 28, 2024
1 parent 3b89599 commit 15754e3
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions pumpkin/src/block/blocks/chest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use crate::{
server::Server,
};

#[derive(PartialEq)]
pub enum ChestState {
IsOpened,
IsClosed,
}

#[pumpkin_block("minecraft:chest")]
pub struct ChestBlock;

Expand All @@ -28,10 +34,6 @@ impl PumpkinBlock for ChestBlock {
) {
self.open_chest_block(block, player, _location, server)
.await;
player
.world()
.play_block_sound(sound!("block.chest.open"), _location)
.await;
}

async fn on_use_with_item<'a>(
Expand Down Expand Up @@ -65,23 +67,10 @@ impl PumpkinBlock for ChestBlock {
server: &Server,
container: &mut OpenContainer,
) {
player
.world()
.play_block_sound(sound!("block.chest.close"), location)
.await;

container.remove_player(player.entity_id());

if let Some(e) = get_block("minecraft:chest").cloned() {
server
.broadcast_packet_all(&CBlockAction::new(
&location,
1,
container.get_number_of_players() as u8,
VarInt(e.id.into()),
))
.await;
}
self.play_chest_action(container, player, location, server, ChestState::IsClosed)
.await;
}
}

Expand All @@ -106,17 +95,42 @@ impl ChestBlock {
if let Some(container_id) = server.get_container_id(location, block.clone()).await {
let open_containers = server.open_containers.read().await;
if let Some(container) = open_containers.get(&u64::from(container_id)) {
if let Some(e) = get_block("minecraft:chest").cloned() {
server
.broadcast_packet_all(&CBlockAction::new(
&location,
1,
container.get_number_of_players() as u8,
VarInt(e.id.into()),
))
.await;
}
self.play_chest_action(container, player, location, server, ChestState::IsOpened)
.await;
}
}
}

pub async fn play_chest_action(
&self,
container: &OpenContainer,
player: &Player,
location: WorldPosition,
server: &Server,
state: ChestState,
) {
let num_players = container.get_number_of_players() as u8;
if state == ChestState::IsClosed && num_players == 0 {
player
.world()
.play_block_sound(sound!("block.chest.close"), location)
.await;
} else if state == ChestState::IsOpened && num_players == 1 {
player
.world()
.play_block_sound(sound!("block.chest.open"), location)
.await;
}

if let Some(e) = get_block("minecraft:chest").cloned() {
server
.broadcast_packet_all(&CBlockAction::new(
&location,
1,
num_players,
VarInt(e.id.into()),
))
.await;
}
}
}

0 comments on commit 15754e3

Please sign in to comment.