From ea7a3707150c5664aa73ac4c7de08fb606cda2f8 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Fri, 21 Feb 2025 22:09:58 -0500 Subject: [PATCH] refactor: impl From for Vec3 --- src/lua/client/movement.rs | 40 ++++++++------------------------------ src/lua/client/world.rs | 15 ++------------ src/lua/vec3.rs | 31 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/lua/client/movement.rs b/src/lua/client/movement.rs index 06b9ee5..7a208a6 100644 --- a/src/lua/client/movement.rs +++ b/src/lua/client/movement.rs @@ -16,12 +16,7 @@ pub fn direction(_lua: &Lua, client: &Client) -> Result { } pub fn eye_position(_lua: &Lua, client: &Client) -> Result { - let p = client.eye_position(); - Ok(Vec3 { - x: p.x, - y: p.y, - z: p.z, - }) + Ok(Vec3::from(client.eye_position())) } pub async fn goto( @@ -119,21 +114,14 @@ pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> { } pub fn looking_at(lua: &Lua, client: &Client) -> Result> { - let hr = client.component::(); - Ok(if hr.miss { + let r = client.component::(); + Ok(if r.miss { None } else { let result = lua.create_table()?; - result.set( - "position", - Vec3 { - x: f64::from(hr.block_pos.x), - y: f64::from(hr.block_pos.y), - z: f64::from(hr.block_pos.z), - }, - )?; - result.set("inside", hr.inside)?; - result.set("world_border", hr.world_border)?; + result.set("position", Vec3::from(r.block_pos))?; + result.set("inside", r.inside)?; + result.set("world_border", r.world_border)?; Some(result) }) } @@ -166,14 +154,7 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result { pathfinder.set( "is_executing", if let Some(p) = client.get_component::() { - pathfinder.set( - "last_reached_node", - Vec3 { - x: f64::from(p.last_reached_node.x), - y: f64::from(p.last_reached_node.y), - z: f64::from(p.last_reached_node.z), - }, - )?; + pathfinder.set("last_reached_node", Vec3::from(p.last_reached_node))?; pathfinder.set( "last_node_reach_elapsed", p.last_node_reached_at.elapsed().as_millis(), @@ -188,12 +169,7 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result
{ } pub fn position(_lua: &Lua, client: &Client) -> Result { - let p = client.component::(); - Ok(Vec3 { - x: p.x, - y: p.y, - z: p.z, - }) + Ok(Vec3::from(&client.component::())) } pub fn set_direction(_lua: &Lua, client: &mut Client, direction: (f32, f32)) -> Result<()> { diff --git a/src/lua/client/world.rs b/src/lua/client/world.rs index 0000d76..83167c4 100644 --- a/src/lua/client/world.rs +++ b/src/lua/client/world.rs @@ -37,11 +37,7 @@ pub fn find_blocks( set: block_states.iter().map(|&id| BlockState { id }).collect(), }, ) - .map(|p| Vec3 { - x: f64::from(p.x), - y: f64::from(p.y), - z: f64::from(p.z), - }) + .map(Vec3::from) .collect()) } @@ -62,14 +58,7 @@ pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result< 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, - }, - )?; + entity.set("position", Vec3::from(position))?; entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?; if filter_fn.call::(&entity)? { diff --git a/src/lua/vec3.rs b/src/lua/vec3.rs index 5d8e50b..e8f0e97 100644 --- a/src/lua/vec3.rs +++ b/src/lua/vec3.rs @@ -1,3 +1,4 @@ +use azalea::{BlockPos, entity::Position}; use mlua::{FromLua, IntoLua, Lua, Result, Value}; #[derive(Clone)] @@ -17,6 +18,36 @@ impl IntoLua for Vec3 { } } +impl From for Vec3 { + fn from(v: azalea::Vec3) -> Self { + Self { + x: v.x, + y: v.y, + z: v.z, + } + } +} + +impl From<&Position> for Vec3 { + fn from(p: &Position) -> Self { + Self { + x: p.x, + y: p.y, + z: p.z, + } + } +} + +impl From for Vec3 { + fn from(p: BlockPos) -> Self { + Vec3 { + x: f64::from(p.x), + y: f64::from(p.y), + z: f64::from(p.z), + } + } +} + impl FromLua for Vec3 { fn from_lua(value: Value, _lua: &Lua) -> Result { match value {