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> {
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<Option<Table>> {
let hr = client.component::<HitResultComponent>();
Ok(if hr.miss {
let r = client.component::<HitResultComponent>();
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<Table> {
pathfinder.set(
"is_executing",
if let Some(p) = client.get_component::<ExecutingPath>() {
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<Table> {
}
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
let p = client.component::<Position>();
Ok(Vec3 {
x: p.x,
y: p.y,
z: p.z,
})
Ok(Vec3::from(&client.component::<Position>()))
}
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(),
},
)
.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::<bool>(&entity)? {

View File

@ -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<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 {
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
match value {