diff options
| author | mat <git@matdoes.dev> | 2025-06-09 17:15:07 +0900 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-06-09 17:15:07 +0900 |
| commit | 4a4de819616d620d15680e71fb32390e28ab07cd (patch) | |
| tree | 9bd1159dec3527b07651d0d9c78f96c95d115e7b /azalea-protocol | |
| parent | 45d73712746fbfd365e8a68a75dfad6ae2e0d174 (diff) | |
| download | azalea-drasl-4a4de819616d620d15680e71fb32390e28ab07cd.tar.xz | |
handle relative teleports correctly and fix entity chunk indexing warnings
Diffstat (limited to 'azalea-protocol')
| -rw-r--r-- | azalea-protocol/src/common/movements.rs | 63 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/game/c_teleport_entity.rs | 2 |
2 files changed, 61 insertions, 4 deletions
diff --git a/azalea-protocol/src/common/movements.rs b/azalea-protocol/src/common/movements.rs index ffc3452f..a70342b3 100644 --- a/azalea-protocol/src/common/movements.rs +++ b/azalea-protocol/src/common/movements.rs @@ -1,8 +1,11 @@ -use std::io::{self, Cursor, Write}; +use std::{ + io::{self, Cursor, Write}, + ops::Add, +}; use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; -use azalea_core::{bitset::FixedBitSet, position::Vec3}; -use azalea_entity::LookDirection; +use azalea_core::{bitset::FixedBitSet, math, position::Vec3}; +use azalea_entity::{LookDirection, Physics, Position}; /// The updated position, velocity, and rotations for an entity. /// @@ -32,6 +35,60 @@ impl RelativeMovements { pub fn all_absolute() -> Self { RelativeMovements::default() } + pub fn all_relative() -> Self { + RelativeMovements { + x: true, + y: true, + z: true, + y_rot: true, + x_rot: true, + delta_x: true, + delta_y: true, + delta_z: true, + rotate_delta: true, + } + } + + pub fn apply( + &self, + change: &PositionMoveRotation, + position: &mut Position, + direction: &mut LookDirection, + physics: &mut Physics, + ) { + let new_position = Vec3::new( + apply_change(position.x, self.x, change.pos.x), + apply_change(position.y, self.y, change.pos.y), + apply_change(position.z, self.z, change.pos.z), + ); + + let new_look_direction = LookDirection::new( + apply_change(direction.y_rot, self.y_rot, change.look_direction.y_rot), + apply_change(direction.x_rot, self.x_rot, change.look_direction.x_rot), + ); + + let mut new_delta = physics.velocity; + if self.rotate_delta { + let y_rot_delta = direction.y_rot - new_look_direction.y_rot; + let x_rot_delta = direction.x_rot - new_look_direction.x_rot; + new_delta = new_delta + .x_rot(math::to_radians(x_rot_delta as f64) as f32) + .y_rot(math::to_radians(y_rot_delta as f64) as f32); + } + let new_delta = Vec3::new( + apply_change(new_delta.x, self.delta_x, change.delta.x), + apply_change(new_delta.y, self.delta_y, change.delta.y), + apply_change(new_delta.z, self.delta_z, change.delta.z), + ); + + **position = new_position; + *direction = new_look_direction; + physics.velocity = new_delta; + } +} + +fn apply_change<T: Add<Output = T>>(base: T, condition: bool, change: T) -> T { + if condition { base + change } else { change } } impl AzaleaRead for RelativeMovements { diff --git a/azalea-protocol/src/packets/game/c_teleport_entity.rs b/azalea-protocol/src/packets/game/c_teleport_entity.rs index 92b8f1eb..e19ded58 100644 --- a/azalea-protocol/src/packets/game/c_teleport_entity.rs +++ b/azalea-protocol/src/packets/game/c_teleport_entity.rs @@ -9,6 +9,6 @@ pub struct ClientboundTeleportEntity { #[var] pub id: MinecraftEntityId, pub change: PositionMoveRotation, - pub relatives: RelativeMovements, + pub relative: RelativeMovements, pub on_ground: bool, } |
