Improve node name normalization again

so that "desert stone" won't select "desert stone block"
This commit is contained in:
sfan5 2020-05-12 21:53:01 +02:00
parent 4918610c43
commit 867cd6edc7
1 changed files with 22 additions and 6 deletions

View File

@ -154,6 +154,8 @@ local function string_endswith(full, part)
return full:find(part, 1, true) == #full - #part + 1 return full:find(part, 1, true) == #full - #part + 1
end end
local description_cache = nil
-- normalizes node "description" `nodename`, returning a string (or nil) -- normalizes node "description" `nodename`, returning a string (or nil)
worldedit.normalize_nodename = function(nodename) worldedit.normalize_nodename = function(nodename)
nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces
@ -164,16 +166,30 @@ worldedit.normalize_nodename = function(nodename)
return fullname return fullname
end end
nodename = nodename:lower() nodename = nodename:lower()
for key, value in pairs(minetest.registered_nodes) do
if string_endswith(key, ":" .. nodename) then -- matches name (w/o mod part) for key, _ in pairs(minetest.registered_nodes) do
if string_endswith(key:lower(), ":" .. nodename) then -- matches name (w/o mod part)
return key return key
end end
end end
for key, value in pairs(minetest.registered_nodes) do
local desc = strip_escapes(value.description):gsub("\n.*", "", 1):lower() if description_cache == nil then
-- cache stripped descriptions
description_cache = {}
for key, value in pairs(minetest.registered_nodes) do
local desc = strip_escapes(value.description):gsub("\n.*", "", 1):lower()
if desc ~= "" then
description_cache[key] = desc
end
end
end
for key, desc in pairs(description_cache) do
if desc == nodename then -- matches description if desc == nodename then -- matches description
return key return key
end end
end
for key, desc in pairs(description_cache) do
if desc == nodename .. " block" then if desc == nodename .. " block" then
-- fuzzy description match (e.g. "Steel" == "Steel Block") -- fuzzy description match (e.g. "Steel" == "Steel Block")
return key return key
@ -181,8 +197,8 @@ worldedit.normalize_nodename = function(nodename)
end end
local match = nil local match = nil
for key, value in pairs(minetest.registered_nodes) do for key, value in pairs(description_cache) do
if value.description:lower():find(nodename, 1, true) ~= nil then if value:find(nodename, 1, true) ~= nil then
if match ~= nil then if match ~= nil then
return nil return nil
end end