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 Dec 2, 2024
1 parent 34fc7f7 commit f57ac17
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,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 f57ac17

Please sign in to comment.