1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

itemstack

This commit is contained in:
y5nw 2025-02-22 22:46:44 +01:00
parent 2a1a2b1f9d
commit 54b14c6c62
2 changed files with 7 additions and 18 deletions

View file

@ -31,3 +31,9 @@ function core.register_async_metatable(...)
end
core.register_portable_metatable("__builtin:vector", vector.metatable)
if ItemStack then
local item = ItemStack()
local itemstack_mt = getmetatable(item)
core.register_portable_metatable("__itemstack", itemstack_mt, item.to_table, ItemStack)
end

View file

@ -8,14 +8,6 @@ local next, rawget, pairs, pcall, error, type, setfenv, loadstring
local table_concat, string_dump, string_format, string_match, math_huge
= table.concat, string.dump, string.format, string.match, math.huge
local itemstack_mt
if ItemStack then
itemstack_mt = getmetatable(ItemStack())
end
local function is_itemstack(x)
return itemstack_mt and getmetatable(x) == itemstack_mt
end
local function pack_args(...)
return {n = select("#", ...), ...}
end
@ -82,10 +74,6 @@ local function dump_func(func)
return string_format("loadstring(%q)", string_dump(func))
end
local function dump_itemstack(item)
return string_format("ItemStack(%q)", item:to_string())
end
-- Serializes Lua nil, booleans, numbers, strings, tables and even functions
-- Tables are referenced by reference, strings are referenced by value. Supports circular tables.
local function serialize(value, write)
@ -118,12 +106,10 @@ local function serialize(value, write)
write(dump_func(object))
elseif type_ == "string" then
write(quote(object))
elseif is_itemstack(object) then
write(dump_itemstack(object))
end
write(";")
references[object] = reference
if type_ ~= "string" and not is_itemstack(object) then
if type_ ~= "string" then
to_fill[object] = reference
end
refnum = refnum + 1
@ -189,9 +175,6 @@ local function serialize(value, write)
if type_ == "function" then
return write(dump_func(value))
end
if is_itemstack(value) then
return write(dump_itemstack(value))
end
if type_ == "table" then
write("{")
-- First write list keys: