From 4dce8aa90da11973058def75e82de3a0748937d5 Mon Sep 17 00:00:00 2001 From: StripedMonkey Date: Tue, 3 Sep 2024 20:47:30 -0400 Subject: [PATCH] Allow From> for (_,_,_) for easy destructuring. --- pumpkin-core/src/math/vector3.rs | 6 ++++++ pumpkin/src/client/player_packet.rs | 18 ++++++------------ pumpkin/src/entity/player.rs | 12 +++--------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/pumpkin-core/src/math/vector3.rs b/pumpkin-core/src/math/vector3.rs index 67bc038bc..65da454e6 100644 --- a/pumpkin-core/src/math/vector3.rs +++ b/pumpkin-core/src/math/vector3.rs @@ -98,6 +98,12 @@ impl From<(T, T, T)> for Vector3 { } } +impl From> for (T, T, T) { + fn from(vector: Vector3) -> Self { + (vector.x, vector.y, vector.z) + } +} + pub trait Math: Mul + Neg diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 8fa1852c9..3d38f7b71 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -76,9 +76,7 @@ impl Player { return; } let entity = &mut self.entity; - self.lastx = entity.pos.x; - self.lasty = entity.pos.y; - self.lastz = entity.pos.z; + self.last_position = entity.pos; entity.set_pos( Self::clamp_horizontal(position.x), Self::clamp_vertical(position.feet_y), @@ -89,9 +87,8 @@ impl Player { // send new position to all other players let on_ground = self.on_ground; let entity_id = entity.entity_id; - let (x, lastx) = (entity.pos.x, self.lastx); - let (y, lasty) = (entity.pos.y, self.lasty); - let (z, lastz) = (entity.pos.z, self.lastz); + let (x, y, z) = entity.pos.into(); + let (lastx, lasty, lastz) = self.last_position.into(); let world = self.world.clone(); let world = world.lock().await; world.broadcast_packet( @@ -125,9 +122,7 @@ impl Player { } let entity = &mut self.entity; - self.lastx = entity.pos.x; - self.lasty = entity.pos.y; - self.lastz = entity.pos.z; + self.last_position = entity.pos; entity.set_pos( Self::clamp_horizontal(position_rotation.x), Self::clamp_vertical(position_rotation.feet_y), @@ -139,9 +134,8 @@ impl Player { // send new position to all other players let on_ground = self.on_ground; let entity_id = entity.entity_id; - let (x, lastx) = (entity.pos.x, self.lastx); - let (y, lasty) = (entity.pos.y, self.lasty); - let (z, lastz) = (entity.pos.z, self.lastz); + let (x, y, z) = entity.pos.into(); + let (lastx, lasty, lastz) = self.last_position.into(); let yaw = modulus(entity.yaw * 256.0 / 360.0, 256.0); let pitch = modulus(entity.pitch * 256.0 / 360.0, 256.0); // let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor(); diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 1cb2f3b47..35b9b46b8 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -69,9 +69,7 @@ pub struct Player { /// send `send_abilties_update` when changed pub abilities: PlayerAbilities, - pub lastx: f64, - pub lasty: f64, - pub lastz: f64, + pub last_position: Vector3, // Client side value, Should be not trusted pub on_ground: bool, @@ -132,9 +130,7 @@ impl Player { abilities: PlayerAbilities::default(), gamemode, watched_section: Vector3::new(0, 0, 0), - lastx: 0.0, - lasty: 0.0, - lastz: 0.0, + last_position: Vector3::new(0.0, 0.0, 0.0), } } @@ -255,9 +251,7 @@ impl Player { } let entity = &mut self.entity; entity.set_pos(x, y, z); - self.lastx = x; - self.lasty = y; - self.lastz = z; + self.last_position = entity.pos; entity.yaw = yaw; entity.pitch = pitch; self.awaiting_teleport = Some((self.teleport_id_count.into(), Vector3::new(x, y, z)));