From 0303244897f76e319862cd3fe5e39454d75c3474 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Mon, 17 Feb 2025 23:09:44 -0500 Subject: [PATCH] refactor: remove redundant lua type wrappers --- src/lua/block.rs | 74 +++++++++-------------------------------- src/lua/client/mod.rs | 3 -- src/lua/client/state.rs | 13 ++++---- src/lua/client/world.rs | 62 +++++++++++++++++++--------------- src/lua/entity.rs | 43 ------------------------ src/lua/fluid_state.rs | 36 -------------------- src/lua/hunger.rs | 33 ------------------ src/lua/mod.rs | 3 -- src/lua/vec3.rs | 2 +- 9 files changed, 58 insertions(+), 211 deletions(-) delete mode 100644 src/lua/entity.rs delete mode 100644 src/lua/fluid_state.rs delete mode 100644 src/lua/hunger.rs diff --git a/src/lua/block.rs b/src/lua/block.rs index 747b442..a43c73b 100644 --- a/src/lua/block.rs +++ b/src/lua/block.rs @@ -2,54 +2,7 @@ use azalea::blocks::{ Block as AzaleaBlock, BlockState, properties::{ChestType, Facing, LightLevel}, }; -use mlua::{FromLua, Function, IntoLua, Lua, Result, Table, Value}; - -#[derive(Clone)] -pub struct Block { - pub id: String, - pub friction: f32, - pub jump_factor: f32, - pub destroy_time: f32, - pub explosion_resistance: f32, - pub requires_correct_tool_for_drops: bool, -} - -impl IntoLua for Block { - fn into_lua(self, lua: &Lua) -> Result { - let table = lua.create_table()?; - table.set("id", self.id)?; - table.set("friction", self.friction)?; - table.set("jump_factor", self.jump_factor)?; - table.set("destroy_time", self.destroy_time)?; - table.set("explosion_resistance", self.explosion_resistance)?; - table.set( - "requires_correct_tool_for_drops", - self.requires_correct_tool_for_drops, - )?; - Ok(Value::Table(table)) - } -} - -impl FromLua for Block { - fn from_lua(value: Value, _lua: &Lua) -> Result { - if let Value::Table(table) = value { - Ok(Self { - id: table.get("id")?, - friction: table.get("friction")?, - jump_factor: table.get("jump_factor")?, - destroy_time: table.get("destroy_time")?, - explosion_resistance: table.get("explosion_resistance")?, - requires_correct_tool_for_drops: table.get("requires_correct_tool_for_drops")?, - }) - } else { - Err(mlua::Error::FromLuaConversionError { - from: value.type_name(), - to: "Block".to_string(), - message: None, - }) - } - } -} +use mlua::{Function, Lua, Result, Table}; pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> { globals.set( @@ -61,21 +14,24 @@ pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> { Ok(()) } -pub fn get_block_from_state(_lua: &Lua, state: u32) -> Result> { +pub fn get_block_from_state(lua: &Lua, state: u32) -> Result> { let Ok(state) = BlockState::try_from(state) else { return Ok(None); }; - let block: Box = state.into(); - let behavior = block.behavior(); + let b: Box = state.into(); + let bh = b.behavior(); - Ok(Some(Block { - id: block.id().to_string(), - friction: behavior.friction, - jump_factor: behavior.jump_factor, - destroy_time: behavior.destroy_time, - explosion_resistance: behavior.explosion_resistance, - requires_correct_tool_for_drops: behavior.requires_correct_tool_for_drops, - })) + let block = lua.create_table()?; + block.set("id", b.id())?; + block.set("friction", bh.friction)?; + block.set("jump_factor", bh.jump_factor)?; + block.set("destroy_time", bh.destroy_time)?; + block.set("explosion_resistance", bh.explosion_resistance)?; + block.set( + "requires_correct_tool_for_drops", + bh.requires_correct_tool_for_drops, + )?; + Ok(Some(block)) } pub fn get_block_states( diff --git a/src/lua/client/mod.rs b/src/lua/client/mod.rs index bf25489..81a1c48 100644 --- a/src/lua/client/mod.rs +++ b/src/lua/client/mod.rs @@ -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; diff --git a/src/lua/client/state.rs b/src/lua/client/state.rs index 1f7a34a..1ed1c7e 100644 --- a/src/lua/client/state.rs +++ b/src/lua/client/state.rs @@ -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 { Ok(client.inner.as_ref().unwrap().health()) } -pub fn hunger(_lua: &Lua, client: &Client) -> Result { +pub fn hunger(lua: &Lua, client: &Client) -> Result { 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 { diff --git a/src/lua/client/world.rs b/src/lua/client/world.rs index 2049cb4..46c4f33 100644 --- a/src/lua/client/world.rs +++ b/src/lua/client/world.rs @@ -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> { +pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result> { 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>(); 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::(entity.clone())? { + if filter_fn.call::(&entity)? { matched.push(entity); } } @@ -101,22 +103,28 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result Result> { +pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result> { #[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 + }, + ) } diff --git a/src/lua/entity.rs b/src/lua/entity.rs deleted file mode 100644 index 8d8a881..0000000 --- a/src/lua/entity.rs +++ /dev/null @@ -1,43 +0,0 @@ -use super::vec3::Vec3; -use mlua::{FromLua, IntoLua, Lua, Result, Value}; - -#[derive(Clone)] -pub struct Entity { - pub id: u32, - pub uuid: String, - pub kind: String, - pub position: Vec3, - pub custom_name: Option, -} - -impl IntoLua for Entity { - fn into_lua(self, lua: &Lua) -> Result { - let entity = lua.create_table()?; - entity.set("id", self.id)?; - entity.set("uuid", self.uuid)?; - entity.set("kind", self.kind)?; - entity.set("position", self.position)?; - entity.set("custom_name", self.custom_name)?; - Ok(Value::Table(entity)) - } -} - -impl FromLua for Entity { - fn from_lua(value: Value, _lua: &Lua) -> Result { - if let Value::Table(table) = value { - Ok(Self { - id: table.get("id")?, - uuid: table.get("uuid")?, - kind: table.get("kind")?, - position: table.get("position")?, - custom_name: table.get("custom_name")?, - }) - } else { - Err(mlua::Error::FromLuaConversionError { - from: value.type_name(), - to: "Position".to_string(), - message: None, - }) - } - } -} diff --git a/src/lua/fluid_state.rs b/src/lua/fluid_state.rs deleted file mode 100644 index 6233f75..0000000 --- a/src/lua/fluid_state.rs +++ /dev/null @@ -1,36 +0,0 @@ -use mlua::{FromLua, IntoLua, Lua, Result, Value}; - -#[derive(Clone)] -pub struct FluidState { - pub kind: u8, - pub amount: u8, - pub falling: bool, -} - -impl IntoLua for FluidState { - fn into_lua(self, lua: &Lua) -> Result { - let table = lua.create_table()?; - table.set("kind", self.kind)?; - table.set("amount", self.amount)?; - table.set("falling", self.falling)?; - Ok(Value::Table(table)) - } -} - -impl FromLua for FluidState { - fn from_lua(value: Value, _lua: &Lua) -> Result { - if let Value::Table(table) = value { - Ok(Self { - kind: table.get("kind")?, - amount: table.get("amount")?, - falling: table.get("falling")?, - }) - } else { - Err(mlua::Error::FromLuaConversionError { - from: value.type_name(), - to: "FluidState".to_string(), - message: None, - }) - } - } -} diff --git a/src/lua/hunger.rs b/src/lua/hunger.rs deleted file mode 100644 index fd74f59..0000000 --- a/src/lua/hunger.rs +++ /dev/null @@ -1,33 +0,0 @@ -use mlua::{FromLua, IntoLua, Lua, Result, Value}; - -#[derive(Clone)] -pub struct Hunger { - pub food: u32, - pub saturation: f32, -} - -impl IntoLua for Hunger { - fn into_lua(self, lua: &Lua) -> Result { - let table = lua.create_table()?; - table.set("food", self.food)?; - table.set("saturation", self.saturation)?; - Ok(Value::Table(table)) - } -} - -impl FromLua for Hunger { - fn from_lua(value: Value, _lua: &Lua) -> Result { - if let Value::Table(table) = value { - Ok(Self { - food: table.get("food")?, - saturation: table.get("saturation")?, - }) - } else { - Err(mlua::Error::FromLuaConversionError { - from: value.type_name(), - to: "Hunger".to_string(), - message: None, - }) - } - } -} diff --git a/src/lua/mod.rs b/src/lua/mod.rs index 827ec15..59bfbfa 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -2,9 +2,6 @@ pub mod block; pub mod client; pub mod container; pub mod direction; -pub mod entity; -pub mod fluid_state; -pub mod hunger; pub mod logging; pub mod vec3; diff --git a/src/lua/vec3.rs b/src/lua/vec3.rs index 45cdbe5..4ac8385 100644 --- a/src/lua/vec3.rs +++ b/src/lua/vec3.rs @@ -28,7 +28,7 @@ impl FromLua for Vec3 { } else { Err(mlua::Error::FromLuaConversionError { from: value.type_name(), - to: "Position".to_string(), + to: "Vec3".to_string(), message: None, }) }