From 8150e5c09c341e933da5020e85340aa9464e430e Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Sat, 15 Mar 2025 16:53:34 -0400 Subject: [PATCH] refactor(lua/client/world): split finders into module --- src/lua/client/mod.rs | 10 +-- src/lua/client/world/find.rs | 121 +++++++++++++++++++++++++++++++++ src/lua/client/world/mod.rs | 128 +---------------------------------- 3 files changed, 129 insertions(+), 130 deletions(-) create mode 100644 src/lua/client/world/find.rs diff --git a/src/lua/client/mod.rs b/src/lua/client/mod.rs index e5d2b85..cc87832 100644 --- a/src/lua/client/mod.rs +++ b/src/lua/client/mod.rs @@ -56,10 +56,10 @@ impl UserData for Client { } fn add_methods>(m: &mut M) { - m.add_async_method("find_all_entities", world::find_all_entities); - m.add_async_method("find_all_players", world::find_all_players); - m.add_async_method("find_entities", world::find_entities); - m.add_async_method("find_players", world::find_players); + m.add_async_method("find_all_entities", world::find::all_entities); + m.add_async_method("find_all_players", world::find::all_players); + 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("mine", interaction::mine); m.add_async_method("open_container_at", container::open_container_at); @@ -67,7 +67,7 @@ impl UserData for Client { m.add_method("best_tool_for_block", world::best_tool_for_block); m.add_method("chat", chat); m.add_method("disconnect", disconnect); - m.add_method("find_blocks", world::find_blocks); + m.add_method("find_blocks", world::find::blocks); m.add_method("get_block_state", world::get_block_state); m.add_method("get_fluid_state", world::get_fluid_state); m.add_method("set_component", state::set_component); diff --git a/src/lua/client/world/find.rs b/src/lua/client/world/find.rs new file mode 100644 index 0000000..656c258 --- /dev/null +++ b/src/lua/client/world/find.rs @@ -0,0 +1,121 @@ +use super::{Client, Direction, Vec3}; +use azalea::{ + BlockPos, + blocks::{BlockState, BlockStates}, + ecs::query::{With, Without}, + entity::{ + Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition, + metadata::{CustomName, Owneruuid, Player}, + }, + world::MinecraftEntityId, +}; +use mlua::{Function, Lua, Result, Table, UserDataRef}; + +pub fn blocks( + _lua: &Lua, + client: &Client, + (nearest_to, block_states): (Vec3, Vec), +) -> Result> { + #[allow(clippy::cast_possible_truncation)] + Ok(client + .world() + .read() + .find_blocks( + BlockPos::new( + nearest_to.x as i32, + nearest_to.y as i32, + nearest_to.z as i32, + ), + &BlockStates { + set: block_states.iter().map(|&id| BlockState { id }).collect(), + }, + ) + .map(Vec3::from) + .collect()) +} + +pub async fn all_entities(lua: Lua, client: UserDataRef, (): ()) -> Result> { + let mut matched = Vec::with_capacity(256); + for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in + get_entities!(client) + { + let table = lua.create_table()?; + table.set("position", position)?; + table.set("custom_name", custom_name)?; + table.set("kind", kind)?; + table.set("uuid", uuid)?; + table.set("direction", direction)?; + table.set("id", id)?; + table.set( + "owner_uuid", + owner_uuid.and_then(|v| *v).map(|v| v.to_string()), + )?; + table.set("pose", pose)?; + matched.push(table); + } + Ok(matched) +} + +pub async fn entities( + lua: Lua, + client: UserDataRef, + filter_fn: Function, +) -> Result> { + let mut matched = Vec::new(); + for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in + get_entities!(client) + { + let table = lua.create_table()?; + table.set("position", position)?; + table.set("custom_name", custom_name)?; + table.set("kind", kind)?; + table.set("uuid", uuid)?; + table.set("direction", direction)?; + table.set("id", id)?; + table.set( + "owner_uuid", + owner_uuid.and_then(|v| *v).map(|v| v.to_string()), + )?; + table.set("pose", pose)?; + if filter_fn.call_async::(&table).await? { + matched.push(table); + } + } + Ok(matched) +} + +pub async fn all_players(lua: Lua, client: UserDataRef, (): ()) -> Result> { + let mut matched = Vec::new(); + for (id, uuid, kind, position, direction, pose) in get_players!(client) { + let table = lua.create_table()?; + table.set("id", id)?; + table.set("uuid", uuid)?; + table.set("kind", kind)?; + table.set("position", position)?; + table.set("direction", direction)?; + table.set("pose", pose)?; + matched.push(table); + } + Ok(matched) +} + +pub async fn players( + lua: Lua, + client: UserDataRef, + filter_fn: Function, +) -> Result> { + let mut matched = Vec::new(); + for (id, uuid, kind, position, direction, pose) in get_players!(client) { + let table = lua.create_table()?; + table.set("id", id)?; + table.set("uuid", uuid)?; + table.set("kind", kind)?; + table.set("position", position)?; + table.set("direction", direction)?; + table.set("pose", pose)?; + if filter_fn.call_async::(&table).await? { + matched.push(table); + } + } + Ok(matched) +} diff --git a/src/lua/client/world/mod.rs b/src/lua/client/world/mod.rs index 8991df0..bb16351 100644 --- a/src/lua/client/world/mod.rs +++ b/src/lua/client/world/mod.rs @@ -1,19 +1,10 @@ #[macro_use] mod queries; +pub mod find; use super::{Client, Direction, Vec3}; -use azalea::{ - BlockPos, - auto_tool::AutoToolClientExt, - blocks::{BlockState, BlockStates}, - ecs::query::{With, Without}, - entity::{ - Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition, - metadata::{CustomName, Owneruuid, Player}, - }, - world::{InstanceName, MinecraftEntityId}, -}; -use mlua::{Function, Lua, Result, Table, UserDataRef}; +use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName}; +use mlua::{Lua, Result, Table}; pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result { let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state }); @@ -28,119 +19,6 @@ pub fn dimension(_lua: &Lua, client: &Client) -> Result { Ok(client.component::().to_string()) } -pub fn find_blocks( - _lua: &Lua, - client: &Client, - (nearest_to, block_states): (Vec3, Vec), -) -> Result> { - #[allow(clippy::cast_possible_truncation)] - Ok(client - .world() - .read() - .find_blocks( - BlockPos::new( - nearest_to.x as i32, - nearest_to.y as i32, - nearest_to.z as i32, - ), - &BlockStates { - set: block_states.iter().map(|&id| BlockState { id }).collect(), - }, - ) - .map(Vec3::from) - .collect()) -} - -pub async fn find_all_entities( - lua: Lua, - client: UserDataRef, - (): (), -) -> Result> { - let mut matched = Vec::with_capacity(256); - for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in - get_entities!(client) - { - let table = lua.create_table()?; - table.set("position", position)?; - table.set("custom_name", custom_name)?; - table.set("kind", kind)?; - table.set("uuid", uuid)?; - table.set("direction", direction)?; - table.set("id", id)?; - table.set( - "owner_uuid", - owner_uuid.and_then(|v| *v).map(|v| v.to_string()), - )?; - table.set("pose", pose)?; - matched.push(table); - } - Ok(matched) -} - -pub async fn find_entities( - lua: Lua, - client: UserDataRef, - filter_fn: Function, -) -> Result> { - let mut matched = Vec::new(); - for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in - get_entities!(client) - { - let table = lua.create_table()?; - table.set("position", position)?; - table.set("custom_name", custom_name)?; - table.set("kind", kind)?; - table.set("uuid", uuid)?; - table.set("direction", direction)?; - table.set("id", id)?; - table.set( - "owner_uuid", - owner_uuid.and_then(|v| *v).map(|v| v.to_string()), - )?; - table.set("pose", pose)?; - if filter_fn.call_async::(&table).await? { - matched.push(table); - } - } - Ok(matched) -} - -pub async fn find_all_players(lua: Lua, client: UserDataRef, (): ()) -> Result> { - let mut matched = Vec::new(); - for (id, uuid, kind, position, direction, pose) in get_players!(client) { - let table = lua.create_table()?; - table.set("id", id)?; - table.set("uuid", uuid)?; - table.set("kind", kind)?; - table.set("position", position)?; - table.set("direction", direction)?; - table.set("pose", pose)?; - matched.push(table); - } - Ok(matched) -} - -pub async fn find_players( - lua: Lua, - client: UserDataRef, - filter_fn: Function, -) -> Result> { - let mut matched = Vec::new(); - for (id, uuid, kind, position, direction, pose) in get_players!(client) { - let table = lua.create_table()?; - table.set("id", id)?; - table.set("uuid", uuid)?; - table.set("kind", kind)?; - table.set("position", position)?; - table.set("direction", direction)?; - table.set("pose", pose)?; - if filter_fn.call_async::(&table).await? { - matched.push(table); - } - } - Ok(matched) -} - pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result> { #[allow(clippy::cast_possible_truncation)] Ok(client