Skip to content

Commit

Permalink
Merge pull request #161 from Alvsch/master
Browse files Browse the repository at this point in the history
Fixed player movement syncing
  • Loading branch information
Snowiiii authored Oct 20, 2024
2 parents 05e921e + 0c0ca16 commit f14b9b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
32 changes: 21 additions & 11 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,26 @@ impl Player {
self.kick(TextComponent::text("Invalid movement")).await;
return;
}

let entity = &self.living_entity.entity;
entity.set_pos(
Self::clamp_horizontal(position.x),
Self::clamp_vertical(position.feet_y),
Self::clamp_horizontal(position.z),
);

let pos = entity.pos.load();
self.last_position.store(pos);
let last_position = self.last_position.load();

self.last_position.store(pos);

entity
.on_ground
.store(position.ground, std::sync::atomic::Ordering::Relaxed);

let entity_id = entity.entity_id;
let Vector3 { x, y, z } = pos;
let (lastx, lasty, lastz) = (last_position.x, last_position.y, last_position.z);
let (last_x, last_y, last_z) = (last_position.x, last_position.y, last_position.z);
let world = &entity.world;

// let delta = Vector3::new(x - lastx, y - lasty, z - lastz);
Expand All @@ -110,9 +115,9 @@ impl Player {
&[self.client.id],
&CUpdateEntityPos::new(
entity_id.into(),
x.mul_add(4096.0, -(lastx * 4096.0)) as i16,
y.mul_add(4096.0, -(lasty * 4096.0)) as i16,
z.mul_add(4096.0, -(lastz * 4096.0)) as i16,
x.mul_add(4096.0, -(last_x * 4096.0)) as i16,
y.mul_add(4096.0, -(last_y * 4096.0)) as i16,
z.mul_add(4096.0, -(last_z * 4096.0)) as i16,
position.ground,
),
)
Expand All @@ -128,32 +133,37 @@ impl Player {
self.kick(TextComponent::text("Invalid movement")).await;
return;
}

if position_rotation.yaw.is_infinite() || position_rotation.pitch.is_infinite() {
self.kick(TextComponent::text("Invalid rotation")).await;
return;
}
let entity = &self.living_entity.entity;

let entity = &self.living_entity.entity;
entity.set_pos(
Self::clamp_horizontal(position_rotation.x),
Self::clamp_vertical(position_rotation.feet_y),
Self::clamp_horizontal(position_rotation.z),
);

let pos = entity.pos.load();
self.last_position.store(pos);
let last_position = self.last_position.load();

self.last_position.store(pos);
entity.on_ground.store(
position_rotation.ground,
std::sync::atomic::Ordering::Relaxed,
);

entity.set_rotation(
wrap_degrees(position_rotation.yaw) % 360.0,
wrap_degrees(position_rotation.pitch).clamp(-90.0, 90.0) % 360.0,
);

let entity_id = entity.entity_id;
let Vector3 { x, y, z } = pos;
let (lastx, lasty, lastz) = (last_position.x, last_position.y, last_position.z);
let (last_x, last_y, last_z) = (last_position.x, last_position.y, last_position.z);

let yaw = modulus(entity.yaw.load() * 256.0 / 360.0, 256.0);
let pitch = modulus(entity.pitch.load() * 256.0 / 360.0, 256.0);
// let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor();
Expand All @@ -178,9 +188,9 @@ impl Player {
&[self.client.id],
&CUpdateEntityPosRot::new(
entity_id.into(),
x.mul_add(4096.0, -(lastx * 4096.0)) as i16,
y.mul_add(4096.0, -(lasty * 4096.0)) as i16,
z.mul_add(4096.0, -(lastz * 4096.0)) as i16,
x.mul_add(4096.0, -(last_x * 4096.0)) as i16,
y.mul_add(4096.0, -(last_y * 4096.0)) as i16,
z.mul_add(4096.0, -(last_z * 4096.0)) as i16,
yaw as u8,
pitch as u8,
position_rotation.ground,
Expand Down
1 change: 1 addition & 0 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Player {
.await;
}

/// yaw and pitch in degrees
pub async fn teleport(&self, x: f64, y: f64, z: f64, yaw: f32, pitch: f32) {
// this is the ultra special magic code used to create the teleport id
// This returns the old value
Expand Down
6 changes: 5 additions & 1 deletion pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ impl World {
let z = 10.0;
let yaw = 10.0;
let pitch = 10.0;
player.teleport(x, y, z, 10.0, 10.0).await;
player.teleport(x, y, z, yaw, pitch).await;

let pos = player.living_entity.entity.pos.load();
player.last_position.store(pos);

let gameprofile = &player.gameprofile;
// first send info update to our new player, So he can see his Skin
// also send his info to everyone else
Expand Down

0 comments on commit f14b9b1

Please sign in to comment.