1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

Added Left Click Mine Functionality (#156)

* Added Auto Mine

* Removed unnecessary Block Reach Check

* 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.

* clippy allow type complexity

* Changed name to `LeftClickMine`
This commit is contained in:
Aditya Kumar 2024-07-22 06:48:51 +05:30 committed by GitHub
parent 24c5cb80aa
commit f35ba028f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
use azalea_block::{Block, BlockState, FluidState}; use azalea_block::{Block, BlockState, FluidState};
use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick}; 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, LocalEntity};
use azalea_inventory::ItemSlot; use azalea_inventory::ItemSlot;
use azalea_physics::PhysicsSet; use azalea_physics::PhysicsSet;
use azalea_protocol::packets::game::serverbound_player_action_packet::{ 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_app::{App, Plugin, Update};
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use azalea_entity::metadata::Player;
use crate::{ use crate::{
interact::{ interact::{
@ -25,6 +26,7 @@ use crate::{
/// A plugin that allows clients to break blocks in the world. /// A plugin that allows clients to break blocks in the world.
pub struct MinePlugin; pub struct MinePlugin;
impl Plugin for MinePlugin { impl Plugin for MinePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<StartMiningBlockEvent>() app.add_event::<StartMiningBlockEvent>()
@ -33,7 +35,15 @@ impl Plugin for MinePlugin {
.add_event::<StopMiningBlockEvent>() .add_event::<StopMiningBlockEvent>()
.add_event::<MineBlockProgressEvent>() .add_event::<MineBlockProgressEvent>()
.add_event::<AttackBlockEvent>() .add_event::<AttackBlockEvent>()
.add_systems(GameTick, continue_mining_block.before(PhysicsSet)) .add_systems(
GameTick,
(
continue_mining_block,
handle_left_click_mine
)
.chain()
.before(PhysicsSet),
)
.add_systems( .add_systems(
Update, Update,
( (
@ -66,6 +76,69 @@ impl Client {
position, 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::<LeftClickMine>();
}
}
}
#[derive(Component)]
pub struct LeftClickMine;
#[allow(clippy::type_complexity)]
fn handle_left_click_mine(
mut query: Query<
(
&HitResultComponent,
Entity,
Option<&Mining>,
&InventoryComponent,
&MineBlockPos,
&MineItem,
),
(With<LeftClickMine>, With<Player>, With<LocalEntity>),
>,
mut start_mining_block_event: EventWriter<StartMiningBlockEvent>,
mut stop_mining_block_event: EventWriter<StopMiningBlockEvent>
) {
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 as the block is not reachable
stop_mining_block_event.send(StopMiningBlockEvent {
entity
});
}
}
} }
/// Information about the block we're currently mining. This is only present if /// Information about the block we're currently mining. This is only present if
@ -85,6 +158,7 @@ pub struct StartMiningBlockEvent {
pub entity: Entity, pub entity: Entity,
pub position: BlockPos, pub position: BlockPos,
} }
fn handle_start_mining_block_event( fn handle_start_mining_block_event(
mut events: EventReader<StartMiningBlockEvent>, mut events: EventReader<StartMiningBlockEvent>,
mut start_mining_events: EventWriter<StartMiningBlockWithDirectionEvent>, mut start_mining_events: EventWriter<StartMiningBlockWithDirectionEvent>,
@ -113,6 +187,7 @@ pub struct StartMiningBlockWithDirectionEvent {
pub position: BlockPos, pub position: BlockPos,
pub direction: Direction, pub direction: Direction,
} }
#[allow(clippy::too_many_arguments, clippy::type_complexity)] #[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn handle_start_mining_block_with_direction_event( fn handle_start_mining_block_with_direction_event(
mut events: EventReader<StartMiningBlockWithDirectionEvent>, mut events: EventReader<StartMiningBlockWithDirectionEvent>,
@ -414,6 +489,7 @@ fn handle_finish_mining_block_event(
pub struct StopMiningBlockEvent { pub struct StopMiningBlockEvent {
pub entity: Entity, pub entity: Entity,
} }
fn handle_stop_mining_block_event( fn handle_stop_mining_block_event(
mut events: EventReader<StopMiningBlockEvent>, mut events: EventReader<StopMiningBlockEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>, mut send_packet_events: EventWriter<SendPacketEvent>,