1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2025-10-19 00:45:39 +02:00

Tune node name normalization

This commit is contained in:
sfan5
2025-10-16 15:24:57 +02:00
parent fe7a552c40
commit 6a902a8040

View File

@@ -30,6 +30,18 @@ local function string_endswith(full, part)
return full:sub(-#part) == part
end
local function make_description_cache()
local t = {}
for key, def in pairs(minetest.registered_nodes) do
local desc = def.short_description or (def.description or ""):gsub("\n.*", "", 1)
desc = strip_escapes(desc):lower()
if def.groups.not_in_creative_inventory ~= 1 and desc ~= "" then
t[key] = desc
end
end
return t
end
local description_cache = nil
-- normalizes node "description" `nodename`, returning a string (or nil)
@@ -39,30 +51,32 @@ worldedit.normalize_nodename = function(nodename)
return nil
end
if nodename:find(" ", 1, true) == nil then
local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
if minetest.registered_nodes[fullname] or fullname == "air" then -- full name
if minetest.registered_nodes[fullname] then -- full name
return fullname
end
nodename = nodename:lower()
end
local match
for key, _ in pairs(minetest.registered_nodes) do
if string_endswith(key:lower(), ":" .. nodename) then -- matches name (w/o mod part)
return key
if string_endswith(key, ":" .. nodename) then
if match then
match = nil
break
end
match = key -- matches name w/o mod part (only if unique)
end
end
if match then
return match
end
nodename = nodename:lower()
if description_cache == nil then
-- cache stripped descriptions
-- Note: since we don't handle translations this will work only in the original
-- language of the description (English)
description_cache = {}
for key, def in pairs(minetest.registered_nodes) do
local desc = def.short_description or (def.description or ""):gsub("\n.*", "", 1)
desc = strip_escapes(desc):lower()
if desc ~= "" then
description_cache[key] = desc
end
end
description_cache = make_description_cache()
end
for key, desc in pairs(description_cache) do
@@ -77,14 +91,16 @@ worldedit.normalize_nodename = function(nodename)
end
end
local match = nil
match = nil
for key, value in pairs(description_cache) do
if value:find(nodename, 1, true) ~= nil then
if match ~= nil then
return nil
if match then
match = nil
break
end
match = key -- substring description match (only if no ambiguities)
match = key -- substring description match (only if unique)
end
end
return match
end