Allow removal of item definition fields (#14675)

This commit is contained in:
Lars Müller 2024-05-20 20:37:40 +02:00 committed by GitHub
parent 8972e829f2
commit 408faa11a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

@ -404,7 +404,7 @@ core.register_item(":", {
}) })
function core.override_item(name, redefinition) function core.override_item(name, redefinition, del_fields)
if redefinition.name ~= nil then if redefinition.name ~= nil then
error("Attempt to redefine name of "..name.." to "..dump(redefinition.name), 2) error("Attempt to redefine name of "..name.." to "..dump(redefinition.name), 2)
end end
@ -418,6 +418,9 @@ function core.override_item(name, redefinition)
for k, v in pairs(redefinition) do for k, v in pairs(redefinition) do
rawset(item, k, v) rawset(item, k, v)
end end
for _, field in ipairs(del_fields or {}) do
rawset(item, field, nil)
end
register_item_raw(item) register_item_raw(item)
end end

@ -5613,11 +5613,17 @@ Call these functions only at load time!
* `minetest.register_node(name, node definition)` * `minetest.register_node(name, node definition)`
* `minetest.register_craftitem(name, item definition)` * `minetest.register_craftitem(name, item definition)`
* `minetest.register_tool(name, item definition)` * `minetest.register_tool(name, item definition)`
* `minetest.override_item(name, redefinition)` * `minetest.override_item(name, redefinition, del_fields)`
* `redefinition` is a table of fields `[name] = new_value`,
overwriting fields of or adding fields to the existing definition.
* `del_fields` is a list of field names to be set
to `nil` ("deleted from") the original definition.
* Overrides fields of an item registered with register_node/tool/craftitem. * Overrides fields of an item registered with register_node/tool/craftitem.
* Note: Item must already be defined, (opt)depend on the mod defining it. * Note: Item must already be defined, (opt)depend on the mod defining it.
* Example: `minetest.override_item("default:mese", * Example: `minetest.override_item("default:mese",
{light_source=minetest.LIGHT_MAX})` {light_source=minetest.LIGHT_MAX}, {"sounds"})`:
Overwrites the `light_source` field,
removes the sounds from the definition of the mese block.
* `minetest.unregister_item(name)` * `minetest.unregister_item(name)`
* Unregisters the item from the engine, and deletes the entry with key * Unregisters the item from the engine, and deletes the entry with key
`name` from `minetest.registered_items` and from the associated item table `name` from `minetest.registered_items` and from the associated item table