1use azalea_block::{BlockState, fluid_state::FluidKind};
2use azalea_core::position::BlockPos;
3use azalea_entity::{ActiveEffects, Attributes, FluidOnEyes, Physics, inventory::Inventory};
4use azalea_inventory::Menu;
5use azalea_registry::{
6 builtin::{BlockKind, EntityKind},
7 item::{ItemStack, components},
8};
9
10use crate::{Client, client_impl::error::AzaleaResult};
11
12#[derive(Debug)]
13pub struct BestToolResult {
14 pub index: usize,
15 pub percentage_per_tick: f32,
16}
17
18impl Client {
19 pub fn best_tool_in_hotbar_for_block(&self, block: BlockState) -> AzaleaResult<BestToolResult> {
20 self.query_self::<(
21 &Inventory,
22 &Physics,
23 &FluidOnEyes,
24 &Attributes,
25 &ActiveEffects,
26 ), _>(
27 |(inventory, physics, fluid_on_eyes, attributes, active_effects)| {
28 let menu = &inventory.inventory_menu;
29 accurate_best_tool_in_hotbar_for_block(
30 block,
31 menu,
32 physics,
33 fluid_on_eyes,
34 attributes,
35 active_effects,
36 )
37 },
38 )
39 }
40
41 pub async fn mine_with_auto_tool(&self, block_pos: BlockPos) -> AzaleaResult<()> {
42 let block_state = self
43 .world()?
44 .read()
45 .get_block_state(block_pos)
46 .unwrap_or_default();
47 let best_tool_result = self.best_tool_in_hotbar_for_block(block_state)?;
48 self.set_selected_hotbar_slot(best_tool_result.index as u8);
49 self.mine(block_pos).await;
50 Ok(())
51 }
52}
53
54pub fn best_tool_in_hotbar_for_block(block: BlockState, menu: &Menu) -> BestToolResult {
60 let mut physics = Physics::default();
61 physics.set_on_ground(true);
62
63 let inactive_effects = ActiveEffects::default();
64 accurate_best_tool_in_hotbar_for_block(
65 block,
66 menu,
67 &physics,
68 &FluidOnEyes::new(FluidKind::Empty),
69 &Attributes::new(EntityKind::Player),
70 &inactive_effects,
71 )
72}
73
74pub fn accurate_best_tool_in_hotbar_for_block(
75 block: BlockState,
76 menu: &Menu,
77 physics: &Physics,
78 fluid_on_eyes: &FluidOnEyes,
79 attributes: &Attributes,
80 active_effects: &ActiveEffects,
81) -> BestToolResult {
82 let hotbar_slots = &menu.slots()[menu.hotbar_slots_range()];
83
84 let mut best_speed = 0.;
85 let mut best_slot = None;
86
87 let block = block.to_trait();
88 let registry_block = block.as_block_kind();
89
90 if matches!(registry_block, BlockKind::Water | BlockKind::Lava) {
91 return BestToolResult {
93 index: 0,
94 percentage_per_tick: 0.,
95 };
96 }
97
98 for (i, item_stack_data) in hotbar_slots.iter().enumerate() {
100 let this_item_speed;
101 match item_stack_data {
102 ItemStack::Empty => {
103 this_item_speed = Some(azalea_entity::mining::get_mine_progress(
104 block,
105 &ItemStack::Empty,
106 fluid_on_eyes,
107 physics,
108 attributes,
109 active_effects,
110 ));
111 }
112 ItemStack::Present(item_stack) => {
113 if !item_stack.component_patch.has::<components::Damage>() {
116 this_item_speed = Some(azalea_entity::mining::get_mine_progress(
117 block,
118 item_stack_data,
119 fluid_on_eyes,
120 physics,
121 attributes,
122 active_effects,
123 ));
124 } else {
125 this_item_speed = None;
126 }
127 }
128 }
129 if let Some(this_item_speed) = this_item_speed
130 && this_item_speed > best_speed
131 {
132 best_slot = Some(i);
133 best_speed = this_item_speed;
134 }
135 }
136
137 for (i, item_stack) in hotbar_slots.iter().enumerate() {
139 if item_stack.is_present() {
140 let this_item_speed = azalea_entity::mining::get_mine_progress(
141 block,
142 item_stack,
143 fluid_on_eyes,
144 physics,
145 attributes,
146 active_effects,
147 );
148 if this_item_speed > best_speed {
149 best_slot = Some(i);
150 best_speed = this_item_speed;
151 }
152 }
153 }
154
155 BestToolResult {
156 index: best_slot.unwrap_or(0),
157 percentage_per_tick: best_speed,
158 }
159}