feat(item_stack): add custom food component

This commit is contained in:
Ryan 2025-03-08 18:38:51 -05:00
parent f8c9dab689
commit 85e1efd9c1
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3

View File

@ -1,4 +1,4 @@
use azalea::inventory::components::{CustomName, Damage, MaxDamage}; use azalea::inventory::components::{CustomName, Damage, Food, MaxDamage};
use mlua::{UserData, UserDataFields, UserDataMethods}; use mlua::{UserData, UserDataFields, UserDataMethods};
pub struct ItemStack { pub struct ItemStack {
@ -36,6 +36,25 @@ impl UserData for ItemStack {
.as_present() .as_present()
.map(|data| data.components.get::<MaxDamage>().map(|d| d.amount))) .map(|data| data.components.get::<MaxDamage>().map(|d| d.amount)))
}); });
f.add_field_method_get("food", |lua, this| {
Ok(
if let Some(food) = this
.inner
.as_present()
.and_then(|data| data.components.get::<Food>())
{
let table = lua.create_table()?;
table.set("nutrition", food.nutrition)?;
table.set("saturation", food.saturation)?;
table.set("can_always_eat", food.can_always_eat)?;
table.set("eat_seconds", food.eat_seconds)?;
Some(table)
} else {
None
},
)
});
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {