From 832f9605319052e903fce56b401461e17954f316 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Mon, 22 Jul 2024 06:47:40 +0530 Subject: [PATCH] Added Left Click Mine (1.21) (#168) * Added Auto Mine * Unnecessary Block Reach Check * Added `LeftClickMine` --- azalea-client/src/mining.rs | 72 ++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index f0b86db19..54e27013e 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -33,7 +33,15 @@ impl Plugin for MinePlugin { .add_event::() .add_event::() .add_event::() - .add_systems(GameTick, continue_mining_block.before(PhysicsSet)) + .add_systems( + GameTick, + ( + continue_mining_block, + handle_auto_mine + ) + .chain() + .before(PhysicsSet) + ) .add_systems( Update, ( @@ -66,6 +74,68 @@ impl Client { position, }); } + + /// When enabled, the bot will mine any block that it is looking at if it is reachable. + pub fn left_click_mine(&self, enabled: bool) { + let mut ecs = self.ecs.lock(); + let mut entity_mut = ecs.entity_mut(self.entity); + + if enabled { + entity_mut.insert(LeftClickMine); + } else { + entity_mut.remove::(); + } + } +} + +#[derive(Component)] +pub struct LeftClickMine; + +#[allow(clippy::type_complexity)] +fn handle_auto_mine( + mut query: Query< + ( + &HitResultComponent, + Entity, + Option<&Mining>, + &InventoryComponent, + &MineBlockPos, + &MineItem, + ), + With, + >, + mut start_mining_block_event: EventWriter, + mut stop_mining_block_event: EventWriter +) { + for ( + hit_result_component, + entity, + mining, + inventory, + current_mining_pos, + current_mining_item, + ) in &mut query.iter_mut() + { + let block_pos = hit_result_component.block_pos; + + if (mining.is_none() + || !is_same_mining_target( + block_pos, + inventory, + current_mining_pos, + current_mining_item, + )) && !hit_result_component.miss + { + start_mining_block_event.send(StartMiningBlockEvent { + entity, + position: block_pos, + }); + } else if mining.is_some() && hit_result_component.miss { + stop_mining_block_event.send(StopMiningBlockEvent { + entity + }); + } + } } /// Information about the block we're currently mining. This is only present if