refactor: directly wrap structs

This commit is contained in:
2025-03-12 18:28:30 -04:00
parent 44f7b9a00f
commit 85729401e5
8 changed files with 50 additions and 80 deletions

View File

@@ -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<azalea::inventory::ItemStack> 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: UserDataFields<Self>>(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::<CustomName>()
.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::<Damage>().map(|d| d.amount)))
});
f.add_field_method_get("max_damage", |_, this| {
Ok(this
.inner
.0
.as_present()
.map(|data| data.components.get::<MaxDamage>().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::<Food>())
{
@@ -59,10 +54,10 @@ impl UserData for ItemStack {
fn add_methods<M: UserDataMethods<Self>>(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(())
});
}

View File

@@ -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: UserDataFields<Self>>(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::<Vec<_>>()
}))
});
@@ -31,7 +29,7 @@ impl UserData for Container {
m.add_method(
"click",
|_, this, (operation, operation_type): (Table, Option<u8>)| {
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: UserDataFields<Self>>(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::<Vec<_>>()
}))
});
@@ -62,14 +58,14 @@ impl UserData for ContainerRef {
fn add_methods<M: UserDataMethods<Self>>(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<u8>)| {
this.inner
this.0
.click(operation_from_table(operation, operation_type)?);
Ok(())
},