build(deps)!: update azalea and fix ecs changes

This commit is contained in:
2025-04-18 00:20:55 -04:00
parent 3b154c6a35
commit 63cfc7c9c0
24 changed files with 2690 additions and 1881 deletions

View File

@@ -1,20 +1,19 @@
use azalea::{
BlockPos,
inventory::{Inventory, Menu, Player, SlotList},
prelude::ContainerClientExt,
entity::inventory::Inventory,
inventory::{Menu, Player, SlotList},
protocol::packets::game::ServerboundSetCarriedItem,
};
use log::error;
use mlua::{Lua, Result, UserDataRef, Value};
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
Ok(client.get_open_container().map(ContainerRef))
pub fn container(_lua: &Lua, client: &Client) -> Result<ContainerRef> {
Ok(ContainerRef(client.get_inventory()))
}
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
Ok(ItemStack(client.component::<Inventory>().held_item()))
Ok(ItemStack(client.get_held_item()))
}
pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
@@ -117,20 +116,14 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
return Ok(());
}
{
let mut ecs = client.ecs.lock();
let mut inventory = client.query::<&mut Inventory>(&mut ecs);
if inventory.selected_hotbar_slot == slot {
return Ok(());
client.query_self::<&mut Inventory, _>(|mut inventory| {
if inventory.selected_hotbar_slot != slot {
inventory.selected_hotbar_slot = slot;
}
inventory.selected_hotbar_slot = slot;
};
if let Err(error) = client.write_packet(ServerboundSetCarriedItem {
});
client.write_packet(ServerboundSetCarriedItem {
slot: u16::from(slot),
}) {
error!("failed to send SetCarriedItem packet: {error:?}");
}
});
Ok(())
}

View File

@@ -1,15 +1,16 @@
use azalea::{
BlockPos, BotClientExt,
BlockPos,
core::entity_id::MinecraftEntityId,
protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand},
world::MinecraftEntityId,
};
use log::error;
use mlua::{Lua, Result, UserDataRef};
use super::{Client, Vec3};
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
client.attack(MinecraftEntityId(entity_id));
if let Some(entity) = client.entity_id_by_minecraft_id(MinecraftEntityId(entity_id)) {
client.attack(entity);
}
Ok(())
}
@@ -40,8 +41,8 @@ pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Res
Ok(())
}
pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
client.left_click_mine(mining);
pub fn set_mining(_lua: &Lua, client: &Client, state: bool) -> Result<()> {
client.left_click_mine(state);
Ok(())
}
@@ -55,18 +56,16 @@ pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
Ok(())
}
pub fn use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
pub fn start_use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
let direction = client.direction();
if let Err(error) = client.write_packet(ServerboundUseItem {
client.write_packet(ServerboundUseItem {
hand: match hand {
Some(1) => InteractionHand::OffHand,
_ => InteractionHand::MainHand,
},
sequence: 0,
yaw: direction.0,
pitch: direction.1,
}) {
error!("failed to send UseItem packet: {error:?}");
}
seq: 0,
x_rot: direction.x_rot(),
y_rot: direction.y_rot(),
});
Ok(())
}

View File

