From 9513f42e87f64c409cdb2a100500a50e5a713bac Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 27 Dec 2025 22:02:00 -0600 Subject: Move Client struct to azalea crate (#297) * move the Client struct out of azalea-client into azalea * actually add client impls in azalea --- azalea/src/client_impl/movement.rs | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 azalea/src/client_impl/movement.rs (limited to 'azalea/src/client_impl/movement.rs') diff --git a/azalea/src/client_impl/movement.rs b/azalea/src/client_impl/movement.rs new file mode 100644 index 00000000..08624263 --- /dev/null +++ b/azalea/src/client_impl/movement.rs @@ -0,0 +1,104 @@ +use azalea_client::{ + PhysicsState, SprintDirection, StartSprintEvent, StartWalkEvent, WalkDirection, +}; +use azalea_entity::{Jumping, LookDirection}; + +use crate::Client; + +impl Client { + /// Set whether we're jumping. This acts as if you held space in + /// vanilla. + /// + /// If you want to jump once, use the `jump` function in `azalea`. + /// + /// If you're making a realistic client, calling this function every tick is + /// recommended. + pub fn set_jumping(&self, jumping: bool) { + self.query_self::<&mut Jumping, _>(|mut j| **j = jumping); + } + + /// Returns whether the player will try to jump next tick. + pub fn jumping(&self) -> bool { + *self.component::() + } + + pub fn set_crouching(&self, crouching: bool) { + self.query_self::<&mut PhysicsState, _>(|mut p| p.trying_to_crouch = crouching); + } + + /// Whether the client is currently trying to sneak. + /// + /// You may want to check the [`Pose`] instead. + pub fn crouching(&self) -> bool { + self.query_self::<&PhysicsState, _>(|p| p.trying_to_crouch) + } + + /// Sets the direction the client is looking. + /// + /// `y_rot` is yaw (looking to the side, between -180 to 180), and `x_rot` + /// is pitch (looking up and down, between -90 to 90). + /// + /// You can get these numbers from the vanilla f3 screen. + pub fn set_direction(&self, y_rot: f32, x_rot: f32) { + self.query_self::<&mut LookDirection, _>(|mut ld| { + ld.update(LookDirection::new(y_rot, x_rot)); + }); + } + + /// Returns the direction the client is looking. + /// + /// See [`Self::set_direction`] for more details. + pub fn direction(&self) -> (f32, f32) { + let look_direction: LookDirection = self.component::(); + (look_direction.y_rot(), look_direction.x_rot()) + } + + /// Start walking in the given direction. + /// + /// To sprint, use [`Client::sprint`]. To stop walking, call walk with + /// [`WalkDirection::None`]. + /// + /// # Example + /// + /// ```rust,no_run + /// # use azalea::{Client, WalkDirection}; + /// # use std::time::Duration; + /// # async fn example(mut bot: &Client) { + /// // walk for one second + /// bot.walk(WalkDirection::Forward); + /// tokio::time::sleep(Duration::from_secs(1)).await; + /// bot.walk(WalkDirection::None); + /// # } + /// ``` + pub fn walk(&self, direction: WalkDirection) { + let mut ecs = self.ecs.lock(); + ecs.write_message(StartWalkEvent { + entity: self.entity, + direction, + }); + } + + /// Start sprinting in the given direction. + /// + /// o stop moving, call [`bot.walk(WalkDirection::None)`](Self::walk) + /// + /// # Example + /// + /// ```rust,no_run + /// # use azalea::{Client, WalkDirection, SprintDirection}; + /// # use std::time::Duration; + /// # async fn example(bot: &Client) { + /// // sprint for one second + /// bot.sprint(SprintDirection::Forward); + /// tokio::time::sleep(Duration::from_secs(1)).await; + /// bot.walk(WalkDirection::None); + /// # } + /// ``` + pub fn sprint(&self, direction: SprintDirection) { + let mut ecs = self.ecs.lock(); + ecs.write_message(StartSprintEvent { + entity: self.entity, + direction, + }); + } +} -- cgit v1.2.3