Skip to content

Commit 504b7bc

Browse files
committed
get component function
1 parent efe4d81 commit 504b7bc

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

autodocs/autodoc.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-01-15T15:24:24.626639200
5+
-- Generated: 2026-01-20T10:55:53.180503
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion
@@ -209,6 +209,11 @@ function ItemStack.get_item_type() end
209209
---@return string
210210
function ItemStack.get_item_id() end
211211

212+
--- Gets a component from this stack as NBT.
213+
---@param component_id string The id of the component to fetch.
214+
---@return table
215+
function ItemStack.get_component(component_id) end
216+
212217

213218
--#endregion
214219

src/main/java/dev/diamond/luafy/script/object/game/ItemStackScriptObject.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,18 @@ public void toTable(ItemStack obj, LuaTableBuilder builder, LuaScript script) {
6262
String key = MetamethodImpl.tostring(args.arg1());
6363

6464
// get the component
65-
DataComponentType<?> type = BuiltInRegistries.DATA_COMPONENT_TYPE.get(Identifier.parse(key)).orElseThrow().value();
65+
DataComponentType<Object> type = (DataComponentType<Object>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(Identifier.parse(key)).orElseThrow().value();
66+
// holy cursed programming
67+
6668
Object component = obj.getComponents().get(type);
6769

6870
// convert component object to nbt and then to a lua table.
6971
// codecs are ass bro
7072

71-
// how do i both know what type component is and not????
7273
DataResult<Tag> result = type.codecOrThrow().encodeStart(NbtOps.INSTANCE, component);
74+
CompoundTag tag = result.getOrThrow().asCompound().orElse(new CompoundTag());
7375

74-
return LuaTableBuilder.fromNbtCompound();
76+
return LuaTableBuilder.fromNbtCompound(tag);
7577
});
7678
}
7779

0 commit comments

Comments
 (0)