diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index f1cc35bfb..7c03fe932 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -580,20 +580,25 @@ 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_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| { - !player - .living_entity - .entity - .bounding_box - .load() - .intersects(&block_bounding_box) - }); + let can_place = world + .get_nearby_players( + Vector3::new( + f64::from(world_pos.0.x), + f64::from(world_pos.0.y), + f64::from(world_pos.0.z), + ), + 20, + ) + .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; diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 8e19fdfea..6a10e1cd6 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -554,25 +554,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 }