diff --git a/pumpkin-inventory/src/open_container.rs b/pumpkin-inventory/src/open_container.rs index 3025ed92..c563ee47 100644 --- a/pumpkin-inventory/src/open_container.rs +++ b/pumpkin-inventory/src/open_container.rs @@ -76,6 +76,10 @@ impl OpenContainer { self.players.clone() } + pub fn get_number_of_players(&self) -> usize { + self.players.len() + } + pub fn get_location(&self) -> Option { self.location } diff --git a/pumpkin/src/block/blocks/chest.rs b/pumpkin/src/block/blocks/chest.rs index 1be42eca..9baf687d 100644 --- a/pumpkin/src/block/blocks/chest.rs +++ b/pumpkin/src/block/blocks/chest.rs @@ -63,16 +63,23 @@ impl PumpkinBlock for ChestBlock { player: &Player, location: WorldPosition, server: &Server, - _container: &OpenContainer, + 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, 0, VarInt(e.id.into()))) + .broadcast_packet_all(&CBlockAction::new( + &location, + 1, + container.get_number_of_players() as u8, + VarInt(e.id.into()), + )) .await; } } @@ -96,10 +103,20 @@ impl ChestBlock { ) .await; - if let Some(e) = get_block("minecraft:chest").cloned() { - server - .broadcast_packet_all(&CBlockAction::new(&location, 1, 1, VarInt(e.id.into()))) - .await; + 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; + } + } } } } diff --git a/pumpkin/src/block/blocks/crafting_table.rs b/pumpkin/src/block/blocks/crafting_table.rs index 01ad4179..c879f060 100644 --- a/pumpkin/src/block/blocks/crafting_table.rs +++ b/pumpkin/src/block/blocks/crafting_table.rs @@ -53,7 +53,7 @@ impl PumpkinBlock for CraftingTableBlock { player: &Player, _location: WorldPosition, _server: &Server, - container: &OpenContainer, + container: &mut OpenContainer, ) { let entity_id = player.entity_id(); for player_id in container.all_player_ids() { @@ -62,6 +62,8 @@ impl PumpkinBlock for CraftingTableBlock { } } + container.remove_player(entity_id); + // TODO: items should be re-added to player inventory or dropped dependending on if they are in movement. // TODO: unique containers should be implemented as a separate stack internally (optimizes large player servers for example) // TODO: ephemeral containers (crafting tables) might need to be separate data structure than stored (ender chest) diff --git a/pumpkin/src/block/pumpkin_block.rs b/pumpkin/src/block/pumpkin_block.rs index c0a65c31..90de5718 100644 --- a/pumpkin/src/block/pumpkin_block.rs +++ b/pumpkin/src/block/pumpkin_block.rs @@ -60,7 +60,7 @@ pub trait PumpkinBlock: Send + Sync { _player: &Player, _location: WorldPosition, _server: &Server, - _container: &OpenContainer, + _container: &mut OpenContainer, ) { } }