From 867cd6edc7d02c6e9fa4a9ae288db3a2b653b33f Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 12 May 2020 21:53:01 +0200 Subject: [PATCH] Improve node name normalization again so that "desert stone" won't select "desert stone block" --- worldedit_commands/init.lua | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 28b61c5..1cdb107 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -154,6 +154,8 @@ local function string_endswith(full, part) return full:find(part, 1, true) == #full - #part + 1 end +local description_cache = nil + -- normalizes node "description" `nodename`, returning a string (or nil) worldedit.normalize_nodename = function(nodename) nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces @@ -164,16 +166,30 @@ worldedit.normalize_nodename = function(nodename) return fullname end 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 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 return key end + end + for key, desc in pairs(description_cache) do if desc == nodename .. " block" then -- fuzzy description match (e.g. "Steel" == "Steel Block") return key @@ -181,8 +197,8 @@ worldedit.normalize_nodename = function(nodename) end local match = nil - for key, value in pairs(minetest.registered_nodes) do - if value.description:lower():find(nodename, 1, true) ~= nil then + for key, value in pairs(description_cache) do + if value:find(nodename, 1, true) ~= nil then if match ~= nil then return nil end