diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index c7f1fb01..8a5fb6aa 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -723,25 +723,38 @@ impl World { .collect::>>() } - pub async fn get_nearby_players(&self, pos: Vector3, radius: u16) -> HashMap> { - let radius_squared = (radius as f64).powi(2); - + /// Gets the nearby players around a given world position + /// It "creates" a sphere and checks if whether players are inside + /// and returns a hashmap where the uuid is the key and the player + /// object the value. + /// + /// # Arguments + /// * `pos`: The middlepoint of the sphere + /// * `radius`: The radius of the sphere. The higher the radius + /// the more area will be checked, in every direction. + pub async fn get_nearby_players( + &self, + pos: Vector3, + radius: u16, + ) -> HashMap> { + let radius_squared = (f64::from(radius)).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 + 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.insert(*player.0, player.1.clone()); } } - + found_players }