Skip to content

Commit

Permalink
Add nearby player check when placing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiiita committed Nov 10, 2024
1 parent a375882 commit e600a1a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,22 @@ impl Player {

let world_pos = WorldPosition(location.0 + face.to_offset());
let block_bounding_box = BoundingBox::from_block(&world_pos);
let bounding_box = entity.bounding_box.load();
//TODO: Make this check for every entity in that posistion
if !bounding_box.intersects(&block_bounding_box) {

let can_place =
world
.get_players_by_pos(world_pos)
.await
.values()
.all(|player| {
!player
.living_entity
.entity
.bounding_box
.load()
.intersects(&block_bounding_box)
});

if can_place {
world.set_block(world_pos, block.default_state_id).await;
}
}
Expand Down
31 changes: 31 additions & 0 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,37 @@ impl World {
return self.current_players.lock().await.get(&id).cloned();
}

/// Gets a list of players who's location equals the given position in the world.
///
/// It iterates through the players in the world and checks their location. If the player's location matches the
/// given position it will add this to a Vec which it later returns. If no
/// player was found in that position it will just return an empty Vec.
///
/// # Arguments
///
/// * `position`: The position the function will check.
pub async fn get_players_by_pos(
&self,
position: WorldPosition,
) -> HashMap<uuid::Uuid, Arc<Player>> {
self.current_players
.lock()
.await
.iter()
.filter_map(|(uuid, player)| {
let player_block_pos = player.living_entity.entity.block_pos.load().0;
if position.0.x == player_block_pos.x
&& position.0.y == player_block_pos.y
&& position.0.z == player_block_pos.z
{
Some((*uuid, Arc::clone(player)))
} else {
None
}
})
.collect::<HashMap<uuid::Uuid, Arc<Player>>>()
}

/// Adds a player to the world and broadcasts a join message if enabled.
///
/// This function takes a player's UUID and an `Arc<Player>` reference.
Expand Down

0 comments on commit e600a1a

Please sign in to comment.