refactor: remove redundant lua type wrappers

This commit is contained in:
2025-02-17 23:09:44 -05:00
parent 2cba4d797f
commit 0303244897
9 changed files with 58 additions and 211 deletions

View File

@@ -8,9 +8,6 @@ use super::{
container::item_stack::ItemStack,
container::{Container, ContainerRef},
direction::Direction,
entity::Entity,
fluid_state::FluidState,
hunger::Hunger,
vec3::Vec3,
};
use azalea::Client as AzaleaClient;

View File

@@ -1,4 +1,4 @@
use super::{Client, Hunger};
use super::Client;
use azalea::{
ClientInformation,
entity::metadata::{AirSupply, Score},
@@ -13,12 +13,13 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
Ok(client.inner.as_ref().unwrap().health())
}
pub fn hunger(_lua: &Lua, client: &Client) -> Result<Hunger> {
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
let h = client.inner.as_ref().unwrap().hunger();
Ok(Hunger {
food: h.food,
saturation: h.saturation,
})
let hunger = lua.create_table()?;
hunger.set("food", h.food)?;
hunger.set("saturation", h.saturation)?;
Ok(hunger)
}
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {

View File

@@ -1,4 +1,4 @@
use super::{Client, Entity, FluidState, Vec3};
use super::{Client, Vec3};
use azalea::{
BlockPos,
auto_tool::AutoToolClientExt,
@@ -52,7 +52,7 @@ pub fn find_blocks(
.collect())
}
pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Entity>> {
pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Table>> {
let mut matched = Vec::new();
let mut ecs = client.inner.as_ref().unwrap().ecs.lock();
@@ -65,19 +65,21 @@ pub fn find_entities(_lua: &Lua, client: &Client, filter_fn: Function) -> Result
), Without<Dead>>();
for (&id, uuid, kind, position, custom_name) in query.iter(&ecs) {
let entity = Entity {
id: id.0,
uuid: uuid.to_string(),
kind: kind.to_string(),
position: Vec3 {
let entity = lua.create_table()?;
entity.set("id", id.0)?;
entity.set("uuid", uuid.to_string())?;
entity.set("kind", kind.to_string())?;
entity.set(
"position",
Vec3 {
x: position.x,
y: position.y,
z: position.z,
},
custom_name: custom_name.as_ref().map(ToString::to_string),
};
)?;
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
if filter_fn.call::<bool>(entity.clone())? {
if filter_fn.call::<bool>(&entity)? {
matched.push(entity);
}
}
@@ -101,22 +103,28 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
.map(|b| b.id))
}
pub fn get_fluid_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<FluidState>> {
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client
.inner
.as_ref()
.unwrap()
.world()
.read()
.get_fluid_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
))
.map(|f| FluidState {
kind: f.kind as u8,
amount: f.amount,
falling: f.falling,
}))
Ok(
if let Some(fs) = client
.inner
.as_ref()
.unwrap()
.world()
.read()
.get_fluid_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
))
{
let fluid_state = lua.create_table()?;
fluid_state.set("kind", fs.kind as u8)?;
fluid_state.set("amount", fs.amount)?;
fluid_state.set("falling", fs.falling)?;
Some(fluid_state)
} else {
None
},
)
}