refactor: make some more things async
This commit is contained in:
parent
b55207a559
commit
6c7156f70d
@ -9,7 +9,7 @@ pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
|
|||||||
"get_block_from_state",
|
"get_block_from_state",
|
||||||
lua.create_function(get_block_from_state)?,
|
lua.create_function(get_block_from_state)?,
|
||||||
)?;
|
)?;
|
||||||
globals.set("get_block_states", lua.create_function(get_block_states)?)?;
|
globals.set("get_block_states", lua.create_async_function(get_block_states)?)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -34,8 +34,8 @@ pub fn get_block_from_state(lua: &Lua, state: u32) -> Result<Option<Table>> {
|
|||||||
Ok(Some(block))
|
Ok(Some(block))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_block_states(
|
pub async fn get_block_states(
|
||||||
lua: &Lua,
|
lua: Lua,
|
||||||
(block_names, filter_fn): (Vec<String>, Option<Function>),
|
(block_names, filter_fn): (Vec<String>, Option<Function>),
|
||||||
) -> Result<Vec<u16>> {
|
) -> Result<Vec<u16>> {
|
||||||
let mut matched = Vec::new();
|
let mut matched = Vec::new();
|
||||||
@ -49,7 +49,7 @@ pub fn get_block_states(
|
|||||||
p.set("chest_type", b.property::<ChestType>().map(|v| v as u8))?;
|
p.set("chest_type", b.property::<ChestType>().map(|v| v as u8))?;
|
||||||
p.set("facing", b.property::<Facing>().map(|v| v as u8))?;
|
p.set("facing", b.property::<Facing>().map(|v| v as u8))?;
|
||||||
p.set("light_level", b.property::<LightLevel>().map(|v| v as u8))?;
|
p.set("light_level", b.property::<LightLevel>().map(|v| v as u8))?;
|
||||||
filter_fn.call::<bool>(p.clone())?
|
filter_fn.call_async::<bool>(p.clone()).await?
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
})
|
})
|
||||||
|
@ -59,6 +59,7 @@ 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_entities", world::find_entities);
|
||||||
m.add_async_method("go_to", movement::go_to);
|
m.add_async_method("go_to", movement::go_to);
|
||||||
m.add_async_method("look_at", movement::look_at);
|
m.add_async_method("look_at", movement::look_at);
|
||||||
m.add_async_method("mine", interaction::mine);
|
m.add_async_method("mine", interaction::mine);
|
||||||
@ -68,7 +69,6 @@ impl UserData for Client {
|
|||||||
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("find_entities", world::find_entities);
|
|
||||||
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_held_slot", container::set_held_slot);
|
m.add_method("set_held_slot", container::set_held_slot);
|
||||||
|
@ -10,7 +10,7 @@ use azalea::{
|
|||||||
},
|
},
|
||||||
world::{InstanceName, MinecraftEntityId},
|
world::{InstanceName, MinecraftEntityId},
|
||||||
};
|
};
|
||||||
use mlua::{Function, Lua, Result, Table};
|
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 tr = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
|
let tr = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
|
||||||
@ -48,35 +48,44 @@ pub fn find_blocks(
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Table>> {
|
pub async fn find_entities(
|
||||||
let mut matched = Vec::new();
|
lua: Lua,
|
||||||
|
client: UserDataRef<Client>,
|
||||||
|
filter_fn: Function,
|
||||||
|
) -> Result<Vec<Table>> {
|
||||||
|
let mut entities = Vec::new();
|
||||||
|
|
||||||
let mut ecs = client.ecs.lock();
|
{
|
||||||
let mut query = ecs.query_filtered::<(
|
let mut ecs = client.ecs.lock();
|
||||||
&MinecraftEntityId,
|
let mut query = ecs.query_filtered::<(
|
||||||
&EntityUuid,
|
&MinecraftEntityId,
|
||||||
&EntityKind,
|
&EntityUuid,
|
||||||
&CustomName,
|
&EntityKind,
|
||||||
&AzaleaPosition,
|
&CustomName,
|
||||||
&LookDirection,
|
&AzaleaPosition,
|
||||||
&Pose,
|
&LookDirection,
|
||||||
), Without<Dead>>();
|
&Pose,
|
||||||
|
), Without<Dead>>();
|
||||||
|
|
||||||
for (&id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
|
for (id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
|
||||||
let entity = lua.create_table()?;
|
let entity = lua.create_table()?;
|
||||||
entity.set("id", id.0)?;
|
entity.set("id", id.0)?;
|
||||||
entity.set("uuid", uuid.to_string())?;
|
entity.set("uuid", uuid.to_string())?;
|
||||||
entity.set("kind", kind.to_string())?;
|
entity.set("kind", kind.to_string())?;
|
||||||
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
|
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
|
||||||
entity.set("position", Vec3::from(position))?;
|
entity.set("position", Vec3::from(position))?;
|
||||||
entity.set("direction", Direction::from(direction))?;
|
entity.set("direction", Direction::from(direction))?;
|
||||||
entity.set("pose", *pose as u8)?;
|
entity.set("pose", *pose as u8)?;
|
||||||
|
entities.push(entity);
|
||||||
if filter_fn.call::<bool>(&entity)? {
|
|
||||||
matched.push(entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut matched = Vec::new();
|
||||||
|
for entity in entities {
|
||||||
|
if filter_fn.call_async::<bool>(&entity).await? {
|
||||||
|
matched.push(entity)
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(matched)
|
Ok(matched)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user