1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use std::{any, sync::Arc};
use azalea_core::position::Vec3;
use azalea_entity::Position;
use azalea_world::WorldName;
use bevy_ecs::{
component::Component,
entity::Entity,
query::{QueryData, QueryEntityError, QueryFilter, QueryItem, ROQueryItem},
world::World,
};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use crate::{Client, entity_ref::EntityRef};
impl Client {
/// Get a component from the client.
///
/// This allows you to access certain data stored about the client entity
/// that isn't accessible in a simpler way.
///
/// This returns a reference to the component wrapped by a read guard. This
/// makes the component cheap to access, but means that the ECS cannot be
/// mutated while it's in scope (it will cause a deadlock). In some cases,
/// it may be simpler for you to immediately clone the component after
/// accessing it.
///
/// If the component isn't guaranteed to be present, consider using
/// [`Self::get_component`] instead.
///
/// To do more complex queries or to mutate data, see [`Self::query_self`].
///
/// To access data about other entities, you can use
/// [`Self::entity_component`] (and its other related functions).
///
/// You may also use [`Self::ecs`] directly if you need more control over
/// when the ECS is locked.
///
/// # Panics
///
/// This will panic if the component doesn't exist on the client. Use
/// [`Self::get_component`] to avoid this.
///
/// # Examples
///
/// ```
/// # use azalea_world::WorldName;
/// # fn example(client: &azalea::Client) {
/// let world_name = client.component::<WorldName>();
/// # }
pub fn component<T: Component>(&self) -> MappedRwLockReadGuard<'_, T> {
self.get_component::<T>().unwrap_or_else(|| {
panic!(
"Our client is missing a required component: {:?}",
any::type_name::<&T>()
)
})
}
/// Get a component on this client, or `None` if it doesn't exist.
///
/// If the component is guaranteed to be present, consider using
/// [`Self::component`]. Also see that function for more details.
pub fn get_component<T: Component>(&self) -> Option<MappedRwLockReadGuard<'_, T>> {
self.get_entity_component::<T>(self.entity)
}
/// Query the ECS for data from our client entity.
///
/// To query another entity, you can use [`Self::query_entity`].
///
/// You can use this to mutate data on the client.
///
/// # Examples
///
/// ```
/// # use azalea_entity::Position;
/// # fn example(mut client: azalea::Client) {
/// // teleport one block up
/// client.query_self::<&mut Position, _>(|mut pos| pos.y += 1.0);
/// # }
/// ```
///
/// # Panics
///
/// This will panic if the client is missing a component required by the
/// query. Consider using [`Self::try_query_self`] to avoid this.
pub fn query_self<D: QueryData, R>(&self, f: impl FnOnce(QueryItem<D>) -> R) -> R {
self.try_query_self::<D, R>(f).unwrap_or_else(|_| {
panic!(
"`Client::query_self` failed when querying for {:?}",
any::type_name::<D>()
)
})
}
/// Query the ECS for data from our client entity, or return `None` if the
/// query failed.
///
/// Also see [`Self::query_self`].
pub fn try_query_self<D: QueryData, R>(
&self,
f: impl FnOnce(QueryItem<D>) -> R,
) -> Result<R, QueryEntityError> {
let mut ecs = self.ecs.write();
let mut qs = ecs.query::<D>();
qs.get_mut(&mut ecs, self.entity).map(f)
}
/// Query the ECS for data from an entity.
///
/// Note that it is often simpler to use [`Self::entity_component`].
///
/// To query the client, you should use [`Self::query_self`].
///
/// You can also use this to mutate data on an entity.
///
/// # Panics
///
/// This will panic if the entity doesn't exist or if the query isn't valid
/// for the entity. For a non-panicking version, you may use
/// [`Self::try_query_entity`].
pub fn query_entity<D: QueryData, R>(
&self,
entity: Entity,
f: impl FnOnce(QueryItem<D>) -> R,
) -> R {
self.try_query_entity(entity, f).unwrap_or_else(|_| {
panic!(
"Querying entity {entity} failed when getting {:?}",
any::type_name::<D>()
)
})
}
/// A convenience function for getting components from any entity, or None
/// if the query fails.
///
/// If you're sure that the entity exists and that the query will succeed,
/// you can use [`Self::query_entity`].
pub fn try_query_entity<D: QueryData, R>(
&self,
entity: Entity,
f: impl FnOnce(QueryItem<D>) -> R,
) -> Result<R, QueryEntityError> {
let mut ecs = self.ecs.write();
let mut qs = ecs.query::<D>();
qs.get_mut(&mut ecs, entity).map(f)
}
/// Quickly returns an [`EntityRef`] for an arbitrary entity that
/// matches the given predicate function that is in the same
/// [`World`] as the client.
///
/// [`World`]: azalea_world::World
pub fn any_entity_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Option<EntityRef> {
self.any_entity_id_by(predicate)
.map(|e| self.entity_ref_for(e))
}
/// Quickly returns a lightweight [`Entity`] for an arbitrary entity that
/// matches the given predicate function that is in the same
/// [`World`] as the client.
///
/// To get an [`EntityRef`], consider using [`Self::any_entity_by`]
/// instead.
///
/// If you want to find the nearest entity, consider using
/// [`Self::nearest_entity_id_by`] instead. If you want to find all entities
/// that match the predicate, use [`Self::nearest_entity_ids_by`].
///
/// # Example
///
/// ```
/// use azalea::{entity::metadata::Player, player::GameProfileComponent};
/// use bevy_ecs::query::With;
///
/// # fn example(mut bot: azalea::Client, sender_name: String) {
/// let entity = bot.any_entity_id_by::<&GameProfileComponent, With<Player>>(
/// |profile: &GameProfileComponent| profile.name == sender_name,
/// );
/// # }
/// ```
///
/// [`World`]: azalea_world::World
pub fn any_entity_id_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Option<Entity> {
let world_name = self.get_component::<WorldName>()?.clone();
predicate.find_any(self.ecs.clone(), &world_name)
}
/// Return an [`EntityRef`] for the nearest entity that matches the
/// given predicate function.
///
/// If you don't need the entity to be the nearest one, it may be more
/// efficient to use [`Self::any_entity_by`] instead. You can also use
/// [`Self::nearest_entities_by`] to get all nearby entities.
///
/// Also see [`Self::nearest_entity_id_by`] if you only need the lightweight
/// [`Entity`] identifier.
pub fn nearest_entity_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Option<EntityRef> {
self.nearest_entity_id_by(predicate)
.map(|e| self.entity_ref_for(e))
}
/// Return a lightweight [`Entity`] for the nearest entity that matches the
/// given predicate function.
///
/// To get an [`EntityRef`], consider using [`Self::nearest_entity_by`]
/// instead.
///
/// If you don't need the entity to be the nearest one, it may be more
/// efficient to use [`Self::any_entity_id_by`] instead. You can also use
/// [`Self::nearest_entity_ids_by`] to get all nearby entities.
pub fn nearest_entity_id_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Option<Entity> {
self.nearest_entity_ids_by(predicate).first().copied()
}
/// Returns an array of all [`EntityRef`]s in the world that match the
/// predicate, sorted by nearest first.
///
/// To only get the nearest entity, consider using
/// [`Self::nearest_entity_by`]. If you only need the [`Entity`]
/// identifiers, you can use [`Self::nearest_entity_ids_by`] instead.
pub fn nearest_entities_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Box<[EntityRef]> {
self.nearest_entity_ids_by(predicate)
.into_iter()
.map(|e| self.entity_ref_for(e))
.collect()
}
/// Returns an array of all [`Entity`]s in the world that match the
/// predicate, sorted by nearest first.
///
/// To only get the nearest entity, consider using
/// [`Self::nearest_entity_id_by`]. To get the [`EntityRef`]s instead, you
/// can use [`Self::nearest_entities_by`].
///
/// ```
/// # use azalea_entity::{LocalEntity, Position, metadata::Player};
/// # use bevy_ecs::query::{With, Without};
/// # fn example(mut bot: azalea::Client, sender_name: String) {
/// let nearby_players =
/// bot.nearest_entities_by::<(), (With<Player>, Without<LocalEntity>)>(|_: ()| true);
/// # }
/// ```
pub fn nearest_entity_ids_by<Q: QueryData, F: QueryFilter>(
&self,
predicate: impl EntityPredicate<Q, F>,
) -> Box<[Entity]> {
let (world_name, position) = {
let Some(world_name) = self.get_component::<WorldName>() else {
return Box::new([]);
};
let Some(position) = self.get_component::<Position>() else {
return Box::new([]);
};
(world_name.clone(), **position)
};
predicate.find_all_sorted(self.ecs.clone(), &world_name, position)
}
/// Get a component from an entity.
///
/// This allows you to access data stored about entities that isn't
/// accessible in a simpler way.
///
/// This returns a reference to the component wrapped by a read guard. This
/// makes the component cheap to access, but means that the ECS cannot be
/// mutated while it's in scope. In some cases, it may be simpler for you to
/// immediately clone the component after accessing it.
///
/// If you're trying to get a component for this client, you should use
/// [`Self::component`] instead.
///
/// To do more complex queries or to mutate data, see
/// [`Self::query_entity`].
///
/// # Panics
///
/// This will panic if the component doesn't exist on the entity. Use
/// [`Self::get_entity_component`] to avoid this.
pub fn entity_component<T: Component>(&self, entity: Entity) -> MappedRwLockReadGuard<'_, T> {
self.get_entity_component::<T>(entity).unwrap_or_else(|| {
panic!(
"Entity {entity} is missing a required component: {:?}",
any::type_name::<&T>()
)
})
}
/// Get a component from an entity, if it exists.
///
/// This is similar to [`Self::entity_component`] but returns an `Option`
/// instead of panicking if the component isn't present.
pub fn get_entity_component<T: Component>(
&self,
entity: Entity,
) -> Option<MappedRwLockReadGuard<'_, T>> {
let ecs = self.ecs.read();
RwLockReadGuard::try_map(ecs, |ecs: &World| ecs.get(entity)).ok()
}
}
pub trait EntityPredicate<Q: QueryData, Filter: QueryFilter> {
fn find_any(&self, ecs_lock: Arc<RwLock<World>>, world_name: &WorldName) -> Option<Entity>;
fn find_all_sorted(
&self,
ecs_lock: Arc<RwLock<World>>,
world_name: &WorldName,
nearest_to: Vec3,
) -> Box<[Entity]>;
}
impl<F, Q: QueryData, Filter: QueryFilter> EntityPredicate<Q, Filter> for F
where
F: Fn(ROQueryItem<Q>) -> bool,
for<'w, 's> <<Q as QueryData>::ReadOnly as QueryData>::Item<'w, 's>: Copy,
{
fn find_any(&self, ecs_lock: Arc<RwLock<World>>, world_name: &WorldName) -> Option<Entity> {
let mut ecs = ecs_lock.write();
let mut query = ecs.query_filtered::<(Entity, &WorldName, Q), Filter>();
query
.iter(&ecs)
.find(|(_, e_world_name, q)| *e_world_name == world_name && (self)(*q))
.map(|(e, _, _)| e)
}
fn find_all_sorted(
&self,
ecs_lock: Arc<RwLock<World>>,
world_name: &WorldName,
nearest_to: Vec3,
) -> Box<[Entity]> {
let mut ecs = ecs_lock.write();
let mut query = ecs.query_filtered::<(Entity, &WorldName, &Position, Q), Filter>();
let mut entities = query
.iter(&ecs)
.filter(|(_, e_world_name, _, q)| *e_world_name == world_name && (self)(*q))
.map(|(e, _, position, _)| (e, Vec3::from(position)))
.collect::<Vec<(Entity, Vec3)>>();
entities.sort_by_cached_key(|(_, position)| {
// to_bits is fine here as long as the number is positive
position.distance_squared_to(nearest_to).to_bits()
});
entities
.into_iter()
.map(|(e, _)| e)
.collect::<Box<[Entity]>>()
}
}