refactor(movement): clean up goto matching with macro

This commit is contained in:
Ryan 2025-03-07 18:13:05 -05:00
parent 73e7e17da4
commit 0ea2c81d2a
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3

View File

@ -25,7 +25,7 @@ pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
pub async fn go_to( pub async fn go_to(
lua: Lua, lua: Lua,
client: UserDataRef<Client>, client: UserDataRef<Client>,
(data, metadata): (Value, Option<Table>), (data, metadata): (Table, Option<Table>),
) -> Result<()> { ) -> Result<()> {
fn goto_with_options<G: Goal + Send + Sync + 'static>( fn goto_with_options<G: Goal + Send + Sync + 'static>(
client: &Client, client: &Client,
@ -39,78 +39,48 @@ pub async fn go_to(
} }
} }
fn goto<G: Goal + Send + Sync + 'static>(client: &Client, options: Table, goal: G) { let table = metadata.unwrap_or(lua.create_table()?);
if options.get("inverse").unwrap_or_default() { let goal_type = table.get("type").unwrap_or_default();
goto_with_options(client, &options, InverseGoal(goal)); let options = table.get("options").unwrap_or(lua.create_table()?);
} else {
goto_with_options(client, &options, goal);
}
}
let error = mlua::Error::FromLuaConversionError { macro_rules! goto {
from: data.type_name(), ($goal:expr) => {
to: "Table".to_string(), if options.get("inverse").unwrap_or_default() {
message: None, goto_with_options(&client, &options, InverseGoal($goal));
};
let (goal_type, options) = if let Some(metadata) = metadata {
(
metadata.get("type").unwrap_or_default(),
metadata.get("options").unwrap_or(lua.create_table()?),
)
} else { } else {
(0, lua.create_table()?) goto_with_options(&client, &options, $goal);
}
}; };
}
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
match goal_type { match goal_type {
1 => { 1 => {
let t = data.as_table().ok_or(error)?; let p = Vec3::from_lua(data.get("position")?, &lua)?;
let p = Vec3::from_lua(t.get("position")?, &lua)?; goto!(RadiusGoal {
goto(
&client,
options,
RadiusGoal {
pos: azalea::Vec3::new(p.x, p.y, p.z), pos: azalea::Vec3::new(p.x, p.y, p.z),
radius: t.get("radius")?, radius: data.get("radius")?,
}, });
);
} }
2 => { 2 => {
let p = Vec3::from_lua(data, &lua)?; let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto( goto!(ReachBlockPosGoal {
&client,
options,
ReachBlockPosGoal {
pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32), pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32),
chunk_storage: client.world().read().chunks.clone(), chunk_storage: client.world().read().chunks.clone(),
}, });
);
} }
3 => { 3 => {
let t = data.as_table().ok_or(error)?; goto!(XZGoal {
goto( x: data.get("x")?,
&client, z: data.get("z")?,
options, });
XZGoal {
x: t.get("x")?,
z: t.get("z")?,
},
);
} }
4 => goto( 4 => goto!(YGoal { y: data.get("y")? }),
&client,
options,
YGoal {
y: data.as_table().ok_or(error)?.get("y")?,
},
),
_ => { _ => {
let p = Vec3::from_lua(data, &lua)?; let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto( goto!(BlockPosGoal(BlockPos::new(
&client, p.x as i32, p.y as i32, p.z as i32
options, )));
BlockPosGoal(BlockPos::new(p.x as i32, p.y as i32, p.z as i32)),
);
} }
} }