fix(client): handle borrows with async

This commit is contained in:
2026-04-13 13:58:58 -04:00
parent d6c16f0d5d
commit 57397d3718
7 changed files with 84 additions and 50 deletions

View File

@@ -10,7 +10,7 @@ use azalea::{
use mlua::{Function, Lua, Result, Table, UserDataRef};
use super::{Client, Direction, Vec3};
use crate::lua::client::MinecraftEntityId;
use crate::{lua::client::MinecraftEntityId, unpack};
pub fn blocks(
_lua: &Lua,
@@ -39,6 +39,8 @@ pub fn blocks(
}
pub async fn all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::with_capacity(256);
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client)
@@ -65,6 +67,8 @@ pub async fn entities(
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new();
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client)
@@ -89,6 +93,8 @@ pub async fn entities(
}
pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?;
@@ -108,6 +114,8 @@ pub async fn players(
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?;

View File

@@ -1,8 +1,8 @@
#[macro_export]
macro_rules! get_entities {
($client:ident) => {{
let mut ecs = $client.ecs.write();
ecs.query::<(
let ecs = $client.ecs.read();
if let Some(mut query) = ecs.try_query::<(
&AzaleaPosition,
&CustomName,
&EntityKindComponent,
@@ -11,31 +11,35 @@ macro_rules! get_entities {
&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,
)>() {
query
.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<_>>()
.collect::<Vec<_>>()
} else {
Vec::new()
}
}};
}
#[macro_export]
macro_rules! get_players {
($client:ident) => {{
let mut ecs = $client.ecs.write();
ecs.query_filtered::<(
let ecs = $client.ecs.read();
if let Some(mut query) = ecs.try_query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKindComponent,
@@ -43,17 +47,22 @@ macro_rules! get_players {
&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<_>>()
{
query
.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<_>>()
} else {
Vec::new()
}
}};
}