aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/bot.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-07-15 04:39:43 -0500
committerGitHub <noreply@github.com>2023-07-15 04:39:43 -0500
commitcde7e35046b726b07bf3e067c080b85a12b2fd74 (patch)
tree9d517911cbaf14f007958a92392101f24ec14118 /azalea/src/bot.rs
parent148f20381750be3e2c38a6bdaf8d339113da1b39 (diff)
downloadazalea-drasl-cde7e35046b726b07bf3e067c080b85a12b2fd74.tar.xz
Attacking (#96)
* add Client::attack * partially implement attack cooldowns * attack speed modifiers * don't care clippy --------- Co-authored-by: mat <git@matdoes.dev>
Diffstat (limited to 'azalea/src/bot.rs')
-rw-r--r--azalea/src/bot.rs50
1 files changed, 47 insertions, 3 deletions
diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs
index 7940e7b0..71d96c4d 100644
--- a/azalea/src/bot.rs
+++ b/azalea/src/bot.rs
@@ -8,7 +8,10 @@ use crate::ecs::{
query::{With, Without},
system::{Commands, Query},
};
-use azalea_core::Vec3;
+use azalea_client::interact::SwingArmEvent;
+use azalea_client::mining::Mining;
+use azalea_client::TickBroadcast;
+use azalea_core::{BlockPos, Vec3};
use azalea_entity::{
clamp_look_direction, metadata::Player, EyeHeight, Jumping, Local, LookDirection, Position,
};
@@ -67,18 +70,25 @@ fn stop_jumping(mut query: Query<(&mut Jumping, &mut Bot)>) {
}
pub trait BotClientExt {
+ /// Queue a jump for the next tick.
fn jump(&mut self);
+ /// Turn the bot's head to look at the coordinate in the world.
fn look_at(&mut self, pos: Vec3);
+ /// Get a receiver that will receive a message every tick.
+ fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()>;
+ /// 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
+ async fn mine(&mut self, position: BlockPos);
}
impl BotClientExt for azalea_client::Client {
- /// Queue a jump for the next tick.
fn jump(&mut self) {
let mut ecs = self.ecs.lock();
ecs.send_event(JumpEvent(self.entity));
}
- /// Turn the bot's head to look at the coordinate in the world.
fn look_at(&mut self, position: Vec3) {
let mut ecs = self.ecs.lock();
ecs.send_event(LookAtEvent {
@@ -86,6 +96,40 @@ impl BotClientExt for azalea_client::Client {
position,
});
}
+
+ /// ```
+ /// # use azalea::prelude::*;
+ /// # async fn example(mut bot: azalea::Client) {
+ /// let mut ticks = self.get_tick_broadcaster();
+ /// while ticks.recv().await.is_ok() {
+ /// let ecs = bot.ecs.lock();
+ /// if ecs.get::<WaitingForInventoryOpen>(self.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()
+ }
+
+ async fn mine(&mut self, position: BlockPos) {
+ self.start_mining(position);
+ // vanilla sends an extra swing arm packet when we start mining
+ self.ecs.lock().send_event(SwingArmEvent {
+ entity: self.entity,
+ });
+
+ let mut receiver = self.get_tick_broadcaster();
+ while receiver.recv().await.is_ok() {
+ let ecs = self.ecs.lock();
+ if ecs.get::<Mining>(self.entity).is_none() {
+ break;
+ }
+ }
+ }
}
/// Event to jump once.