diff options
Diffstat (limited to 'azalea/src/mining.rs')
| -rw-r--r-- | azalea/src/mining.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/azalea/src/mining.rs b/azalea/src/mining.rs new file mode 100644 index 00000000..8ba16436 --- /dev/null +++ b/azalea/src/mining.rs @@ -0,0 +1,40 @@ +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::<TickBroadcast>(); + tick_broadcast.subscribe() + }; + while receiver.recv().await.is_ok() { + let ecs = self.ecs.lock(); + if ecs.get::<Mining>(self.entity).is_none() { + break; + } + } + } +} |
