diff --git a/src/events.rs b/src/events.rs index ac9f625..5e70580 100644 --- a/src/events.rs +++ b/src/events.rs @@ -241,12 +241,7 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> .map_err(Error::external) })?, )?; - globals.set( - "client", - client::Client { - inner: Some(client), - }, - )?; + globals.set("client", client::Client(Some(client)))?; call_listeners(state, "init", ()).await; Ok(()) } diff --git a/src/lua/client/container.rs b/src/lua/client/container.rs index 26e5403..2d63a59 100644 --- a/src/lua/client/container.rs +++ b/src/lua/client/container.rs @@ -9,13 +9,11 @@ use log::error; use mlua::{Lua, Result, Table, UserDataRef}; pub fn container(_lua: &Lua, client: &Client) -> Result> { - Ok(client - .get_open_container() - .map(|c| ContainerRef { inner: c })) + Ok(client.get_open_container().map(ContainerRef)) } pub fn held_item(_lua: &Lua, client: &Client) -> Result { - Ok(ItemStack::from(client.component::().held_item())) + Ok(ItemStack(client.component::().held_item())) } pub fn held_slot(_lua: &Lua, client: &Client) -> Result { @@ -26,7 +24,7 @@ pub fn held_slot(_lua: &Lua, client: &Client) -> Result { pub fn menu(lua: &Lua, client: &Client) -> Result { fn from_slot_list(s: SlotList) -> Vec { s.iter() - .map(|i| ItemStack::from(i.to_owned())) + .map(|i| ItemStack(i.to_owned())) .collect::>() } @@ -40,11 +38,11 @@ pub fn menu(lua: &Lua, client: &Client) -> Result
{ offhand, }) => { table.set("type", 0)?; - table.set("craft_result", ItemStack::from(craft_result))?; + table.set("craft_result", ItemStack(craft_result))?; table.set("craft", from_slot_list(craft))?; table.set("armor", from_slot_list(armor))?; table.set("inventory", from_slot_list(inventory))?; - table.set("offhand", ItemStack::from(offhand))?; + table.set("offhand", ItemStack(offhand))?; } Menu::Generic9x3 { contents, player } => { table.set("type", 3)?; @@ -62,7 +60,7 @@ pub fn menu(lua: &Lua, client: &Client) -> Result
{ player, } => { table.set("type", 13)?; - table.set("result", ItemStack::from(result))?; + table.set("result", ItemStack(result))?; table.set("grid", from_slot_list(grid))?; table.set("player", from_slot_list(player))?; } @@ -78,7 +76,7 @@ pub fn menu(lua: &Lua, client: &Client) -> Result
{ } => { table.set("type", 20)?; table.set("payments", from_slot_list(payments))?; - table.set("result", ItemStack::from(result))?; + table.set("result", ItemStack(result))?; table.set("player", from_slot_list(player))?; } Menu::ShulkerBox { contents, player } => { @@ -105,11 +103,11 @@ pub async fn open_container_at( position.z as i32, )) .await - .map(|c| Container { inner: c })) + .map(Container)) } pub fn open_inventory(_lua: &Lua, client: &mut Client, _: ()) -> Result> { - Ok(client.open_inventory().map(|c| Container { inner: c })) + Ok(client.open_inventory().map(Container)) } pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> { diff --git a/src/lua/client/mod.rs b/src/lua/client/mod.rs index cc811de..4d4e514 100644 --- a/src/lua/client/mod.rs +++ b/src/lua/client/mod.rs @@ -14,25 +14,19 @@ use azalea::{Client as AzaleaClient, world::MinecraftEntityId}; use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods}; use std::ops::{Deref, DerefMut}; -pub struct Client { - pub inner: Option, -} +pub struct Client(pub Option); impl Deref for Client { type Target = AzaleaClient; fn deref(&self) -> &Self::Target { - self.inner - .as_ref() - .expect("should have received init event") + self.0.as_ref().expect("should have received init event") } } impl DerefMut for Client { fn deref_mut(&mut self) -> &mut Self::Target { - self.inner - .as_mut() - .expect("should have received init event") + self.0.as_mut().expect("should have received init event") } } diff --git a/src/lua/container/item_stack.rs b/src/lua/container/item_stack.rs index 44f3ab8..936403e 100644 --- a/src/lua/container/item_stack.rs +++ b/src/lua/container/item_stack.rs @@ -1,24 +1,19 @@ -use azalea::inventory::components::{CustomName, Damage, Food, MaxDamage}; +use azalea::inventory::{ + self, + components::{CustomName, Damage, Food, MaxDamage}, +}; use mlua::{UserData, UserDataFields, UserDataMethods}; -pub struct ItemStack { - pub inner: azalea::inventory::ItemStack, -} - -impl From for ItemStack { - fn from(inner: azalea::inventory::ItemStack) -> Self { - Self { inner } - } -} +pub struct ItemStack(pub inventory::ItemStack); impl UserData for ItemStack { fn add_fields>(f: &mut F) { - f.add_field_method_get("is_empty", |_, this| Ok(this.inner.is_empty())); - f.add_field_method_get("is_present", |_, this| Ok(this.inner.is_present())); - f.add_field_method_get("count", |_, this| Ok(this.inner.count())); - f.add_field_method_get("kind", |_, this| Ok(this.inner.kind().to_string())); + f.add_field_method_get("is_empty", |_, this| Ok(this.0.is_empty())); + f.add_field_method_get("is_present", |_, this| Ok(this.0.is_present())); + f.add_field_method_get("count", |_, this| Ok(this.0.count())); + f.add_field_method_get("kind", |_, this| Ok(this.0.kind().to_string())); f.add_field_method_get("custom_name", |_, this| { - Ok(this.inner.as_present().map(|data| { + Ok(this.0.as_present().map(|data| { data.components .get::() .map(|c| c.name.to_string()) @@ -26,13 +21,13 @@ impl UserData for ItemStack { }); f.add_field_method_get("damage", |_, this| { Ok(this - .inner + .0 .as_present() .map(|data| data.components.get::().map(|d| d.amount))) }); f.add_field_method_get("max_damage", |_, this| { Ok(this - .inner + .0 .as_present() .map(|data| data.components.get::().map(|d| d.amount))) }); @@ -40,7 +35,7 @@ impl UserData for ItemStack { f.add_field_method_get("food", |lua, this| { Ok( if let Some(food) = this - .inner + .0 .as_present() .and_then(|data| data.components.get::()) { @@ -59,10 +54,10 @@ impl UserData for ItemStack { fn add_methods>(m: &mut M) { m.add_method_mut("split", |_, this, count: u32| { - Ok(ItemStack::from(this.inner.split(count))) + Ok(ItemStack(this.0.split(count))) }); m.add_method_mut("update_empty", |_, this, (): ()| { - this.inner.update_empty(); + this.0.update_empty(); Ok(()) }); } diff --git a/src/lua/container/mod.rs b/src/lua/container/mod.rs index d87e03e..e1f1f13 100644 --- a/src/lua/container/mod.rs +++ b/src/lua/container/mod.rs @@ -6,22 +6,20 @@ use click::operation_from_table; use item_stack::ItemStack; use mlua::{Table, UserData, UserDataFields, UserDataMethods}; -pub struct Container { - pub inner: ContainerHandle, -} +pub struct Container(pub ContainerHandle); impl UserData for Container { fn add_fields>(f: &mut F) { - f.add_field_method_get("id", |_, this| Ok(this.inner.id())); + f.add_field_method_get("id", |_, this| Ok(this.0.id())); f.add_field_method_get("menu", |_, this| { - Ok(this.inner.menu().map(|m| format!("{m:?}"))) + Ok(this.0.menu().map(|m| format!("{m:?}"))) }); f.add_field_method_get("contents", |_, this| { - Ok(this.inner.contents().map(|v| { + Ok(this.0.contents().map(|v| { v.iter() - .map(|i| ItemStack::from(i.to_owned())) + .map(|i| ItemStack(i.to_owned())) .collect::>() })) }); @@ -31,7 +29,7 @@ impl UserData for Container { m.add_method( "click", |_, this, (operation, operation_type): (Table, Option)| { - this.inner + this.0 .click(operation_from_table(operation, operation_type)?); Ok(()) }, @@ -39,22 +37,20 @@ impl UserData for Container { } } -pub struct ContainerRef { - pub inner: ContainerHandleRef, -} +pub struct ContainerRef(pub ContainerHandleRef); impl UserData for ContainerRef { fn add_fields>(f: &mut F) { - f.add_field_method_get("id", |_, this| Ok(this.inner.id())); + f.add_field_method_get("id", |_, this| Ok(this.0.id())); f.add_field_method_get("menu", |_, this| { - Ok(this.inner.menu().map(|m| format!("{m:?}"))) + Ok(this.0.menu().map(|m| format!("{m:?}"))) }); f.add_field_method_get("contents", |_, this| { - Ok(this.inner.contents().map(|v| { + Ok(this.0.contents().map(|v| { v.iter() - .map(|i| ItemStack::from(i.to_owned())) + .map(|i| ItemStack(i.to_owned())) .collect::>() })) }); @@ -62,14 +58,14 @@ impl UserData for ContainerRef { fn add_methods>(m: &mut M) { m.add_method("close", |_, this, (): ()| { - this.inner.close(); + this.0.close(); Ok(()) }); m.add_method( "click", |_, this, (operation, operation_type): (Table, Option)| { - this.inner + this.0 .click(operation_from_table(operation, operation_type)?); Ok(()) }, diff --git a/src/lua/nochatreports/crypt.rs b/src/lua/nochatreports/crypt.rs index 2952d30..c1b7c33 100644 --- a/src/lua/nochatreports/crypt.rs +++ b/src/lua/nochatreports/crypt.rs @@ -4,7 +4,7 @@ macro_rules! crypt { macro_rules! crypt_with { ($algo:ident) => {{ let encoding = $options.get("encoding").unwrap_or_default(); - let key = &$options.get::>("key")?.inner; + let key = &$options.get::>("key")?.0; match encoding { 1 => $algo::::$op($text, &key), 2 => $algo::::$op($text, &key), diff --git a/src/lua/nochatreports/key.rs b/src/lua/nochatreports/key.rs index adbc7fe..9e4123a 100644 --- a/src/lua/nochatreports/key.rs +++ b/src/lua/nochatreports/key.rs @@ -1,12 +1,10 @@ use mlua::UserData; -pub struct AesKey { - pub inner: ncr::AesKey, -} +pub struct AesKey(pub ncr::AesKey); impl UserData for AesKey { fn add_fields>(f: &mut F) { - f.add_field_method_get("base64", |_, this| Ok(this.inner.encode_base64())); - f.add_field_method_get("bytes", |_, this| Ok(this.inner.as_ref().to_vec())); + f.add_field_method_get("base64", |_, this| Ok(this.0.encode_base64())); + f.add_field_method_get("bytes", |_, this| Ok(this.0.as_ref().to_vec())); } } diff --git a/src/lua/nochatreports/mod.rs b/src/lua/nochatreports/mod.rs index 7a8eebd..cf4722e 100644 --- a/src/lua/nochatreports/mod.rs +++ b/src/lua/nochatreports/mod.rs @@ -14,29 +14,23 @@ pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> { globals.set( "ncr_aes_key_from_passphrase", lua.create_function(|_, passphrase: Vec| { - Ok(AesKey { - inner: ncr::AesKey::gen_from_passphrase(&passphrase), - }) + Ok(AesKey(ncr::AesKey::gen_from_passphrase(&passphrase))) })?, )?; globals.set( "ncr_aes_key_from_base64", lua.create_function(|_, base64: String| { - Ok(AesKey { - inner: ncr::AesKey::decode_base64(&base64) + Ok(AesKey( + ncr::AesKey::decode_base64(&base64) .map_err(|error| Error::external(error.to_string()))?, - }) + )) })?, )?; globals.set( "ncr_generate_random_aes_key", - lua.create_function(|_, (): ()| { - Ok(AesKey { - inner: ncr::AesKey::gen_random_key(), - }) - })?, + lua.create_function(|_, (): ()| Ok(AesKey(ncr::AesKey::gen_random_key())))?, )?; globals.set(