mirror of
https://github.com/Uberi/Minetest-WorldEdit.git
synced 2025-10-18 16:35:32 +02:00
Tune node name normalization
This commit is contained in:
@@ -30,6 +30,18 @@ local function string_endswith(full, part)
|
|||||||
return full:sub(-#part) == part
|
return full:sub(-#part) == part
|
||||||
end
|
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
|
local description_cache = nil
|
||||||
|
|
||||||
-- normalizes node "description" `nodename`, returning a string (or nil)
|
-- normalizes node "description" `nodename`, returning a string (or nil)
|
||||||
@@ -39,30 +51,32 @@ worldedit.normalize_nodename = function(nodename)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
|
if nodename:find(" ", 1, true) == nil then
|
||||||
if minetest.registered_nodes[fullname] or fullname == "air" then -- full name
|
local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
|
||||||
return fullname
|
if minetest.registered_nodes[fullname] then -- full name
|
||||||
end
|
return fullname
|
||||||
nodename = nodename:lower()
|
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local match
|
||||||
|
for key, _ in pairs(minetest.registered_nodes) do
|
||||||
|
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
|
if description_cache == nil then
|
||||||
-- cache stripped descriptions
|
|
||||||
-- Note: since we don't handle translations this will work only in the original
|
-- Note: since we don't handle translations this will work only in the original
|
||||||
-- language of the description (English)
|
-- language of the description (English)
|
||||||
description_cache = {}
|
description_cache = make_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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for key, desc in pairs(description_cache) do
|
for key, desc in pairs(description_cache) do
|
||||||
@@ -77,14 +91,16 @@ worldedit.normalize_nodename = function(nodename)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local match = nil
|
match = nil
|
||||||
for key, value in pairs(description_cache) do
|
for key, value in pairs(description_cache) do
|
||||||
if value:find(nodename, 1, true) ~= nil then
|
if value:find(nodename, 1, true) ~= nil then
|
||||||
if match ~= nil then
|
if match then
|
||||||
return nil
|
match = nil
|
||||||
|
break
|
||||||
end
|
end
|
||||||
match = key -- substring description match (only if no ambiguities)
|
match = key -- substring description match (only if unique)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return match
|
return match
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user