@@ -8,7 +8,7 @@ mod world;
use std::ops::{Deref, DerefMut};
use azalea::{Client as AzaleaClient, world::MinecraftEntityId};
use azalea::{Client as AzaleaClient, core::entity_id::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use super::{
@@ -64,14 +64,11 @@ impl UserData for Client {
m.add_async_method("find_entities", world::find::entities);
m.add_async_method("find_players", world::find::players);
m.add_async_method("go_to", movement::go_to);
m.add_async_method(
"go_to_wait_until_reached",
movement::go_to_wait_until_reached,
);
m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at);
m.add_async_method("set_client_information", state::set_client_information);
m.add_async_method("start_go_to", movement::start_go_to);
m.add_async_method("wait_until_goal_reached", movement::wait_until_goal_reached);
m.add_method("attack", interaction::attack);
m.add_method("best_tool_for_block", world::best_tool_for_block);
m.add_method("block_interact", interaction::block_interact);
@@ -92,9 +89,9 @@ impl UserData for Client {
m.add_method("set_sneaking", movement::set_sneaking);
m.add_method("sprint", movement::sprint);
m.add_method("start_mining", interaction::start_mining);
m.add_method("start_use_item", interaction::start_use_item);
m.add_method("stop_pathfinding", movement::stop_pathfinding);
m.add_method("stop_sleeping", movement::stop_sleeping);
m.add_method("use_item", interaction::use_item);
m.add_method("walk", movement::walk);
}
}

View File

@@ -1,15 +1,14 @@
use azalea::{
BlockPos, BotClientExt, SprintDirection, WalkDirection,
BlockPos, SprintDirection, WalkDirection,
core::{entity_id::MinecraftEntityId, hit_result::HitResult},
entity::Position,
interact::HitResultComponent,
interact::pick::HitResultComponent,
pathfinder::{
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
ExecutingPath, Pathfinder, PathfinderClientExt, PathfinderOpts,
goals::{BlockPosGoal, Goal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
},
protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action},
world::MinecraftEntityId,
};
use log::error;
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
use super::{Client, Direction, Vec3};
@@ -28,7 +27,7 @@ impl Goal for AnyGoal {
}
#[allow(clippy::cast_possible_truncation)]
fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -> Result<AnyGoal> {
fn to_goal(lua: &Lua, client: &Client, data: Table, kind: u8) -> Result<AnyGoal> {
let goal: Box<dyn Goal> = match kind {
1 => {
let pos = Vec3::from_lua(data.get("position")?, lua)?;
@@ -38,11 +37,13 @@ fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -
})
}
2 => {
let distance = data.get("distance").unwrap_or(4.5);
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(ReachBlockPosGoal {
pos: BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
chunk_storage: client.world().read().chunks.clone(),
})
Box::new(ReachBlockPosGoal::new_with_distance(
BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
distance,
client.world().read().chunks.clone(),
))
}
3 => Box::new(XZGoal {
x: data.get("x")?,
@@ -59,22 +60,14 @@ fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -
}
};
Ok(AnyGoal(if options.get("inverse").unwrap_or_default() {
Box::new(InverseGoal(AnyGoal(goal)))
} else {
goal
}))
Ok(AnyGoal(goal))
}
pub fn go_to_reached(_lua: &Lua, client: &Client) -> Result<bool> {
Ok(client.is_goto_target_reached())
}
pub async fn go_to_wait_until_reached(
_lua: Lua,
client: UserDataRef<Client>,
(): (),
) -> Result<()> {
pub async fn wait_until_goal_reached(_lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<()> {
client.wait_until_goto_target_reached().await;
Ok(())
}
@@ -90,11 +83,10 @@ pub async fn go_to(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
client.start_goto_with_opts(goal, PathfinderOpts::new().allow_mining(false));
client.wait_until_goto_target_reached().await;
} else {
client.goto(goal).await;
@@ -113,19 +105,13 @@ pub async fn start_go_to(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
} else {
client.start_goto(goal);
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
client.start_goto_with_opts(
goal,
PathfinderOpts::new().allow_mining(options.get("without_mining").unwrap_or_default()),
);
let _ = client.get_tick_broadcaster().recv().await;
Ok(())
}
@@ -133,8 +119,8 @@ pub async fn start_go_to(
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let direction = client.direction();
Ok(Direction {
y: direction.0,
x: direction.1,
y: direction.y_rot(),
x: direction.x_rot(),
})
}
@@ -148,16 +134,19 @@ pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
}
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let result = client.component::<HitResultComponent>();
Ok(if result.miss {
None
} else {
let table = lua.create_table()?;
table.set("position", Vec3::from(result.block_pos))?;
table.set("inside", result.inside)?;
table.set("world_border", result.world_border)?;
Some(table)
})
Ok(
if let HitResult::Block(ref result) = **client.component::<HitResultComponent>() {
let table = lua.create_table()?;
table.set("direction", Vec3::from(result.direction.normal()))?;
table.set("inside", result.inside)?;
table.set("location", Vec3::from(result.location))?;
table.set("position", Vec3::from(result.block_pos))?;
table.set("world_border", result.world_border)?;
Some(table)
} else {
None
},
)
}
pub fn look_at(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
@@ -179,8 +168,8 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
Vec3::from(pathfinder.last_reached_node),
)?;
table.set(
"last_node_reach_elapsed",
pathfinder.last_node_reached_at.elapsed().as_millis(),
"ticks_since_last_node_reached",
pathfinder.ticks_since_last_node_reached,
)?;
table.set("is_path_partial", pathfinder.is_path_partial)?;
true
@@ -192,7 +181,7 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
}
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(&client.component::<Position>()))
Ok(Vec3::from(*client.component::<Position>()))
}
pub fn set_direction(_lua: &Lua, client: &Client, direction: Direction) -> Result<()> {
@@ -205,27 +194,17 @@ pub fn set_jumping(_lua: &Lua, client: &Client, jumping: bool) -> Result<()> {
Ok(())
}
pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<()> {
let mut ecs = client.ecs.lock();
let mut position = client.query::<&mut Position>(&mut ecs);
position.x = new_position.x;
position.y = new_position.y;
position.z = new_position.z;
pub fn set_position(_lua: &Lua, client: &Client, new_pos: Vec3) -> Result<()> {
client.query_self::<&mut Position, _>(|mut pos| {
pos.x = new_pos.x;
pos.y = new_pos.y;
pos.z = new_pos.z;
});
Ok(())
}
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: if sneaking {
Action::PressShiftKey
} else {
Action::ReleaseShiftKey
},
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
client.set_crouching(sneaking);
Ok(())
}
@@ -244,13 +223,11 @@ pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
}
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
client.write_packet(ServerboundPlayerCommand {
id: *client.component::<MinecraftEntityId>(),
action: Action::StopSleeping,
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
});
Ok(())
}

