Skip to content

Commit

Permalink
spawn player on ground
Browse files Browse the repository at this point in the history
make the player spawn on the highest block instead of in the air.
  • Loading branch information
Alvsch committed Nov 5, 2024
1 parent 4328817 commit 26252a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,18 @@ impl Player {
log::debug!("Sending player abilities to {}", self.gameprofile.name);
self.send_abilties_update().await;

let world = &self.living_entity.entity.world;

// teleport
let position = Vector3::new(10.0, 120.0, 10.0);
let mut position = Vector3::new(10.0, 120.0, 10.0);
let yaw = 10.0;
let pitch = 10.0;

let top = world
.get_top_block(Vector2::new(position.x as i32, position.z as i32))
.await;
position.y = f64::from(top + 1);

log::debug!("Sending player teleport to {}", self.gameprofile.name);
self.teleport(position, yaw, pitch).await;

Expand Down
20 changes: 19 additions & 1 deletion pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ impl World {
}
}

/// Gets the y position of the first non air block from the top down
pub async fn get_top_block(&self, position: Vector2<i32>) -> i32 {
for y in (-64..=319).rev() {
let pos = WorldPosition(Vector3::new(position.x, y, position.z));
let block = self.get_block(pos).await;
if block == 0 || block == 750 || block == 751 {
continue;
}
return y;
}
319
}

#[expect(clippy::too_many_lines)]
pub async fn spawn_player(&self, base_config: &BasicConfiguration, player: Arc<Player>) {
// This code follows the vanilla packet order
Expand Down Expand Up @@ -184,10 +197,15 @@ impl World {
player.send_abilties_update().await;

// teleport
let position = Vector3::new(10.0, 120.0, 10.0);
let mut position = Vector3::new(10.0, 120.0, 10.0);
let yaw = 10.0;
let pitch = 10.0;

let top = self
.get_top_block(Vector2::new(position.x as i32, position.z as i32))
.await;
position.y = f64::from(top + 1);

log::debug!("Sending player teleport to {}", player.gameprofile.name);
player.teleport(position, yaw, pitch).await;

Expand Down

0 comments on commit 26252a2

Please sign in to comment.