Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiiita committed Nov 15, 2024
1 parent 1044a75 commit b9ee79b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
33 changes: 19 additions & 14 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 21 additions & 8 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,25 +554,38 @@ 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);

/// 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<f64>,
radius: u16,
) -> HashMap<uuid::Uuid, Arc<Player>> {
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
}

Expand Down

0 comments on commit b9ee79b

Please sign in to comment.