refactor(lua/client/world): split finders into module

This commit is contained in:
Ryan 2025-03-15 16:53:34 -04:00
parent 0f2a5a0dc5
commit 8150e5c09c
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 129 additions and 130 deletions

View File

@ -56,10 +56,10 @@ impl UserData for Client {
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_async_method("find_all_entities", world::find_all_entities); 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_all_players", world::find::all_players);
m.add_async_method("find_entities", world::find_entities); m.add_async_method("find_entities", world::find::entities);
m.add_async_method("find_players", world::find_players); m.add_async_method("find_players", world::find::players);
m.add_async_method("go_to", movement::go_to); m.add_async_method("go_to", movement::go_to);
m.add_async_method("mine", interaction::mine); m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at); 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("best_tool_for_block", world::best_tool_for_block);
m.add_method("chat", chat); m.add_method("chat", chat);
m.add_method("disconnect", disconnect); 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_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state); m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("set_component", state::set_component); m.add_method("set_component", state::set_component);

View File

@ -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<u16>),
) -> Result<Vec<Vec3>> {
#[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<Client>, (): ()) -> Result<Vec<Table>> {
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<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
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::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}
pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
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<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
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::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}

View File

@ -1,19 +1,10 @@
#[macro_use] #[macro_use]
mod queries; mod queries;
pub mod find;
use super::{Client, Direction, Vec3}; use super::{Client, Direction, Vec3};
use azalea::{ use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
BlockPos, use mlua::{Lua, Result, Table};
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};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> { 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 }); 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<String> {
Ok(client.component::<InstanceName>().to_string()) Ok(client.component::<InstanceName>().to_string())
} }
pub fn find_blocks(
_lua: &Lua,
client: &Client,
(nearest_to, block_states): (Vec3, Vec<u16>),
) -> Result<Vec<Vec3>> {
#[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<Client>,
(): (),
) -> Result<Vec<Table>> {
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<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
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::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}
pub async fn find_all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
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<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
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::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> { pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
Ok(client Ok(client