azalea/client_impl/
movement.rs

1use azalea_client::{
2    PhysicsState, SprintDirection, StartSprintEvent, StartWalkEvent, WalkDirection,
3};
4use azalea_entity::{Jumping, LookDirection};
5
6use crate::Client;
7
8impl Client {
9    /// Set whether we're jumping. This acts as if you held space in
10    /// vanilla.
11    ///
12    /// If you want to jump once, use the `jump` function in `azalea`.
13    ///
14    /// If you're making a realistic client, calling this function every tick is
15    /// recommended.
16    pub fn set_jumping(&self, jumping: bool) {
17        self.query_self::<&mut Jumping, _>(|mut j| **j = jumping);
18    }
19
20    /// Returns whether the player will try to jump next tick.
21    pub fn jumping(&self) -> bool {
22        **self.component::<Jumping>()
23    }
24
25    pub fn set_crouching(&self, crouching: bool) {
26        self.query_self::<&mut PhysicsState, _>(|mut p| p.trying_to_crouch = crouching);
27    }
28
29    /// Whether the client is currently trying to sneak.
30    ///
31    /// You may want to check the [`Pose`](azalea_entity::Pose) instead.
32    pub fn crouching(&self) -> bool {
33        self.query_self::<&PhysicsState, _>(|p| p.trying_to_crouch)
34    }
35
36    /// Sets the direction the client is looking.
37    ///
38    /// `y_rot` is yaw (looking to the side, between -180 to 180), and `x_rot`
39    /// is pitch (looking up and down, between -90 to 90).
40    ///
41    /// You can get these numbers from the vanilla f3 screen.
42    pub fn set_direction(&self, y_rot: f32, x_rot: f32) {
43        self.query_self::<&mut LookDirection, _>(|mut ld| {
44            ld.update(LookDirection::new(y_rot, x_rot));
45        });
46    }
47
48    /// Returns the direction the client is looking.
49    ///
50    /// See [`Self::set_direction`] for more details.
51    pub fn direction(&self) -> LookDirection {
52        *self.component::<LookDirection>()
53    }
54
55    /// Start walking in the given direction.
56    ///
57    /// To sprint, use [`Client::sprint`]. To stop walking, call walk with
58    /// [`WalkDirection::None`].
59    ///
60    /// # Example
61    ///
62    /// ```rust,no_run
63    /// # use azalea::{Client, WalkDirection};
64    /// # use std::time::Duration;
65    /// # async fn example(mut bot: &Client) {
66    /// // walk for one second
67    /// bot.walk(WalkDirection::Forward);
68    /// tokio::time::sleep(Duration::from_secs(1)).await;
69    /// bot.walk(WalkDirection::None);
70    /// # }
71    /// ```
72    pub fn walk(&self, direction: WalkDirection) {
73        let mut ecs = self.ecs.write();
74        ecs.write_message(StartWalkEvent {
75            entity: self.entity,
76            direction,
77        });
78    }
79
80    /// Start sprinting in the given direction.
81    ///
82    /// o stop moving, call [`bot.walk(WalkDirection::None)`](Self::walk)
83    ///
84    /// # Example
85    ///
86    /// ```rust,no_run
87    /// # use azalea::{Client, WalkDirection, SprintDirection};
88    /// # use std::time::Duration;
89    /// # async fn example(bot: &Client) {
90    /// // sprint for one second
91    /// bot.sprint(SprintDirection::Forward);
92    /// tokio::time::sleep(Duration::from_secs(1)).await;
93    /// bot.walk(WalkDirection::None);
94    /// # }
95    /// ```
96    pub fn sprint(&self, direction: SprintDirection) {
97        let mut ecs = self.ecs.write();
98        ecs.write_message(StartSprintEvent {
99            entity: self.entity,
100            direction,
101        });
102    }
103}