aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/bot.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-04-15 22:04:43 -0430
committermat <git@matdoes.dev>2025-04-15 22:04:43 -0430
commita9820dfd79bf24a0a6fcb2345aad6c79a21585a5 (patch)
treea8e6290707fee0e1b18812aba599da74e7fc6eed /azalea/src/bot.rs
parent1a0c4e2de9e6d82d5efdfac2bab17a73c79fc466 (diff)
downloadazalea-drasl-a9820dfd79bf24a0a6fcb2345aad6c79a21585a5.tar.xz
make goto async and clean up some examples
Diffstat (limited to 'azalea/src/bot.rs')
-rw-r--r--azalea/src/bot.rs40
1 files changed, 39 insertions, 1 deletions
diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs
index 514cea1e..dd8e6209 100644
--- a/azalea/src/bot.rs
+++ b/azalea/src/bot.rs
@@ -1,8 +1,8 @@
use std::f64::consts::PI;
-use azalea_client::TickBroadcast;
use azalea_client::interact::SwingArmEvent;
use azalea_client::mining::Mining;
+use azalea_client::tick_broadcast::{TickBroadcast, UpdateBroadcast};
use azalea_core::position::{BlockPos, Vec3};
use azalea_core::tick::GameTick;
use azalea_entity::{
@@ -86,6 +86,12 @@ pub trait BotClientExt {
fn look_at(&self, pos: Vec3);
/// Get a receiver that will receive a message every tick.
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;
/// 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`].
///
@@ -133,6 +139,38 @@ impl BotClientExt for azalea_client::Client {
tick_broadcast.subscribe()
}
+ /// Returns a Receiver that receives a message every ECS Update.
+ ///
+ /// ECS Updates happen at least at the frequency of game ticks, usually
+ /// faster.
+ ///
+ /// This is useful if you're sending an ECS event and want to make sure it's
+ /// been handled before continuing.
+ fn get_update_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()> {
+ let ecs = self.ecs.lock();
+ let update_broadcast = ecs.resource::<UpdateBroadcast>();
+ update_broadcast.subscribe()
+ }
+
+ /// Wait for one tick 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) {
+ let mut receiver = self.get_tick_broadcaster();
+ // wait for the next tick
+ let _ = receiver.recv().await;
+ }
+ /// Waits for one ECS Update using [`Self::get_update_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_update(&self) {
+ let mut receiver = self.get_update_broadcaster();
+ // wait for the next tick
+ let _ = receiver.recv().await;
+ }
+
async fn mine(&self, position: BlockPos) {
self.start_mining(position);
// vanilla sends an extra swing arm packet when we start mining