1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Warn if metatable passed to itemdef registration function

This commit is contained in:
sfan5 2025-03-16 16:46:37 +01:00
parent 4125ce877d
commit 2540667f04
3 changed files with 12 additions and 6 deletions

View file

@ -32,8 +32,8 @@ do
all.registered_craftitems = {}
all.registered_tools = {}
for k, v in pairs(all.registered_items) do
-- Disable further modification
setmetatable(v, {__newindex = {}})
-- Ignore new keys
setmetatable(v, {__newindex = function() end})
-- Reassemble the other tables
if v.type == "node" then
getmetatable(v).__index = all.nodedef_default

View file

@ -9,8 +9,8 @@ do
all.registered_craftitems = {}
all.registered_tools = {}
for k, v in pairs(all.registered_items) do
-- Disable further modification
setmetatable(v, {__newindex = {}})
-- Ignore new keys
setmetatable(v, {__newindex = function() end})
-- Reassemble the other tables
if v.type == "node" then
getmetatable(v).__index = all.nodedef_default

View file

@ -139,6 +139,12 @@ function core.register_item(name, itemdef)
end
itemdef.name = name
local mt = getmetatable(itemdef)
if mt ~= nil and next(mt) ~= nil then
core.log("warning", "Item definition has a metatable, this is "..
"unsupported and it will be overwritten: " .. name)
end
-- Apply defaults and add to registered_* table
if itemdef.type == "node" then
-- Use the nodebox as selection box if it's not set manually
@ -194,8 +200,8 @@ function core.register_item(name, itemdef)
itemdef.mod_origin = core.get_current_modname() or "??"
-- Disable all further modifications
getmetatable(itemdef).__newindex = {}
-- Ignore new keys as a failsafe to prevent mistakes
getmetatable(itemdef).__newindex = function() end
--core.log("Registering item: " .. itemdef.name)
core.registered_items[itemdef.name] = itemdef