refactor: impl From for Vec3

This commit is contained in:
Ryan 2025-02-21 22:09:58 -05:00
parent 6f0f3938a7
commit ea7a370715
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 41 additions and 45 deletions

View File

@ -16,12 +16,7 @@ pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
} }
pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> { pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
let p = client.eye_position(); Ok(Vec3::from(client.eye_position()))
Ok(Vec3 {
x: p.x,
y: p.y,
z: p.z,
})
} }
pub async fn goto( 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<Option<Table>> { pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let hr = client.component::<HitResultComponent>(); let r = client.component::<HitResultComponent>();
Ok(if hr.miss { Ok(if r.miss {
None None
} else { } else {
let result = lua.create_table()?; let result = lua.create_table()?;
result.set( result.set("position", Vec3::from(r.block_pos))?;
"position", result.set("inside", r.inside)?;
Vec3 { result.set("world_border", r.world_border)?;
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)?;
Some(result) Some(result)
}) })
} }
@ -166,14 +154,7 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
pathfinder.set( pathfinder.set(
"is_executing", "is_executing",
if let Some(p) = client.get_component::<ExecutingPath>() { if let Some(p) = client.get_component::<ExecutingPath>() {
pathfinder.set( pathfinder.set("last_reached_node", Vec3::from(p.last_reached_node))?;
"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( pathfinder.set(
"last_node_reach_elapsed", "last_node_reach_elapsed",
p.last_node_reached_at.elapsed().as_millis(), p.last_node_reached_at.elapsed().as_millis(),
@ -188,12 +169,7 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
} }
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> { pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
let p = client.component::<Position>(); Ok(Vec3::from(&client.component::<Position>()))
Ok(Vec3 {
x: p.x,
y: p.y,
z: p.z,
})
} }
pub fn set_direction(_lua: &Lua, client: &mut Client, direction: (f32, f32)) -> Result<()> { pub fn set_direction(_lua: &Lua, client: &mut Client, direction: (f32, f32)) -> Result<()> {

View File

@ -37,11 +37,7 @@ pub fn find_blocks(
set: block_states.iter().map(|&id| BlockState { id }).collect(), set: block_states.iter().map(|&id| BlockState { id }).collect(),
}, },
) )
.map(|p| Vec3 { .map(Vec3::from)
x: f64::from(p.x),
y: f64::from(p.y),
z: f64::from(p.z),
})
.collect()) .collect())
} }
@ -62,14 +58,7 @@ pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<
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( entity.set("position", Vec3::from(position))?;
"position",
Vec3 {
x: position.x,
y: position.y,
z: position.z,
},
)?;
entity.set("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)? { if filter_fn.call::<bool>(&entity)? {

View File

@ -1,3 +1,4 @@
use azalea::{BlockPos, entity::Position};
use mlua::{FromLua, IntoLua, Lua, Result, Value}; use mlua::{FromLua, IntoLua, Lua, Result, Value};
#[derive(Clone)] #[derive(Clone)]
@ -17,6 +18,36 @@ impl IntoLua for Vec3 {
} }
} }
impl From<azalea::Vec3> 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<BlockPos> 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 { impl FromLua for Vec3 {
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> { fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
match value { match value {