1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16: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_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_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::<StartMiningBlockEvent>()
@ -33,7 +35,15 @@ impl Plugin for MinePlugin {
.add_event::<StopMiningBlockEvent>()
.add_event::<MineBlockProgressEvent>()
.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(
Update,
(
@ -66,6 +76,69 @@ 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::<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
@ -85,6 +158,7 @@ pub struct StartMiningBlockEvent {
pub entity: Entity,
pub position: BlockPos,
}
fn handle_start_mining_block_event(
mut events: EventReader<StartMiningBlockEvent>,
mut start_mining_events: EventWriter<StartMiningBlockWithDirectionEvent>,
@ -113,6 +187,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<StartMiningBlockWithDirectionEvent>,
@ -175,11 +250,11 @@ fn handle_start_mining_block_with_direction_event(
**mine_delay = 5;
} else if mining.is_none()
|| !is_same_mining_target(
event.position,
inventory,
&current_mining_pos,
&current_mining_item,
)
event.position,
inventory,
&current_mining_pos,
&current_mining_item,
)
{
if mining.is_some() {
// send a packet to stop mining since we just changed target
@ -192,7 +267,7 @@ fn handle_start_mining_block_with_direction_event(
direction: event.direction,
sequence: 0,
}
.get(),
.get(),
});
}
@ -228,12 +303,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 +339,7 @@ fn handle_start_mining_block_with_direction_event(
direction: event.direction,
sequence: **sequence_number,
}
.get(),
.get(),
});
}
}
@ -414,6 +489,7 @@ fn handle_finish_mining_block_event(
pub struct StopMiningBlockEvent {
pub entity: Entity,
}
fn handle_stop_mining_block_event(
mut events: EventReader<StopMiningBlockEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
@ -434,7 +510,7 @@ fn handle_stop_mining_block_event(
direction: Direction::Down,
sequence: 0,
}
.get(),
.get(),
});
commands.entity(event.entity).remove::<Mining>();
**mine_progress = 0.;
@ -508,7 +584,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 +630,7 @@ fn continue_mining_block(
direction: mining.dir,
sequence: **sequence_number,
}
.get(),
.get(),
});
**mine_progress = 0.;
**mine_ticks = 0.;