diff options
| -rw-r--r-- | azalea-client/src/local_player.rs | 4 | ||||
| -rw-r--r-- | azalea/README.md | 2 | ||||
| -rw-r--r-- | azalea/src/client_impl/mining.rs | 7 | ||||
| -rw-r--r-- | azalea/src/client_impl/mod.rs | 11 | ||||
| -rw-r--r-- | azalea/src/entity_ref/mod.rs | 11 | ||||
| -rw-r--r-- | azalea/src/entity_ref/shared_impls.rs | 4 | ||||
| -rw-r--r-- | azalea/src/pathfinder/astar.rs | 4 |
7 files changed, 35 insertions, 8 deletions
diff --git a/azalea-client/src/local_player.rs b/azalea-client/src/local_player.rs index cc2a28dc..9e44913c 100644 --- a/azalea-client/src/local_player.rs +++ b/azalea-client/src/local_player.rs @@ -73,7 +73,7 @@ pub struct PermissionLevel(pub u8); #[derive(Clone, Component, Debug, Default, Deref, DerefMut, Resource)] pub struct TabList(HashMap<Uuid, PlayerInfo>); -#[derive(Clone, Component)] +#[derive(Clone, Component, Debug)] pub struct Hunger { /// The main hunger bar. This is typically in the range `0..=20`. pub food: u32, @@ -81,7 +81,7 @@ pub struct Hunger { /// /// This isn't displayed in the vanilla Minecraft GUI, but it's used /// internally by the game. It's a decrementing counter, and the player's - /// [`Hunger::food`] only starts decreasing when this reaches 0. + /// [`Hunger::food`] only starts decreasing when their saturation reaches 0. pub saturation: f32, } diff --git a/azalea/README.md b/azalea/README.md index 8a02e7da..eb066839 100644 --- a/azalea/README.md +++ b/azalea/README.md @@ -119,7 +119,7 @@ Backtraces are also useful, though they're sometimes hard to read and don't alwa ## Using `tokio::task::spawn_local` instead of `tokio::spawn` -If you spawn a task with `tokio::spawn` and move your bot into it, it's possible for Tokio to run the handler function or schedule a Minecraft tick at an unexpected moment. For instance, `bot.component::<TicksConnected>() == bot.component::<TicksConnected>()` is not guaranteed to be true inside of a `tokio::spawn`. Azalea already mitigates this in the handler function by using a Tokio [LocalSet](https://docs.rs/tokio/latest/tokio/task/struct.LocalSet.html), but that mitigation does not apply if you call `tokio::spawn` yourself. To avoid this, you must call `tokio::task::spawn_local` in place of `tokio::spawn`. Alternatively, you could also mark your main function with `#[tokio::main(flavor = "current_thread")]`. +If you spawn a task with `tokio::spawn` and move your bot into it, it's possible for Tokio to run the handler function or schedule a Minecraft tick at an unexpected moment. For instance, `bot.ticks_connected() == bot.ticks_connected()` is not guaranteed to be true inside of a `tokio::spawn`. Azalea already mitigates this in the handler function by using a Tokio [LocalSet](https://docs.rs/tokio/latest/tokio/task/struct.LocalSet.html), but that mitigation does not apply if you call `tokio::spawn` yourself. To avoid this, you must call `tokio::task::spawn_local` in place of `tokio::spawn`. Alternatively, you could also mark your main function with `#[tokio::main(flavor = "current_thread")]`. ## Disabling log messages diff --git a/azalea/src/client_impl/mining.rs b/azalea/src/client_impl/mining.rs index 980fc47f..11c74480 100644 --- a/azalea/src/client_impl/mining.rs +++ b/azalea/src/client_impl/mining.rs @@ -1,4 +1,4 @@ -use azalea_client::mining::{LeftClickMine, StartMiningBlockEvent}; +use azalea_client::mining::{LeftClickMine, Mining, StartMiningBlockEvent}; use azalea_core::position::BlockPos; use crate::Client; @@ -14,6 +14,11 @@ impl Client { }); } + /// Returns true if the client is currently trying to mine a block. + pub fn is_mining(&self) -> bool { + self.get_component::<Mining>().is_some() + } + /// When enabled, the bot will mine any block that it is looking at if it is /// reachable. pub fn left_click_mine(&self, enabled: bool) { diff --git a/azalea/src/client_impl/mod.rs b/azalea/src/client_impl/mod.rs index db9ecf91..dc4ab90f 100644 --- a/azalea/src/client_impl/mod.rs +++ b/azalea/src/client_impl/mod.rs @@ -11,6 +11,7 @@ use azalea_client::{ packet::game::SendGamePacketEvent, player::{GameProfileComponent, PlayerInfo}, start_ecs_runner, + tick_counter::TicksConnected, }; use azalea_core::data_registry::{DataRegistryWithKey, ResolvableDataRegistry}; use azalea_entity::indexing::{EntityIdIndex, EntityUuidIndex}; @@ -450,4 +451,14 @@ impl Client { .map(|(name, data)| f(name, data)) }) } + + /// Returns the number of ticks since the `login` packet was received, or 0 + /// if the client isn't in the world. + /// + /// This is a shortcut for getting the [`TicksConnected`] component. + pub fn ticks_connected(&self) -> u64 { + self.get_component::<TicksConnected>() + .map(|c| c.0) + .unwrap_or(0) + } } diff --git a/azalea/src/entity_ref/mod.rs b/azalea/src/entity_ref/mod.rs index 2f49d975..d604fe00 100644 --- a/azalea/src/entity_ref/mod.rs +++ b/azalea/src/entity_ref/mod.rs @@ -132,4 +132,15 @@ impl EntityRef { pub fn interact(&self) { self.client.entity_interact(self.entity); } + + /// Look at this entity from the client that created the `EntityRef`. + pub fn look_at(&self) { + self.client.look_at(self.eye_position()); + } + + /// Returns the distance between the client's feet position and this + /// entity's feet position. + pub fn distance_to_client(&self) -> f64 { + self.position().distance_to(self.client.position()) + } } diff --git a/azalea/src/entity_ref/shared_impls.rs b/azalea/src/entity_ref/shared_impls.rs index 9ef396a0..5e2255c5 100644 --- a/azalea/src/entity_ref/shared_impls.rs +++ b/azalea/src/entity_ref/shared_impls.rs @@ -87,11 +87,11 @@ impl_entity_functions! { } Client: - /// Get the health of this client. + /// Get the health of this client, typically in the range `0..=20`. /// /// This is a shortcut for `*bot.component::<Health>()`. EntityRef: - /// Get the health of this entity. + /// Get the health of this entity, typically in the range `0..=20`. /// /// Also see [`Client::health`]. pub fn health(&self) -> f32 { diff --git a/azalea/src/pathfinder/astar.rs b/azalea/src/pathfinder/astar.rs index 69151991..ea2d45e2 100644 --- a/azalea/src/pathfinder/astar.rs +++ b/azalea/src/pathfinder/astar.rs @@ -303,8 +303,8 @@ impl PathfinderHeap { pub fn push(&mut self, item: WeightedNode) { if let Some(top) = self.radix_heap.top() { - // this can happen when the heuristic isn't optimal, so just fall back to a - // binary heap in those cases + // this can happen when the heuristic wasn't an underestimate, so just fall back + // to a binary heap in those cases if item.f_score < f32::from_bits(top.0) { self.binary_heap.push(item); return; |