View File

@@ -1,13 +1,13 @@
use azalea::{
ClientInformation,
entity::metadata::{AirSupply, Score},
pathfinder::PathfinderDebugParticles,
pathfinder::debug::PathfinderDebugParticles,
protocol::common::client_information::ModelCustomization,
};
use azalea_hax::AntiKnockback;
use mlua::{Error, Lua, Result, Table, UserDataRef};
use super::Client;
use crate::hacks::anti_knockback::AntiKnockback;
pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<AirSupply>().0)
@@ -35,26 +35,25 @@ pub async fn set_client_information(
info: Table,
) -> Result<()> {
let get_bool = |table: &Table, name| table.get(name).unwrap_or(true);
client
.set_client_information(ClientInformation {
allows_listing: info.get("allows_listing")?,
model_customization: info
.get::<Table>("model_customization")
.map(|t| ModelCustomization {
cape: get_bool(&t, "cape"),
jacket: get_bool(&t, "jacket"),
left_sleeve: get_bool(&t, "left_sleeve"),
right_sleeve: get_bool(&t, "right_sleeve"),
left_pants: get_bool(&t, "left_pants"),
right_pants: get_bool(&t, "right_pants"),
hat: get_bool(&t, "hat"),
})
.unwrap_or_default(),
view_distance: info.get("view_distance").unwrap_or(8),
..ClientInformation::default()
})
.await
.map_err(Error::external)
client.set_client_information(ClientInformation {
allows_listing: info.get("allows_listing")?,
model_customization: info
.get::<Table>("model_customization")
.as_ref()
.map(|t| ModelCustomization {
cape: get_bool(t, "cape"),
jacket: get_bool(t, "jacket"),
left_sleeve: get_bool(t, "left_sleeve"),
right_sleeve: get_bool(t, "right_sleeve"),
left_pants: get_bool(t, "left_pants"),
right_pants: get_bool(t, "right_pants"),
hat: get_bool(t, "hat"),
})
.unwrap_or_default(),
view_distance: info.get("view_distance").unwrap_or(8),
..ClientInformation::default()
});
Ok(())
}
pub fn set_component(
@@ -64,7 +63,7 @@ pub fn set_component(
) -> Result<()> {
macro_rules! set {
($name:ident) => {{
let mut ecs = client.ecs.lock();
let mut ecs = client.ecs.write();
let mut entity = ecs.entity_mut(client.entity);
if enabled.unwrap_or(true) {
entity.insert($name)

View File

@@ -1,16 +1,16 @@
use azalea::{
BlockPos,
blocks::{BlockState, BlockStates},
block::{BlockState, BlockStates},
ecs::query::{With, Without},
entity::{
Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition,
Dead, EntityKindComponent, EntityUuid, LookDirection, Pose, Position as AzaleaPosition,
metadata::{CustomName, Owneruuid, Player},
},
world::MinecraftEntityId,
};
use mlua::{Function, Lua, Result, Table, UserDataRef};
use super::{Client, Direction, Vec3};
use crate::lua::client::MinecraftEntityId;
pub fn blocks(
_lua: &Lua,
@@ -28,7 +28,10 @@ pub fn blocks(
nearest_to.z as i32,
),
&BlockStates {
set: block_states.iter().map(|&id| BlockState { id }).collect(),
set: block_states
.into_iter()
.flat_map(BlockState::try_from)
.collect(),
},
)
.map(Vec3::from)

View File

@@ -2,21 +2,24 @@
mod queries;
pub mod find;
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
use mlua::{Lua, Result, Table};
use azalea::{BlockPos, block::BlockState, world::WorldName};
use mlua::{Lua, Result, Table, Value};
use super::{Client, Direction, Vec3};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Value> {
let Ok(block) = BlockState::try_from(block_state) else {
return Ok(Value::Nil);
};
let result = client.best_tool_in_hotbar_for_block(block);
let table = lua.create_table()?;
table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(table)
Ok(Value::Table(table))
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.component::<InstanceName>().to_string())
Ok(client.component::<WorldName>().to_string())
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
@@ -24,17 +27,17 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
Ok(client
.world()
.read()
.get_block_state(&BlockPos::new(
.get_block_state(BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
))
.map(|block| block.id))
.map(|block| block.id()))
}
#[allow(clippy::cast_possible_truncation)]
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
let fluid_state = client.world().read().get_fluid_state(&BlockPos::new(
let fluid_state = client.world().read().get_fluid_state(BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,

View File

@@ -1,11 +1,11 @@
#[macro_export]
macro_rules! get_entities {
($client:ident) => {{
let mut ecs = $client.ecs.lock();
let mut ecs = $client.ecs.write();
ecs.query::<(
&AzaleaPosition,
&CustomName,
&EntityKind,
&EntityKindComponent,
&EntityUuid,
&LookDirection,
&MinecraftEntityId,
@@ -16,7 +16,7 @@ macro_rules! get_entities {
.map(
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
(
Vec3::from(position),
Vec3::from(*position),
custom_name.as_ref().map(ToString::to_string),
kind.to_string(),
uuid.to_string(),
@@ -34,11 +34,11 @@ macro_rules! get_entities {
#[macro_export]
macro_rules! get_players {
($client:ident) => {{
let mut ecs = $client.ecs.lock();
let mut ecs = $client.ecs.write();
ecs.query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKind,
&EntityKindComponent,
&AzaleaPosition,
&LookDirection,
&Pose,
@@ -49,7 +49,7 @@ macro_rules! get_players {
id.0,
uuid.to_string(),
kind.to_string(),
Vec3::from(position),
Vec3::from(*position),
Direction::from(direction),
*pose as u8,
)