refactor(world)!: create find_all variants of entity filters

This commit is contained in:
Ryan 2025-03-06 21:48:22 -05:00
parent 1ebe7dd0b9
commit 492fecc32c
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 136 additions and 82 deletions

View File

@ -60,6 +60,8 @@ 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("attack", interaction::attack); m.add_async_method("attack", interaction::attack);
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_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);

View File

@ -1,3 +1,6 @@
#[macro_use]
mod queries;
use super::{Client, Direction, Vec3}; use super::{Client, Direction, Vec3};
use azalea::{ use azalea::{
BlockPos, BlockPos,
@ -48,100 +51,90 @@ pub fn find_blocks(
.collect()) .collect())
} }
pub async fn find_all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> 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)?;
matched.push(table);
}
Ok(matched)
}
pub async fn find_entities( pub async fn find_entities(
lua: Lua, lua: Lua,
client: UserDataRef<Client>, client: UserDataRef<Client>,
filter_fn: Function, filter_fn: Function,
) -> Result<Vec<Table>> { ) -> Result<Vec<Table>> {
let entities = {
let mut ecs = client.ecs.lock();
ecs.query::<(
&AzaleaPosition,
&CustomName,
&EntityKind,
&EntityUuid,
&LookDirection,
&MinecraftEntityId,
Option<&Owneruuid>,
&Pose,
)>()
.iter(&ecs)
.map(
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
(
Vec3::from(position),
custom_name.as_ref().map(ToString::to_string),
kind.to_string(),
uuid.to_string(),
Direction::from(direction),
id.0,
owner_uuid.map(ToOwned::to_owned),
*pose as u8,
)
},
)
.collect::<Vec<_>>()
};
let mut matched = Vec::new(); let mut matched = Vec::new();
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in entities { for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
let entity = lua.create_table()?; get_entities!(client)
entity.set("position", position)?; {
entity.set("custom_name", custom_name)?; let table = lua.create_table()?;
entity.set("kind", kind)?; table.set("position", position)?;
entity.set("uuid", uuid)?; table.set("custom_name", custom_name)?;
entity.set("direction", direction)?; table.set("kind", kind)?;
entity.set("id", id)?; table.set("uuid", uuid)?;
if let Some(v) = owner_uuid table.set("direction", direction)?;
&& let Some(uuid) = *v table.set("id", id)?;
{ table.set(
entity.set("owner_uuid", uuid.to_string())?; "owner_uuid",
} owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
entity.set("pose", pose)?; )?;
if filter_fn.call_async::<bool>(&entity).await? { table.set("pose", pose)?;
matched.push(entity); if filter_fn.call_async::<bool>(&table).await? {
matched.push(table);
} }
} }
Ok(matched) Ok(matched)
} }
pub async fn find_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> { pub async fn find_all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let entities = { let mut matched = Vec::new();
let mut ecs = client.ecs.lock(); for (id, uuid, kind, position, direction, pose) in get_players!(client) {
ecs.query_filtered::<( let table = lua.create_table()?;
&MinecraftEntityId, table.set("id", id)?;
&EntityUuid, table.set("uuid", uuid)?;
&EntityKind, table.set("kind", kind)?;
&AzaleaPosition, table.set("position", position)?;
&LookDirection, table.set("direction", direction)?;
&Pose, table.set("pose", pose)?;
), (With<Player>, Without<Dead>)>() matched.push(table);
.iter(&ecs)
.map(|(id, uuid, kind, position, direction, pose)| {
(
id.0,
uuid.to_string(),
kind.to_string(),
Vec3::from(position),
Direction::from(direction),
*pose as u8,
)
})
.collect::<Vec<_>>()
};
let mut players = Vec::new();
for (id, uuid, kind, position, direction, pose) in entities {
let entity = lua.create_table()?;
entity.set("id", id)?;
entity.set("uuid", uuid)?;
entity.set("kind", kind)?;
entity.set("position", position)?;
entity.set("direction", direction)?;
entity.set("pose", pose)?;
players.push(entity);
} }
Ok(players) 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>> {

View File

@ -0,0 +1,59 @@
#[macro_export]
macro_rules! get_entities {
($client:ident) => {{
let mut ecs = $client.ecs.lock();
ecs.query::<(
&AzaleaPosition,
&CustomName,
&EntityKind,
&EntityUuid,
&LookDirection,
&MinecraftEntityId,
Option<&Owneruuid>,
&Pose,
)>()
.iter(&ecs)
.map(
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
(
Vec3::from(position),
custom_name.as_ref().map(ToString::to_string),
kind.to_string(),
uuid.to_string(),
Direction::from(direction),
id.0,
owner_uuid.map(ToOwned::to_owned),
*pose as u8,
)
},
)
.collect::<Vec<_>>()
}};
}
#[macro_export]
macro_rules! get_players {
($client:ident) => {{
let mut ecs = $client.ecs.lock();
ecs.query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKind,
&AzaleaPosition,
&LookDirection,
&Pose,
), (With<Player>, Without<Dead>)>()
.iter(&ecs)
.map(|(id, uuid, kind, position, direction, pose)| {
(
id.0,
uuid.to_string(),
kind.to_string(),
Vec3::from(position),
Direction::from(direction),
*pose as u8,
)
})
.collect::<Vec<_>>()
}};
}