refactor: random fixes and usage improvements

This commit is contained in:
2025-02-17 17:01:23 -05:00
parent 8f2fbf11da
commit dde489a8ed
15 changed files with 203 additions and 90 deletions

View File

@@ -6,6 +6,15 @@ use azalea::{
use log::error;
use mlua::{Lua, Result, UserDataRef};
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
Ok(client
.inner
.as_ref()
.unwrap()
.get_open_container()
.map(|c| ContainerRef { inner: c }))
}
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
Ok(ItemStack {
inner: client
@@ -26,15 +35,6 @@ pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
.selected_hotbar_slot)
}
pub fn open_container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
Ok(client
.inner
.as_ref()
.unwrap()
.get_open_container()
.map(|c| ContainerRef { inner: c }))
}
pub async fn open_container_at(
_lua: Lua,
client: UserDataRef<Client>,

View File

@@ -5,7 +5,6 @@ mod state;
mod world;
use super::{
block::Block,
container::item_stack::ItemStack,
container::{Container, ContainerRef},
direction::Direction,
@@ -24,6 +23,7 @@ pub struct Client {
impl UserData for Client {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("air_supply", state::air_supply);
f.add_field_method_get("container", container::container);
f.add_field_method_get("direction", movement::direction);
f.add_field_method_get("eye_position", movement::eye_position);
f.add_field_method_get("has_attack_cooldown", interaction::has_attack_cooldown);
@@ -32,7 +32,6 @@ impl UserData for Client {
f.add_field_method_get("held_slot", container::held_slot);
f.add_field_method_get("hunger", state::hunger);
f.add_field_method_get("looking_at", movement::looking_at);
f.add_field_method_get("open_container", container::open_container);
f.add_field_method_get("pathfinder", movement::pathfinder);
f.add_field_method_get("position", movement::position);
f.add_field_method_get("score", state::score);
@@ -41,16 +40,15 @@ impl UserData for Client {
}
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_async_method("goto", movement::goto);
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_method("best_tool_for_block", world::best_tool_for_block);
m.add_method("block_names_to_states", world::block_names_to_states);
m.add_method("chat", chat);
m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find_blocks);
m.add_method("find_entities", world::find_entities);
m.add_method("get_block_from_state", world::get_block_from_state);
m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("set_held_slot", container::set_held_slot);
@@ -58,7 +56,6 @@ impl UserData for Client {
m.add_method("stop_pathfinding", movement::stop_pathfinding);
m.add_method_mut("attack", interaction::attack);
m.add_method_mut("block_interact", interaction::block_interact);
m.add_method_mut("goto", movement::goto);
m.add_method_mut("jump", movement::jump);
m.add_method_mut("look_at", movement::look_at);
m.add_method_mut("open_inventory", container::open_inventory);

View File

@@ -3,11 +3,11 @@ use azalea::{
BlockPos, BotClientExt, Client as AzaleaClient, SprintDirection, WalkDirection,
interact::HitResultComponent,
pathfinder::{
ExecutingPath, Pathfinder, PathfinderClientExt,
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
},
};
use mlua::{FromLua, Lua, Result, Table, Value};
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let d = client.inner.as_ref().unwrap().direction();
@@ -23,9 +23,9 @@ pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
})
}
pub fn goto(
lua: &Lua,
client: &mut Client,
pub async fn goto(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Value, Option<Table>),
) -> Result<()> {
fn g(client: &AzaleaClient, without_mining: bool, goal: impl Goal + Send + Sync + 'static) {
@@ -55,7 +55,7 @@ pub fn goto(
match goal_type {
1 => {
let t = data.as_table().ok_or(error)?;
let p = Vec3::from_lua(t.get("position")?, lua)?;
let p = Vec3::from_lua(t.get("position")?, &lua)?;
g(
client,
without_mining,
@@ -66,7 +66,7 @@ pub fn goto(
);
}
2 => {
let p = Vec3::from_lua(data, lua)?;
let p = Vec3::from_lua(data, &lua)?;
g(
client,
without_mining,
@@ -95,7 +95,7 @@ pub fn goto(
},
),
_ => {
let p = Vec3::from_lua(data, lua)?;
let p = Vec3::from_lua(data, &lua)?;
g(
client,
without_mining,
@@ -104,6 +104,12 @@ pub fn goto(
}
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}

View File

@@ -1,8 +1,8 @@
use super::{Block, Client, Entity, FluidState, Vec3};
use super::{Client, Entity, FluidState, Vec3};
use azalea::{
BlockPos,
auto_tool::AutoToolClientExt,
blocks::{Block as AzaleaBlock, BlockState, BlockStates},
blocks::{BlockState, BlockStates},
ecs::query::Without,
entity::{Dead, EntityKind, EntityUuid, Position as AzaleaPosition, metadata::CustomName},
world::MinecraftEntityId,
@@ -22,22 +22,6 @@ pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Resu
Ok(tool_result)
}
pub fn block_names_to_states(
_lua: &Lua,
_client: &Client,
block_names: Vec<String>,
) -> Result<Vec<u16>> {
Ok(block_names
.iter()
.flat_map(|n| {
(u32::MIN..u32::MAX)
.map_while(|i| BlockState::try_from(i).ok())
.filter(move |&b| n == Into::<Box<dyn AzaleaBlock>>::into(b).id())
.map(|b| b.id)
})
.collect())
}
pub fn find_blocks(
_lua: &Lua,
client: &Client,
@@ -93,7 +77,7 @@ pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result
custom_name: custom_name.as_ref().map(ToString::to_string),
};
if filter_fn.call::<bool>(entity.clone()).unwrap() {
if filter_fn.call::<bool>(entity.clone())? {
matched.push(entity);
}
}
@@ -101,23 +85,6 @@ pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result
Ok(matched)
}
pub fn get_block_from_state(_lua: &Lua, _client: &Client, state: u32) -> Result<Option<Block>> {
let Ok(state) = BlockState::try_from(state) else {
return Ok(None);
};
let block: Box<dyn AzaleaBlock> = state.into();
let behavior = block.behavior();
Ok(Some(Block {
id: block.id().to_string(),
friction: behavior.friction,
jump_factor: behavior.jump_factor,
destroy_time: behavior.destroy_time,
explosion_resistance: behavior.explosion_resistance,
requires_correct_tool_for_drops: behavior.requires_correct_tool_for_drops,
}))
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client