From 75b425ffd77b85ba3081ddf2e47f8b6695ec8fa5 Mon Sep 17 00:00:00 2001 From: NO11 Date: Fri, 23 Jul 2021 12:23:30 +0000 Subject: [PATCH 1/7] Fix #1842 make other mods not using "mineclone" name space for item ids --- mods/HELP/mcl_item_id/init.lua | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index 3b3128f26..50247a858 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -1,4 +1,5 @@ local game = "mineclone" +local mcl_mods = {} local same_id = { heads = { "skeleton", "zombie", "creeper", "wither_skeleton" }, @@ -10,17 +11,34 @@ local same_id = { "stonebrick", "stonebrickmossy", }, wool = { - "black", "blue", "brown", "cyan", "green", + "black", "blue", "brown", "cyan", "green", "grey", "light_blue", "lime", "magenta", "orange", "pink", "purple", "red", "silver", "white", "yellow", }, } +local worldmt = io.open(minetest.get_worldpath() .. "/world.mt", "r") +local gameid = worldmt:read("*a"):match("gameid%s*=%s*(%S+)\n") +worldmt:close() + +for _, mod in pairs(minetest.get_modnames()) do + if minetest.get_modpath(mod):match("/games/" .. gameid .. "/") then + table.insert(mcl_mods, mod) + end +end + +local function item_id(id) + if minetest.settings:get_bool("mcl_item_id_debug", false) then + return id, "#555555" + end +end + tt.register_snippet(function(itemstring) local def = minetest.registered_items[itemstring] local desc = def.description local item_split = itemstring:find(":") local new_id = game .. itemstring:sub(item_split) + local mcl_mod = itemstring:sub(1, item_split) for mod, ids in pairs(same_id) do for _, id in pairs(ids) do if itemstring == "mcl_" .. mod .. ":" .. id then @@ -28,12 +46,15 @@ tt.register_snippet(function(itemstring) end end end - if new_id ~= game .. ":book_enchanted" then - minetest.register_alias_force(new_id, itemstring) - end - if minetest.settings:get_bool("mcl_item_id_debug", false) then - return new_id, "#555555" + for _, modname in pairs(mcl_mods) do + if modname .. ":" == mcl_mod then + if new_id ~= game .. ":book_enchanted" and new_id ~= itemstring then + minetest.register_alias_force(new_id, itemstring) + end + return item_id(new_id) + end end + return item_id(itemstring) end) minetest.register_alias_force(game .. ":book_enchanted", "mcl_enchanting:book_enchanted") From 09a68443cd641ed73631cc076916616e518402ea Mon Sep 17 00:00:00 2001 From: NO11 Date: Fri, 23 Jul 2021 16:12:43 +0000 Subject: [PATCH 2/7] Better fix for #1842 (make other mods not using "mineclone" name space for item ids) --- mods/HELP/mcl_item_id/init.lua | 57 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index 50247a858..4e9c7c9f1 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -1,5 +1,20 @@ +mcl_item_id = {} + local game = "mineclone" -local mcl_mods = {} + +function mcl_item_id.set_mod_namespace(modname, namespace) + local namespace = namespace or modname + mcl_item_id[modname .. "_namespace"] = namespace +end + +function mcl_item_id.get_mod_namespace(modname) + local namespace = mcl_item_id[modname .. "_namespace"] + if namespace then + return namespace + else + return "" + end +end local same_id = { heads = { "skeleton", "zombie", "creeper", "wither_skeleton" }, @@ -17,28 +32,15 @@ local same_id = { }, } -local worldmt = io.open(minetest.get_worldpath() .. "/world.mt", "r") -local gameid = worldmt:read("*a"):match("gameid%s*=%s*(%S+)\n") -worldmt:close() - -for _, mod in pairs(minetest.get_modnames()) do - if minetest.get_modpath(mod):match("/games/" .. gameid .. "/") then - table.insert(mcl_mods, mod) - end -end - -local function item_id(id) - if minetest.settings:get_bool("mcl_item_id_debug", false) then - return id, "#555555" - end -end - tt.register_snippet(function(itemstring) local def = minetest.registered_items[itemstring] local desc = def.description local item_split = itemstring:find(":") - local new_id = game .. itemstring:sub(item_split) - local mcl_mod = itemstring:sub(1, item_split) + local id_part1 = itemstring:sub(1, item_split) + local id_part2 = itemstring:sub(item_split) + local modname = id_part1:gsub("%:", "") + local new_id = game .. id_part2 + local mod_namespace = mcl_item_id.get_mod_namespace(modname) for mod, ids in pairs(same_id) do for _, id in pairs(ids) do if itemstring == "mcl_" .. mod .. ":" .. id then @@ -46,15 +48,16 @@ tt.register_snippet(function(itemstring) end end end - for _, modname in pairs(mcl_mods) do - if modname .. ":" == mcl_mod then - if new_id ~= game .. ":book_enchanted" and new_id ~= itemstring then - minetest.register_alias_force(new_id, itemstring) - end - return item_id(new_id) - end + + if mod_namespace then + new_id = mod_namespace .. id_part2 + end + if new_id ~= game .. ":book_enchanted" then + minetest.register_alias_force(new_id, itemstring) + end + if minetest.settings:get_bool("mcl_item_id_debug", false) then + return new_id, "#555555" end - return item_id(itemstring) end) minetest.register_alias_force(game .. ":book_enchanted", "mcl_enchanting:book_enchanted") From e44e9eaf623809bc2fa4c617dd6e9629aa5b3879 Mon Sep 17 00:00:00 2001 From: NO11 Date: Fri, 23 Jul 2021 21:35:10 +0000 Subject: [PATCH 3/7] Fix typo --- mods/HELP/mcl_item_id/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index 4e9c7c9f1..911d8225b 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -12,7 +12,7 @@ function mcl_item_id.get_mod_namespace(modname) if namespace then return namespace else - return "" + return end end From c05e57efb1f3d55c89354c28a84e76abe63aadd5 Mon Sep 17 00:00:00 2001 From: NO11 Date: Sat, 24 Jul 2021 14:09:47 +0000 Subject: [PATCH 4/7] Fix some crashes with set_mod_namespace and bugs --- mods/HELP/mcl_item_id/init.lua | 46 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index 911d8225b..9a2f926e8 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -1,22 +1,38 @@ -mcl_item_id = {} +mcl_item_id = { + mod_namespaces = {}, +} local game = "mineclone" function mcl_item_id.set_mod_namespace(modname, namespace) local namespace = namespace or modname - mcl_item_id[modname .. "_namespace"] = namespace + mcl_item_id.mod_namespaces[modname] = namespace + minetest.register_on_mods_loaded(function() + for item, def in pairs(minetest.registered_items) do + local item_split = item:find(":") + if item_split then + local id_modname = item:sub(1, item_split - 1) + local id_string = item:sub(item_split) + if id_modname == modname then + minetest.register_alias_force(namespace .. id_string, item) + end + end + end + end) end function mcl_item_id.get_mod_namespace(modname) - local namespace = mcl_item_id[modname .. "_namespace"] + local namespace = mcl_item_id.mod_namespaces[modname] if namespace then return namespace else - return + return game end end local same_id = { + enchanting = { "table" }, + experience = { "bottle" }, heads = { "skeleton", "zombie", "creeper", "wither_skeleton" }, mobitems = { "rabbit", "chicken" }, walls = { @@ -34,13 +50,11 @@ local same_id = { tt.register_snippet(function(itemstring) local def = minetest.registered_items[itemstring] - local desc = def.description local item_split = itemstring:find(":") - local id_part1 = itemstring:sub(1, item_split) - local id_part2 = itemstring:sub(item_split) - local modname = id_part1:gsub("%:", "") - local new_id = game .. id_part2 - local mod_namespace = mcl_item_id.get_mod_namespace(modname) + local id_string = itemstring:sub(item_split) + local id_modname = itemstring:sub(1, item_split - 1) + local new_id = game .. id_string + local mod_namespace = mcl_item_id.get_mod_namespace(id_modname) for mod, ids in pairs(same_id) do for _, id in pairs(ids) do if itemstring == "mcl_" .. mod .. ":" .. id then @@ -48,16 +62,12 @@ tt.register_snippet(function(itemstring) end end end - - if mod_namespace then - new_id = mod_namespace .. id_part2 - end - if new_id ~= game .. ":book_enchanted" then + if mod_namespace ~= game then + new_id = mod_namespace .. id_string + else minetest.register_alias_force(new_id, itemstring) end if minetest.settings:get_bool("mcl_item_id_debug", false) then return new_id, "#555555" end -end) - -minetest.register_alias_force(game .. ":book_enchanted", "mcl_enchanting:book_enchanted") +end) \ No newline at end of file From 65d33b935ab23e6a43b069c62124d51ead05c165 Mon Sep 17 00:00:00 2001 From: NO11 Date: Sat, 24 Jul 2021 14:45:55 +0000 Subject: [PATCH 5/7] Add API-md for `mcl_item_id` --- mods/HELP/mcl_item_id/API.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 mods/HELP/mcl_item_id/API.md diff --git a/mods/HELP/mcl_item_id/API.md b/mods/HELP/mcl_item_id/API.md new file mode 100644 index 000000000..a2f244e0c --- /dev/null +++ b/mods/HELP/mcl_item_id/API.md @@ -0,0 +1,24 @@ +# mcl_item_id +Show the item ID of an item in the description. +With this API, you can register a different name space than "mineclone" for your mod. + +## mcl_item_id.set_mod_namespace(modname, namespace) +Set a name space for all items in a mod. + +* param1: the modname +* param2: (optional) string of the desired name space, if nil, it is the name of the mod + +## mcl_item_id.get_mod_namespace(modname) +Get the name space of a mod registered with mcl_item_id.set_mod_namespace(modname, namespace). + +* param1: the modname + +### Examples: + +The name of the mod is "mod" which registered an item called "mod:itemname". + +* mcl_item_id.set_mod_namespace("mod", "mymod") will show "mymod:itemname" in the description of "mod:itemname" +* mcl_item_id.set_mod_namespace(minetest.get_current_modname()) will show "mod:itemname" in the description of "mod:itemname" +* mcl_item_id.get_mod_namespace(minetest.get_current_modname()) will return "mod" + +(If no namespace is set by a mod, mcl_item_id.get_mod_namespace(minetest.get_current_modname()) will return "mineclone") From 5c5c405ccf92762af6f0757d4a3b015ff14d0d37 Mon Sep 17 00:00:00 2001 From: NO11 Date: Sat, 24 Jul 2021 15:19:10 +0000 Subject: [PATCH 6/7] Add missing check --- mods/HELP/mcl_item_id/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index 9a2f926e8..e6df1af03 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -13,7 +13,7 @@ function mcl_item_id.set_mod_namespace(modname, namespace) if item_split then local id_modname = item:sub(1, item_split - 1) local id_string = item:sub(item_split) - if id_modname == modname then + if id_modname == modname and modname ~= namespace then minetest.register_alias_force(namespace .. id_string, item) end end From 4846076c8fc2555dff12bf148c5b1d83ab39ec9d Mon Sep 17 00:00:00 2001 From: NO11 Date: Sat, 24 Jul 2021 19:07:44 +0000 Subject: [PATCH 7/7] `mcl_item_id` simplify code --- mods/HELP/mcl_item_id/init.lua | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/mods/HELP/mcl_item_id/init.lua b/mods/HELP/mcl_item_id/init.lua index e6df1af03..f3e6d2735 100644 --- a/mods/HELP/mcl_item_id/init.lua +++ b/mods/HELP/mcl_item_id/init.lua @@ -7,18 +7,6 @@ local game = "mineclone" function mcl_item_id.set_mod_namespace(modname, namespace) local namespace = namespace or modname mcl_item_id.mod_namespaces[modname] = namespace - minetest.register_on_mods_loaded(function() - for item, def in pairs(minetest.registered_items) do - local item_split = item:find(":") - if item_split then - local id_modname = item:sub(1, item_split - 1) - local id_string = item:sub(item_split) - if id_modname == modname and modname ~= namespace then - minetest.register_alias_force(namespace .. id_string, item) - end - end - end - end) end function mcl_item_id.get_mod_namespace(modname) @@ -64,7 +52,8 @@ tt.register_snippet(function(itemstring) end if mod_namespace ~= game then new_id = mod_namespace .. id_string - else + end + if mod_namespace ~= id_modname then minetest.register_alias_force(new_id, itemstring) end if minetest.settings:get_bool("mcl_item_id_debug", false) then