Skip to main content

azalea/entity_ref/
shared_impls.rs

1use azalea_core::{entity_id::MinecraftEntityId, position::Vec3};
2use azalea_entity::{
3    Attributes, Dead, EntityUuid, Physics, Position, dimensions::EntityDimensions, metadata::Health,
4};
5use azalea_world::WorldName;
6use uuid::Uuid;
7
8use super::EntityRef;
9use crate::Client;
10
11macro_rules! impl_entity_functions {
12    ( $(
13        Client:
14        $(#[$client_doc:meta])*
15        EntityRef:
16        $(#[$entityref_doc:meta])*
17        pub fn $fn_name:ident (&$fn_self:ident) -> $fn_returns:ty $fn_impl:block
18    )* ) => {
19        $(
20            impl Client {
21                $(#[$client_doc])*
22                pub fn $fn_name(&$fn_self) -> $fn_returns $fn_impl
23            }
24            impl EntityRef {
25                $(#[$entityref_doc])*
26                pub fn $fn_name(&$fn_self) -> $fn_returns $fn_impl
27            }
28        )*
29    };
30}
31
32// these functions are defined this way because we want them to be duplicated
33// for both Client and EntityRef but still have their own documentation
34impl_entity_functions! {
35    Client:
36    /// Get the client's position in the world, which is the same as its feet
37    /// position.
38    ///
39    /// This is a shortcut for `**bot.component::<Position>()`.
40    ///
41    /// To get the client's eye position, use [`Self::eye_position`].
42    ///
43    /// Note that this value is given a default of [`Vec3::ZERO`] when it
44    /// receives the login packet, its true position may be set ticks
45    /// later.
46    EntityRef:
47    /// Get the entity's position in the world, which is the same as its feet
48    /// position.
49    ///
50    /// To get the client's eye position, use [`Self::eye_position`].
51    ///
52    /// Also see [`Client::position`].
53    pub fn position(&self) -> Vec3 {
54        **self.component::<Position>()
55    }
56
57    Client:
58    /// Get the bounding box dimensions for our client, which contains our
59    /// width, height, and eye height.
60    ///
61    /// This is a shortcut for
62    /// `self.component::<EntityDimensions>()`.
63    EntityRef:
64    /// Get the bounding box dimensions for the entity, which contains its
65    /// width, height, and eye height.
66    ///
67    /// Also see [`Client::dimensions`]
68    pub fn dimensions(&self) -> EntityDimensions {
69        self.component::<EntityDimensions>().clone()
70    }
71
72    Client:
73    /// Get the position of this client's eyes.
74    ///
75    /// Also see [`Self::position`].
76    ///
77    /// This is a shortcut for
78    /// `bot.position().up(bot.dimensions().eye_height)`.
79    EntityRef:
80    /// Get the position of this entity's eyes.
81    ///
82    /// Also see [`Client::eye_position`].
83    pub fn eye_position(&self) -> Vec3 {
84        self.query_self::<(&Position, &EntityDimensions), _>(|(pos, dim)| {
85            pos.up(dim.eye_height as f64)
86        })
87    }
88
89    Client:
90    /// Get the health of this client, typically in the range `0..=20`.
91    ///
92    /// This is a shortcut for `*bot.component::<Health>()`.
93    EntityRef:
94    /// Get the health of this entity, typically in the range `0..=20`.
95    ///
96    /// Also see [`Client::health`].
97    pub fn health(&self) -> f32 {
98        **self.component::<Health>()
99    }
100
101    Client:
102    /// Get the Minecraft UUID of this client.
103    ///
104    /// This is a shortcut for `**self.component::<EntityUuid>()`.
105    EntityRef:
106    /// Get the Minecraft UUID of this entity.
107    ///
108    /// Also see [`Client::uuid`].
109    pub fn uuid(&self) -> Uuid {
110        **self.component::<EntityUuid>()
111    }
112
113    Client:
114    /// Get the Minecraft ID of this client.
115    ///
116    /// See [`MinecraftEntityId`] for more details. For persistent identifiers,
117    /// consider using [`Self::uuid`] instead.
118    ///
119    /// This is a shortcut for `**self.component::<MinecraftEntityId>()`.
120    EntityRef:
121    /// Get the Minecraft UUID of this entity.
122    ///
123    /// See [`MinecraftEntityId`] for more details. For persistent identifiers,
124    /// consider using [`Self::uuid`] instead.
125    ///
126    /// Also see [`Client::minecraft_id`].
127    pub fn minecraft_id(&self) -> MinecraftEntityId {
128        *self.component::<MinecraftEntityId>()
129    }
130
131    Client:
132    /// Returns the attribute values of our player, which can be used to
133    /// determine things like our movement speed.
134    EntityRef:
135    /// Returns the attribute values of the entity, which can be used to
136    /// determine things like its movement speed.
137    pub fn attributes(&self) -> Attributes {
138        // this *could* return a mapped read guard for performance but that rarely
139        // matters and it's just easier for the user if it doesn't.
140        self.component::<Attributes>().clone()
141    }
142
143    Client:
144    #[deprecated = "renamed to `world_name`."]
145    EntityRef:
146    #[deprecated = "renamed to `world_name`."]
147    pub fn instance_name(&self) -> WorldName {
148        self.world_name()
149    }
150
151    Client:
152    /// Get the name of the world that the bot is in.
153    ///
154    /// This can be used to check if the client is in the same world as another
155    /// entity.
156    #[doc(alias("dimension_name"))]
157    EntityRef:
158    /// Get the name of the world that the entity is in.
159    ///
160    /// This can be used to check if the entity is in the same world as another
161    /// entity.
162    ///
163    /// Also see [`Client::world_name`],
164    #[doc(alias("dimension_name"))]
165    pub fn world_name(&self) -> WorldName {
166        (*self.component::<WorldName>()).clone()
167    }
168
169    Client:
170    /// Returns whether the client is alive and in the world.
171    ///
172    /// You should avoid using this if you have auto-respawn enabled (which is
173    /// the default), instead consider watching for
174    /// [`Event::Death`](crate::Event::Death) instead.
175    ///
176    /// Also see [`Self::exists`].
177    EntityRef:
178    /// Returns whether the entity is alive and hasn't despawned.
179    ///
180    /// Unlike most functions in `EntityRef`, this one will not panic if the
181    /// entity is despawned. Because of this, it may be useful to check `is_alive`
182    /// before calling functions that request data from the world.
183    ///
184    /// Also see [`Client::is_alive`] and [`Self::exists`].
185    pub fn is_alive(&self) -> bool {
186        self.try_query_self::<Option<&Dead>, _>(|dead| dead.is_none()).unwrap_or(false)
187    }
188
189    Client:
190    /// Returns whether the client is in the world (has been assigned an entity ID).
191    ///
192    /// Like [`Self::is_alive`], this will not panic.
193    EntityRef:
194    /// Returns whether the entity is in the world and hasn't despawned.
195    ///
196    /// Like [`Self::is_alive`], this will not panic.
197    ///
198    /// Also see [`Client::exists`].
199    pub fn exists(&self) -> bool {
200        self.try_query_self::<Option<&MinecraftEntityId>, _>(|entity_id| entity_id.is_some()).unwrap_or(false)
201    }
202
203    Client:
204    /// Returns the complete [`Physics`] data for this client, including velocity, bounding box,
205    /// collisions, etc.
206    EntityRef:
207    /// Returns the complete [`Physics`] data for this entity, including velocity, bounding box,
208    /// collisions, etc.
209    ///
210    /// Also see [`Client::physics`].
211    pub fn physics(&self) -> Physics {
212        self.component::<Physics>().clone()
213    }
214}