]> git.lizzy.rs Git - worldedit.git/commitdiff
Improve node name normalization again
authorsfan5 <sfan5@live.de>
Tue, 12 May 2020 19:53:01 +0000 (21:53 +0200)
committersfan5 <sfan5@live.de>
Tue, 12 May 2020 19:53:01 +0000 (21:53 +0200)
so that "desert stone" won't select "desert stone block"

worldedit_commands/init.lua

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