diff options
| author | mat <git@matdoes.dev> | 2025-06-03 03:48:36 +0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-06-03 03:48:36 +0500 |
| commit | abf995a70245028f9cc860ee231dc671f14adfcc (patch) | |
| tree | 3e6052c028c409e4bc7279dbd2e6bcc6bed63748 /azalea/src/bot.rs | |
| parent | 04dd6dd0a44faa760906bd60c9861a1b06a664a1 (diff) | |
| download | azalea-drasl-abf995a70245028f9cc860ee231dc671f14adfcc.tar.xz | |
replace wait_one_tick with wait_ticks and some other api improvements
Diffstat (limited to 'azalea/src/bot.rs')
| -rw-r--r-- | azalea/src/bot.rs | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs index 8bc9d594..9e8566bf 100644 --- a/azalea/src/bot.rs +++ b/azalea/src/bot.rs @@ -92,10 +92,10 @@ pub trait BotClientExt { fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()>; /// Get a receiver that will receive a message every ECS Update. fn get_update_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()>; - /// Wait for one tick. - fn wait_one_tick(&self) -> impl Future<Output = ()> + Send; - /// Wait for one ECS Update. - fn wait_one_update(&self) -> impl Future<Output = ()> + Send; + /// Wait for the specified number of game ticks. + fn wait_ticks(&self, n: usize) -> impl Future<Output = ()> + Send; + /// Wait for the specified number of ECS `Update`s. + fn wait_updates(&self, n: usize) -> impl Future<Output = ()> + Send; /// Mine a block. This won't turn the bot's head towards the block, so if /// that's necessary you'll have to do that yourself with [`look_at`]. /// @@ -156,23 +156,32 @@ impl BotClientExt for azalea_client::Client { update_broadcast.subscribe() } - /// Wait for one tick using [`Self::get_tick_broadcaster`]. + /// Wait for the specified number of ticks using + /// [`Self::get_tick_broadcaster`]. /// /// If you're going to run this in a loop, you may want to use that function - /// instead and use the `Receiver` from it as it'll be more efficient. - async fn wait_one_tick(&self) { + /// instead and use the `Receiver` from it to avoid accidentally skipping + /// ticks and having to wait longer. + async fn wait_ticks(&self, n: usize) { let mut receiver = self.get_tick_broadcaster(); - // wait for the next tick - let _ = receiver.recv().await; + for _ in 0..n { + let _ = receiver.recv().await; + } } - /// Waits for one ECS Update using [`Self::get_update_broadcaster`]. + /// Waits for the specified number of ECS `Update`s using + /// [`Self::get_update_broadcaster`]. + /// + /// These are basically equivalent to frames because even though we have no + /// rendering, some game mechanics depend on frames. /// /// If you're going to run this in a loop, you may want to use that function - /// instead and use the `Receiver` from it as it'll be more efficient. - async fn wait_one_update(&self) { + /// instead and use the `Receiver` from it to avoid accidentally skipping + /// ticks and having to wait longer. + async fn wait_updates(&self, n: usize) { let mut receiver = self.get_update_broadcaster(); - // wait for the next tick - let _ = receiver.recv().await; + for _ in 0..n { + let _ = receiver.recv().await; + } } async fn mine(&self, position: BlockPos) { @@ -221,10 +230,7 @@ fn look_at_listener( if let Ok((position, eye_height, mut look_direction)) = query.get_mut(event.entity) { let new_look_direction = direction_looking_at(&position.up(eye_height.into()), &event.position); - trace!( - "look at {:?} (currently at {:?})", - event.position, **position - ); + trace!("look at {} (currently at {})", event.position, **position); *look_direction = new_look_direction; } } |
