Skip to content

Commit

Permalink
Add seach for nearby players when placing block
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiiita committed Nov 15, 2024
1 parent e600a1a commit 1044a75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,10 @@ impl Player {
let world_pos = WorldPosition(location.0 + face.to_offset());
let block_bounding_box = BoundingBox::from_block(&world_pos);


let can_place =
world
.get_players_by_pos(world_pos)
.get_nearby_players(Vector3::new(world_pos.0.x as f64, world_pos.0.y as f64, world_pos.0.z as f64), 20)
.await
.values()
.all(|player| {
Expand Down
22 changes: 22 additions & 0 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,28 @@ impl World {
.collect::<HashMap<uuid::Uuid, Arc<Player>>>()
}

pub async fn get_nearby_players(&self, pos: Vector3<f64>, radius: u16) -> HashMap<uuid::Uuid, Arc<Player>> {
let radius_squared = (radius as f64).powi(2);

let mut found_players = HashMap::new();
for player in self.current_players.lock().await.iter() {
let player_pos = player.1.living_entity.entity.pos.load();

let diff = Vector3::new(
player_pos.x - pos.x,
player_pos.y - pos.y,
player_pos.z - pos.z
);

let distance_squared = diff.x.powi(2) + diff.y.powi(2) + diff.z.powi(2);
if distance_squared <= radius_squared {
found_players.insert(player.0.clone(), player.1.clone());
}
}

found_players
}

/// 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 1044a75

Please sign in to comment.