From aa33166386f737f213f1f3005ffd6a6adfd2d97f Mon Sep 17 00:00:00 2001 From: paly2 Date: Sun, 10 Jul 2016 15:15:43 +0200 Subject: Add minetest.unregister_item and minetest.register_alias_force --- builtin/game/register.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'builtin/game/register.lua') diff --git a/builtin/game/register.lua b/builtin/game/register.lua index f330491a2..05dc5fef8 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -7,6 +7,9 @@ local register_item_raw = core.register_item_raw core.register_item_raw = nil +local unregister_item_raw = core.unregister_item_raw +core.unregister_item_raw = nil + local register_alias_raw = core.register_alias_raw core.register_alias_raw = nil @@ -172,6 +175,27 @@ function core.register_item(name, itemdef) register_item_raw(itemdef) end +function core.unregister_item(name) + if not core.registered_items[name] then + core.log("warning", "Not unregistering item " ..name.. + " because it doesn't exist.") + return + end + -- Erase from registered_* table + local type = core.registered_items[name].type + if type == "node" then + core.registered_nodes[name] = nil + elseif type == "craft" then + core.registered_craftitems[name] = nil + elseif type == "tool" then + core.registered_tools[name] = nil + end + core.registered_items[name] = nil + + + unregister_item_raw(name) +end + function core.register_node(name, nodedef) nodedef.type = "node" core.register_item(name, nodedef) @@ -242,6 +266,20 @@ function core.register_alias(name, convert_to) end end +function core.register_alias_force(name, convert_to) + if forbidden_item_names[name] then + error("Unable to register alias: Name is forbidden: " .. name) + end + if core.registered_items[name] ~= nil then + core.unregister_item(name) + core.log("info", "Removed item " ..name.. + " while attempting to force add an alias") + end + --core.log("Registering alias: " .. name .. " -> " .. convert_to) + core.registered_aliases[name] = convert_to + register_alias_raw(name, convert_to) +end + function core.on_craft(itemstack, player, old_craft_list, craft_inv) for _, func in ipairs(core.registered_on_crafts) do itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack -- cgit v1.2.3 From 3aefa5d3ceaaa9299f42cc10921dec65fa53c5e0 Mon Sep 17 00:00:00 2001 From: paramat Date: Thu, 15 Sep 2016 22:40:19 +0100 Subject: Register.lua: Throw error if node 'light_source' > core.LIGHT_MAX Add 'core.LIGHT_MAX = 14' to builtin/game/constants.lua with the intention to replace misplaced 'default.LIGHT_MAX = 14' in Minetest Game. Add comment in light.h requiring the constant be changed in both places. Add lighting bug warning to note in lua_api.txt. There are hundreds of mod uses of 15 which causes a lighting bug. --- builtin/game/constants.lua | 5 +++++ builtin/game/register.lua | 3 +++ doc/lua_api.txt | 5 ++++- src/light.h | 6 ++++-- 4 files changed, 16 insertions(+), 3 deletions(-) (limited to 'builtin/game/register.lua') diff --git a/builtin/game/constants.lua b/builtin/game/constants.lua index 56fca9289..50c515b24 100644 --- a/builtin/game/constants.lua +++ b/builtin/game/constants.lua @@ -19,4 +19,9 @@ core.EMERGE_FROM_DISK = 3 core.EMERGE_GENERATED = 4 -- constants.h +-- Size of mapblocks in nodes core.MAP_BLOCKSIZE = 16 + +-- light.h +-- Maximum value for node 'light_source' parameter +core.LIGHT_MAX = 14 diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 05dc5fef8..d3f6b3df8 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -127,6 +127,9 @@ function core.register_item(name, itemdef) fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}, } end + if itemdef.light_source and itemdef.light_source > core.LIGHT_MAX then + error("Unable to register node: 'light_source' exceeds maximum: " .. name) + end setmetatable(itemdef, {__index = core.nodedef_default}) core.registered_nodes[itemdef.name] = itemdef elseif itemdef.type == "craft" then diff --git a/doc/lua_api.txt b/doc/lua_api.txt index b6e6520b2..6f69360c8 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3664,7 +3664,10 @@ Definition tables ^ Don't forget to use "leveled" type nodebox. ]] liquid_range = 8, -- number of flowing nodes around source (max. 8) drowning = 0, -- Player will take this amount of damage if no bubbles are left - light_source = 0, -- Amount of light emitted by node (max. 14) + light_source = 0, --[[ + ^ Amount of light emitted by node. + ^ To set the maximum (currently 14), use the value 'minetest.LIGHT_MAX'. + ^ A value outside the range 0 to minetest.LIGHT_MAX causes undefined behavior.]] damage_per_second = 0, -- If player is inside node, this damage is caused node_box = {type="regular"}, -- See "Node boxes" connects_to = nodenames, --[[ diff --git a/src/light.h b/src/light.h index f49be4518..984e6d7c2 100644 --- a/src/light.h +++ b/src/light.h @@ -27,8 +27,10 @@ with this program; if not, write to the Free Software Foundation, Inc., */ // This directly sets the range of light. -// Actually this is not the real maximum, and this is not the -// brightest. The brightest is LIGHT_SUN. +// Actually this is not the real maximum, and this is not the brightest, the +// brightest is LIGHT_SUN. +// If changed, this constant as defined in builtin/game/constants.lua must +// also be changed. #define LIGHT_MAX 14 // Light is stored as 4 bits, thus 15 is the maximum. // This brightness is reserved for sunlight -- cgit v1.2.3 From 5091cb5ecdac39a49bce2317d7054d69ed43f2c1 Mon Sep 17 00:00:00 2001 From: paramat Date: Wed, 21 Sep 2016 04:25:10 +0100 Subject: Builtin: Change error to warning for light_source > 14 --- builtin/game/register.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'builtin/game/register.lua') diff --git a/builtin/game/register.lua b/builtin/game/register.lua index d3f6b3df8..90f095e9f 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -128,7 +128,9 @@ function core.register_item(name, itemdef) } end if itemdef.light_source and itemdef.light_source > core.LIGHT_MAX then - error("Unable to register node: 'light_source' exceeds maximum: " .. name) + itemdef.light_source = core.LIGHT_MAX + core.log("warning", "Node 'light_source' value exceeds maximum," .. + " limiting to maximum: " ..name) end setmetatable(itemdef, {__index = core.nodedef_default}) core.registered_nodes[itemdef.name] = itemdef -- cgit v1.2.3