From cde7e35046b726b07bf3e067c080b85a12b2fd74 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 15 Jul 2023 04:39:43 -0500 Subject: Attacking (#96) * add Client::attack * partially implement attack cooldowns * attack speed modifiers * don't care clippy --------- Co-authored-by: mat --- azalea/src/auto_respawn.rs | 1 - azalea/src/bot.rs | 50 +++++++++++++++++++++++++++++++++++++++++++--- azalea/src/container.rs | 10 ++++------ azalea/src/lib.rs | 1 - azalea/src/mining.rs | 40 ------------------------------------- azalea/src/prelude.rs | 4 ++-- 6 files changed, 53 insertions(+), 53 deletions(-) delete mode 100644 azalea/src/mining.rs (limited to 'azalea/src') diff --git a/azalea/src/auto_respawn.rs b/azalea/src/auto_respawn.rs index 6d9c5954..77a75b4b 100644 --- a/azalea/src/auto_respawn.rs +++ b/azalea/src/auto_respawn.rs @@ -21,6 +21,5 @@ fn auto_respawn( perform_respawn_events.send(PerformRespawnEvent { entity: event.entity, }); - println!("auto respawning"); } } 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::(self.entity).is_none() { + /// break; + /// } + /// } + /// # } + /// ``` + fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()> { + let ecs = self.ecs.lock(); + let tick_broadcast = ecs.resource::(); + 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::(self.entity).is_none() { + break; + } + } + } } /// Event to jump once. diff --git a/azalea/src/container.rs b/azalea/src/container.rs index dc0ba169..2261469d 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -3,7 +3,7 @@ use std::fmt::Formatter; use azalea_client::{ inventory::{CloseContainerEvent, ContainerClickEvent, InventoryComponent}, packet_handling::PacketEvent, - Client, TickBroadcast, + Client, }; use azalea_core::BlockPos; use azalea_inventory::{operations::ClickOperation, ItemSlot, Menu}; @@ -12,6 +12,8 @@ use bevy_app::{App, Plugin, Update}; use bevy_ecs::{component::Component, prelude::EventReader, system::Commands}; use std::fmt::Debug; +use crate::bot::BotClientExt; + pub struct ContainerPlugin; impl Plugin for ContainerPlugin { fn build(&self, app: &mut App) { @@ -49,11 +51,7 @@ impl ContainerClientExt for Client { .insert(WaitingForInventoryOpen); self.block_interact(pos); - let mut receiver = { - let ecs = self.ecs.lock(); - let tick_broadcast = ecs.resource::(); - tick_broadcast.subscribe() - }; + let mut receiver = self.get_tick_broadcaster(); while receiver.recv().await.is_ok() { let ecs = self.ecs.lock(); if ecs.get::(self.entity).is_none() { diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 297199a0..0aff2a56 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -6,7 +6,6 @@ mod auto_respawn; mod bot; mod container; -pub mod mining; pub mod pathfinder; pub mod prelude; pub mod swarm; diff --git a/azalea/src/mining.rs b/azalea/src/mining.rs deleted file mode 100644 index 8ba16436..00000000 --- a/azalea/src/mining.rs +++ /dev/null @@ -1,40 +0,0 @@ -use azalea_client::{ - interact::SwingArmEvent, - mining::{Mining, StartMiningBlockEvent}, - Client, TickBroadcast, -}; -use azalea_core::BlockPos; - -pub trait MiningExt { - /// Start mining a block. - async fn mine(&mut self, position: BlockPos); -} - -impl MiningExt for Client { - /// Start mining a block. This won't turn the bot's head towards the block, - /// so you'll have to do that yourself with [`look_at`]. - /// - /// [`look_at`]: crate::prelude::BotClientExt::look_at - async fn mine(&mut self, position: BlockPos) { - self.ecs.lock().send_event(StartMiningBlockEvent { - entity: self.entity, - position, - }); - // vanilla sends an extra swing arm packet when we start mining - self.ecs.lock().send_event(SwingArmEvent { - entity: self.entity, - }); - - let mut receiver = { - let ecs = self.ecs.lock(); - let tick_broadcast = ecs.resource::(); - tick_broadcast.subscribe() - }; - while receiver.recv().await.is_ok() { - let ecs = self.ecs.lock(); - if ecs.get::(self.entity).is_none() { - break; - } - } - } -} diff --git a/azalea/src/prelude.rs b/azalea/src/prelude.rs index ff3c11de..87cb0b53 100644 --- a/azalea/src/prelude.rs +++ b/azalea/src/prelude.rs @@ -2,8 +2,8 @@ //! re-exported here. pub use crate::{ - bot::BotClientExt, container::ContainerClientExt, mining::MiningExt, - pathfinder::PathfinderClientExt, ClientBuilder, + bot::BotClientExt, container::ContainerClientExt, pathfinder::PathfinderClientExt, + ClientBuilder, }; pub use azalea_client::{Account, Client, Event}; // this is necessary to make the macros that reference bevy_ecs work -- cgit v1.2.3