From 7fb6fe6087c6f34a48fe2069dc7ea8b025898bf4 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Mon, 17 Jun 2024 12:48:02 +0530 Subject: [PATCH 1/5] Added Auto Mine --- azalea-client/src/mining.rs | 109 ++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 18 deletions(-) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index f0b86db19..81d7c2f4f 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -1,6 +1,6 @@ use azalea_block::{Block, BlockState, FluidState}; use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick}; -use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics}; +use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics, Position, LocalEntity}; use azalea_inventory::ItemSlot; use azalea_physics::PhysicsSet; use azalea_protocol::packets::game::serverbound_player_action_packet::{ @@ -10,6 +10,7 @@ use azalea_world::{InstanceContainer, InstanceName}; use bevy_app::{App, Plugin, Update}; use bevy_ecs::prelude::*; use derive_more::{Deref, DerefMut}; +use azalea_entity::metadata::Player; use crate::{ interact::{ @@ -25,6 +26,7 @@ use crate::{ /// A plugin that allows clients to break blocks in the world. pub struct MinePlugin; + impl Plugin for MinePlugin { fn build(&self, app: &mut App) { app.add_event::() @@ -33,7 +35,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 +76,66 @@ impl Client { position, }); } + + /// When enabled, the bot will mine any block that it is looking at if it is reachable. + /// By default, reachability is set to 5 blocks. + pub fn auto_mine(&self, enabled: bool) { + let mut ecs = self.ecs.lock(); + let mut entity_mut = ecs.entity_mut(self.entity); + + if enabled { + entity_mut.insert(AutoMine); + } else { + entity_mut.remove::(); + } + } +} + +#[derive(Component)] +pub struct AutoMine; + +fn handle_auto_mine( + mut query: Query< + ( + &HitResultComponent, + &Position, + Entity, + Option<&Mining>, + &InventoryComponent, + &MineBlockPos, + &MineItem, + ), + (With, With, With), + >, + mut start_mining_block_event_writer: EventWriter, +) { + for ( + hit_result_component, + position, + 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, + )) + && position.distance_to(&block_pos.to_vec3_floored()) <= 7.0 + { + start_mining_block_event_writer.send(StartMiningBlockEvent { + entity, + position: block_pos, + }); + } + } } /// Information about the block we're currently mining. This is only present if @@ -85,6 +155,7 @@ pub struct StartMiningBlockEvent { pub entity: Entity, pub position: BlockPos, } + fn handle_start_mining_block_event( mut events: EventReader, mut start_mining_events: EventWriter, @@ -113,6 +184,7 @@ pub struct StartMiningBlockWithDirectionEvent { pub position: BlockPos, pub direction: Direction, } + #[allow(clippy::too_many_arguments, clippy::type_complexity)] fn handle_start_mining_block_with_direction_event( mut events: EventReader, @@ -175,11 +247,11 @@ fn handle_start_mining_block_with_direction_event( **mine_delay = 5; } else if mining.is_none() || !is_same_mining_target( - event.position, - inventory, - ¤t_mining_pos, - ¤t_mining_item, - ) + event.position, + inventory, + ¤t_mining_pos, + ¤t_mining_item, + ) { if mining.is_some() { // send a packet to stop mining since we just changed target @@ -192,7 +264,7 @@ fn handle_start_mining_block_with_direction_event( direction: event.direction, sequence: 0, } - .get(), + .get(), }); } @@ -228,12 +300,12 @@ fn handle_start_mining_block_with_direction_event( if block_is_solid && get_mine_progress( - block.as_ref(), - held_item.kind(), - &inventory.inventory_menu, - fluid_on_eyes, - physics, - ) >= 1. + block.as_ref(), + held_item.kind(), + &inventory.inventory_menu, + fluid_on_eyes, + physics, + ) >= 1. { // block was broken instantly finish_mining_events.send(FinishMiningBlockEvent { @@ -264,7 +336,7 @@ fn handle_start_mining_block_with_direction_event( direction: event.direction, sequence: **sequence_number, } - .get(), + .get(), }); } } @@ -414,6 +486,7 @@ fn handle_finish_mining_block_event( pub struct StopMiningBlockEvent { pub entity: Entity, } + fn handle_stop_mining_block_event( mut events: EventReader, mut send_packet_events: EventWriter, @@ -434,7 +507,7 @@ fn handle_stop_mining_block_event( direction: Direction::Down, sequence: 0, } - .get(), + .get(), }); commands.entity(event.entity).remove::(); **mine_progress = 0.; @@ -508,7 +581,7 @@ fn continue_mining_block( direction: mining.dir, sequence: **sequence_number, } - .get(), + .get(), }); swing_arm_events.send(SwingArmEvent { entity }); } else if is_same_mining_target( @@ -554,7 +627,7 @@ fn continue_mining_block( direction: mining.dir, sequence: **sequence_number, } - .get(), + .get(), }); **mine_progress = 0.; **mine_ticks = 0.; From 7a71cb6c0405d65ffb0028ec107b8e476b8bf1fe Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sat, 20 Jul 2024 11:52:44 +0530 Subject: [PATCH 2/5] Removed unnecessary Block Reach Check --- azalea-client/src/mining.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index 81d7c2f4f..d39d785f5 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -1,6 +1,6 @@ use azalea_block::{Block, BlockState, FluidState}; use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick}; -use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics, Position, LocalEntity}; +use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics, LocalEntity}; use azalea_inventory::ItemSlot; use azalea_physics::PhysicsSet; use azalea_protocol::packets::game::serverbound_player_action_packet::{ @@ -98,7 +98,6 @@ fn handle_auto_mine( mut query: Query< ( &HitResultComponent, - &Position, Entity, Option<&Mining>, &InventoryComponent, @@ -111,7 +110,6 @@ fn handle_auto_mine( ) { for ( hit_result_component, - position, entity, mining, inventory, @@ -121,14 +119,13 @@ fn handle_auto_mine( { let block_pos = hit_result_component.block_pos; - if (mining.is_none() + if mining.is_none() || !is_same_mining_target( block_pos, inventory, current_mining_pos, current_mining_item, - )) - && position.distance_to(&block_pos.to_vec3_floored()) <= 7.0 + ) { start_mining_block_event_writer.send(StartMiningBlockEvent { entity, From bcfdeaa463e26eba6f6a18283f0f73f549a53e40 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sat, 20 Jul 2024 13:01:11 +0530 Subject: [PATCH 3/5] Added `hit_result_component.miss` as the condition to send mining event and also send stop mining event if mining a block that is not reachable. --- azalea-client/src/mining.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index d39d785f5..5c6a03f57 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -106,7 +106,8 @@ fn handle_auto_mine( ), (With, With, With), >, - mut start_mining_block_event_writer: EventWriter, + mut start_mining_block_event: EventWriter, + mut stop_mining_block_event: EventWriter ) { for ( hit_result_component, @@ -119,18 +120,23 @@ fn handle_auto_mine( { let block_pos = hit_result_component.block_pos; - if mining.is_none() + 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_writer.send(StartMiningBlockEvent { + start_mining_block_event.send(StartMiningBlockEvent { entity, position: block_pos, }); + } else if mining.is_some() && hit_result_component.miss { + // Stop mining as the block is not reachable + stop_mining_block_event.send(StopMiningBlockEvent { + entity + }); } } } From ee256a249d187a6adbfadaca85930d7c9b0ac1e5 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sat, 20 Jul 2024 13:08:22 +0530 Subject: [PATCH 4/5] clippy allow type complexity --- azalea-client/src/mining.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index 5c6a03f57..26d688e3a 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -94,6 +94,7 @@ impl Client { #[derive(Component)] pub struct AutoMine; +#[allow(clippy::type_complexity)] fn handle_auto_mine( mut query: Query< ( From 0611dbd1bfbe4e12d92a5ab399bc781b350561a1 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sat, 20 Jul 2024 15:18:17 +0530 Subject: [PATCH 5/5] Changed name to `LeftClickMine` --- azalea-client/src/mining.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/azalea-client/src/mining.rs b/azalea-client/src/mining.rs index 26d688e3a..6e4b58f37 100644 --- a/azalea-client/src/mining.rs +++ b/azalea-client/src/mining.rs @@ -39,7 +39,7 @@ impl Plugin for MinePlugin { GameTick, ( continue_mining_block, - handle_auto_mine + handle_left_click_mine ) .chain() .before(PhysicsSet), @@ -78,24 +78,23 @@ impl Client { } /// When enabled, the bot will mine any block that it is looking at if it is reachable. - /// By default, reachability is set to 5 blocks. - pub fn auto_mine(&self, enabled: bool) { + 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(AutoMine); + entity_mut.insert(LeftClickMine); } else { - entity_mut.remove::(); + entity_mut.remove::(); } } } #[derive(Component)] -pub struct AutoMine; +pub struct LeftClickMine; #[allow(clippy::type_complexity)] -fn handle_auto_mine( +fn handle_left_click_mine( mut query: Query< ( &HitResultComponent, @@ -105,7 +104,7 @@ fn handle_auto_mine( &MineBlockPos, &MineItem, ), - (With, With, With), + (With, With, With), >, mut start_mining_block_event: EventWriter, mut stop_mining_block_event: EventWriter