Skip to content

Commit f35ba02

Browse files
authored
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`
1 parent 24c5cb8 commit f35ba02

File tree

1 file changed

+94
-18
lines changed

1 file changed

+94
-18
lines changed

azalea-client/src/mining.rs

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use azalea_block::{Block, BlockState, FluidState};
22
use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick};
3-
use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics};
3+
use azalea_entity::{mining::get_mine_progress, FluidOnEyes, Physics, LocalEntity};
44
use azalea_inventory::ItemSlot;
55
use azalea_physics::PhysicsSet;
66
use azalea_protocol::packets::game::serverbound_player_action_packet::{
@@ -10,6 +10,7 @@ use azalea_world::{InstanceContainer, InstanceName};
1010
use bevy_app::{App, Plugin, Update};
1111
use bevy_ecs::prelude::*;
1212
use derive_more::{Deref, DerefMut};
13+
use azalea_entity::metadata::Player;
1314

1415
use crate::{
1516
interact::{
@@ -25,6 +26,7 @@ use crate::{
2526

2627
/// A plugin that allows clients to break blocks in the world.
2728
pub struct MinePlugin;
29+
2830
impl Plugin for MinePlugin {
2931
fn build(&self, app: &mut App) {
3032
app.add_event::<StartMiningBlockEvent>()
@@ -33,7 +35,15 @@ impl Plugin for MinePlugin {
3335
.add_event::<StopMiningBlockEvent>()
3436
.add_event::<MineBlockProgressEvent>()
3537
.add_event::<AttackBlockEvent>()
36-
.add_systems(GameTick, continue_mining_block.before(PhysicsSet))
38+
.add_systems(
39+
GameTick,
40+
(
41+
continue_mining_block,
42+
handle_left_click_mine
43+
)
44+
.chain()
45+
.before(PhysicsSet),
46+
)
3747
.add_systems(
3848
Update,
3949
(
@@ -66,6 +76,69 @@ impl Client {
6676
position,
6777
});
6878
}
79+
80+
/// When enabled, the bot will mine any block that it is looking at if it is reachable.
81+
pub fn left_click_mine(&self, enabled: bool) {
82+
let mut ecs = self.ecs.lock();
83+
let mut entity_mut = ecs.entity_mut(self.entity);
84+
85+
if enabled {
86+
entity_mut.insert(LeftClickMine);
87+
} else {
88+
entity_mut.remove::<LeftClickMine>();
89+
}
90+
}
91+
}
92+
93+
#[derive(Component)]
94+
pub struct LeftClickMine;
95+
96+
#[allow(clippy::type_complexity)]
97+
fn handle_left_click_mine(
98+
mut query: Query<
99+
(
100+
&HitResultComponent,
101+
Entity,
102+
Option<&Mining>,
103+
&InventoryComponent,
104+
&MineBlockPos,
105+
&MineItem,
106+
),
107+
(With<LeftClickMine>, With<Player>, With<LocalEntity>),
108+
>,
109+
mut start_mining_block_event: EventWriter<StartMiningBlockEvent>,
110+
mut stop_mining_block_event: EventWriter<StopMiningBlockEvent>
111+
) {
112+
for (
113+
hit_result_component,
114+
entity,
115+
mining,
116+
inventory,
117+
current_mining_pos,
118+
current_mining_item,
119+
) in &mut query.iter_mut()
120+
{
121+
let block_pos = hit_result_component.block_pos;
122+
123+
if (mining.is_none()
124+
|| !is_same_mining_target(
125+
block_pos,
126+
inventory,
127+
current_mining_pos,
128+
current_mining_item,
129+
)) && !hit_result_component.miss
130+
{
131+
start_mining_block_event.send(StartMiningBlockEvent {
132+
entity,
133+
position: block_pos,
134+
});
135+
} else if mining.is_some() && hit_result_component.miss {
136+
// Stop mining as the block is not reachable
137+
stop_mining_block_event.send(StopMiningBlockEvent {
138+
entity
139+
});
140+
}
141+
}
69142
}
70143

71144
/// Information about the block we're currently mining. This is only present if
@@ -85,6 +158,7 @@ pub struct StartMiningBlockEvent {
85158
pub entity: Entity,
86159
pub position: BlockPos,
87160
}
161+
88162
fn handle_start_mining_block_event(
89163
mut events: EventReader<StartMiningBlockEvent>,
90164
mut start_mining_events: EventWriter<StartMiningBlockWithDirectionEvent>,
@@ -113,6 +187,7 @@ pub struct StartMiningBlockWithDirectionEvent {
113187
pub position: BlockPos,
114188
pub direction: Direction,
115189
}
190+
116191
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
117192
fn handle_start_mining_block_with_direction_event(
118193
mut events: EventReader<StartMiningBlockWithDirectionEvent>,
@@ -175,11 +250,11 @@ fn handle_start_mining_block_with_direction_event(
175250
**mine_delay = 5;
176251
} else if mining.is_none()
177252
|| !is_same_mining_target(
178-
event.position,
179-
inventory,
180-
&current_mining_pos,
181-
&current_mining_item,
182-
)
253+
event.position,
254+
inventory,
255+
&current_mining_pos,
256+
&current_mining_item,
257+
)
183258
{
184259
if mining.is_some() {
185260
// send a packet to stop mining since we just changed target
@@ -192,7 +267,7 @@ fn handle_start_mining_block_with_direction_event(
192267
direction: event.direction,
193268
sequence: 0,
194269
}
195-
.get(),
270+
.get(),
196271
});
197272
}
198273

@@ -228,12 +303,12 @@ fn handle_start_mining_block_with_direction_event(
228303

229304
if block_is_solid
230305
&& get_mine_progress(
231-
block.as_ref(),
232-
held_item.kind(),
233-
&inventory.inventory_menu,
234-
fluid_on_eyes,
235-
physics,
236-
) >= 1.
306+
block.as_ref(),
307+
held_item.kind(),
308+
&inventory.inventory_menu,
309+
fluid_on_eyes,
310+
physics,
311+
) >= 1.
237312
{
238313
// block was broken instantly
239314
finish_mining_events.send(FinishMiningBlockEvent {
@@ -264,7 +339,7 @@ fn handle_start_mining_block_with_direction_event(
264339
direction: event.direction,
265340
sequence: **sequence_number,
266341
}
267-
.get(),
342+
.get(),
268343
});
269344
}
270345
}
@@ -414,6 +489,7 @@ fn handle_finish_mining_block_event(
414489
pub struct StopMiningBlockEvent {
415490
pub entity: Entity,
416491
}
492+
417493
fn handle_stop_mining_block_event(
418494
mut events: EventReader<StopMiningBlockEvent>,
419495
mut send_packet_events: EventWriter<SendPacketEvent>,
@@ -434,7 +510,7 @@ fn handle_stop_mining_block_event(
434510
direction: Direction::Down,
435511
sequence: 0,
436512
}
437-
.get(),
513+
.get(),
438514
});
439515
commands.entity(event.entity).remove::<Mining>();
440516
**mine_progress = 0.;
@@ -508,7 +584,7 @@ fn continue_mining_block(
508584
direction: mining.dir,
509585
sequence: **sequence_number,
510586
}
511-
.get(),
587+
.get(),
512588
});
513589
swing_arm_events.send(SwingArmEvent { entity });
514590
} else if is_same_mining_target(
@@ -554,7 +630,7 @@ fn continue_mining_block(
554630
direction: mining.dir,
555631
sequence: **sequence_number,
556632
}
557-
.get(),
633+
.get(),
558634
});
559635
**mine_progress = 0.;
560636
**mine_ticks = 0.;

0 commit comments

Comments
 (0)