Skip to content

Commit

Permalink
Added number tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialKris committed Dec 27, 2024
1 parent dbd821c commit 3b89599
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions pumpkin-inventory/src/open_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorldPosition> {
self.location
}
Expand Down
29 changes: 23 additions & 6 deletions pumpkin/src/block/blocks/chest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
}
}
}
4 changes: 3 additions & 1 deletion pumpkin/src/block/blocks/crafting_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/block/pumpkin_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait PumpkinBlock: Send + Sync {
_player: &Player,
_location: WorldPosition,
_server: &Server,
_container: &OpenContainer,
_container: &mut OpenContainer,
) {
}
}

0 comments on commit 3b89599

Please sign in to comment.