aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/bot.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-12-27 22:02:00 -0600
committerGitHub <noreply@github.com>2025-12-27 22:02:00 -0600
commit9513f42e87f64c409cdb2a100500a50e5a713bac (patch)
treebb6aa8b6d50fddf967bcb1f759e023754ea84e49 /azalea/src/bot.rs
parent588902ba4a3965982bdd84d92b20c6f7613f3978 (diff)
downloadazalea-drasl-9513f42e87f64c409cdb2a100500a50e5a713bac.tar.xz
Move Client struct to azalea crate (#297)
* move the Client struct out of azalea-client into azalea * actually add client impls in azalea
Diffstat (limited to 'azalea/src/bot.rs')
-rw-r--r--azalea/src/bot.rs103
1 files changed, 22 insertions, 81 deletions
diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs
index 33293466..6f39c2fe 100644
--- a/azalea/src/bot.rs
+++ b/azalea/src/bot.rs
@@ -1,9 +1,6 @@
use std::f64::consts::PI;
-use azalea_client::{
- mining::Mining,
- tick_broadcast::{TickBroadcast, UpdateBroadcast},
-};
+use azalea_client::mining::Mining;
use azalea_core::{
position::{BlockPos, Vec3},
tick::GameTick,
@@ -15,21 +12,17 @@ use azalea_entity::{
use azalea_physics::PhysicsSystems;
use bevy_app::Update;
use bevy_ecs::prelude::*;
-use futures_lite::Future;
use tracing::trace;
use crate::{
- accept_resource_packs::AcceptResourcePacksPlugin,
+ Client,
app::{App, Plugin, PluginGroup, PluginGroupBuilder},
- auto_respawn::AutoRespawnPlugin,
- container::ContainerPlugin,
ecs::{
component::Component,
entity::Entity,
query::{With, Without},
system::{Commands, Query},
},
- pathfinder::PathfinderPlugin,
};
#[derive(Clone, Default)]
@@ -86,41 +79,19 @@ fn stop_jumping(mut query: Query<(&mut Jumping, &mut Bot)>) {
}
}
-/// A trait that adds a few additional functions to
-/// [`Client`](azalea_client::Client) that help with making bots.
-pub trait BotClientExt {
+impl Client {
/// Queue a jump for the next tick.
- fn jump(&self);
- /// Turn the bot's head to look at the coordinate in the world.
- ///
- /// To look at the center of a block, you should call [`BlockPos::center`].
- 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 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`].
- ///
- /// [`look_at`]: crate::prelude::BotClientExt::look_at
- fn mine(&self, position: BlockPos) -> impl Future<Output = ()> + Send;
-}
-
-impl BotClientExt for azalea_client::Client {
- fn jump(&self) {
+ pub fn jump(&self) {
let mut ecs = self.ecs.lock();
ecs.write_message(JumpEvent {
entity: self.entity,
});
}
- fn look_at(&self, position: Vec3) {
+ /// Turn the bot's head to look at the coordinate in the world.
+ ///
+ /// To look at the center of a block, you should call [`BlockPos::center`].
+ pub fn look_at(&self, position: Vec3) {
let mut ecs = self.ecs.lock();
ecs.write_message(LookAtEvent {
entity: self.entity,
@@ -128,50 +99,13 @@ impl BotClientExt for azalea_client::Client {
});
}
- /// Returns a Receiver that receives a message every game tick.
- ///
- /// This is useful if you want to efficiently loop until a certain condition
- /// is met.
- ///
- /// ```
- /// # use azalea::prelude::*;
- /// # use azalea::container::WaitingForInventoryOpen;
- /// # async fn example(bot: &mut azalea::Client) {
- /// let mut ticks = bot.get_tick_broadcaster();
- /// while ticks.recv().await.is_ok() {
- /// let ecs = bot.ecs.lock();
- /// if ecs.get::<WaitingForInventoryOpen>(bot.entity).is_none() {
- /// break;
- /// }
- /// }
- /// # }
- /// ```
- fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()> {
- let ecs = self.ecs.lock();
- let tick_broadcast = ecs.resource::<TickBroadcast>();
- 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 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 to avoid accidentally skipping
/// ticks and having to wait longer.
- async fn wait_ticks(&self, n: usize) {
+ pub async fn wait_ticks(&self, n: usize) {
let mut receiver = self.get_tick_broadcaster();
for _ in 0..n {
let _ = receiver.recv().await;
@@ -186,14 +120,20 @@ impl BotClientExt for azalea_client::Client {
/// If you're going to run this in a loop, you may want to use that function
/// instead and use the `Receiver` from it to avoid accidentally skipping
/// ticks and having to wait longer.
- async fn wait_updates(&self, n: usize) {
+ pub async fn wait_updates(&self, n: usize) {
let mut receiver = self.get_update_broadcaster();
for _ in 0..n {
let _ = receiver.recv().await;
}
}
- async fn mine(&self, position: BlockPos) {
+ /// 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`].
+ ///
+ /// [`look_at`]: crate::prelude::BotClientExt::look_at
+ pub async fn mine(&self, position: BlockPos) {
self.start_mining(position);
let mut receiver = self.get_tick_broadcaster();
@@ -266,9 +206,10 @@ impl PluginGroup for DefaultBotPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(BotPlugin)
- .add(PathfinderPlugin)
- .add(ContainerPlugin)
- .add(AutoRespawnPlugin)
- .add(AcceptResourcePacksPlugin)
+ .add(crate::pathfinder::PathfinderPlugin)
+ .add(crate::container::ContainerPlugin)
+ .add(crate::auto_respawn::AutoRespawnPlugin)
+ .add(crate::accept_resource_packs::AcceptResourcePacksPlugin)
+ .add(crate::tick_broadcast::TickBroadcastPlugin)
}
}