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

@@ -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(())
},