Skip to content

Commit

Permalink
Improve node name normalization again
Browse files Browse the repository at this point in the history
so that "desert stone" won't select "desert stone block"
  • Loading branch information
sfan5 committed May 12, 2020
1 parent 4918610 commit 867cd6e
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions worldedit_commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -164,25 +166,39 @@ 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
end
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
Expand Down

0 comments on commit 867cd6e

Please sign in to comment.