1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-25 18:20:38 +01:00

Improve node name normalization

This commit is contained in:
sfan5 2017-08-31 20:38:38 +02:00
parent e680d8087e
commit 2cb3fcde66

View File

@ -23,6 +23,7 @@ local function get_position(name) --position 1 retrieval function for when not u
return pos1 return pos1
end end
-- normalize_nodename wrapper for convenience purposes
local function get_node(name, nodename) local function get_node(name, nodename)
local node = worldedit.normalize_nodename(nodename) local node = worldedit.normalize_nodename(nodename)
if not node then if not node then
@ -36,27 +37,47 @@ function worldedit.player_notify(name, message)
minetest.chat_send_player(name, "WorldEdit -!- " .. message, false) minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)
end end
--determines whether `nodename` is a valid node name, returning a boolean local function string_endswith(full, part)
return full:find(part, 1, true) == #full - #part + 1
end
-- normalizes node "description" `nodename`, returning a string (or nil)
worldedit.normalize_nodename = function(nodename) worldedit.normalize_nodename = function(nodename)
nodename = nodename:gsub("^%s*(.-)%s*$", "%1") nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces
if nodename == "" then return nil end if nodename == "" then return nil end
local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names
if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
if minetest.registered_nodes[fullname] or fullname == "air" then -- full name
return fullname return fullname
end end
for key, value in pairs(minetest.registered_nodes) do for key, value in pairs(minetest.registered_nodes) do
if key:find(":" .. nodename, 1, true) then --found in mod if string_endswith(key, ":" .. nodename) then -- matches name (w/o mod part)
return key return key
end end
end end
nodename = nodename:lower() -- lowercase both for case insensitive comparison nodename = nodename:lower() -- lowercase both for case insensitive comparison
for key, value in pairs(minetest.registered_nodes) do for key, value in pairs(minetest.registered_nodes) do
if value.description:lower() == nodename then --found in description local desc = value.description:lower()
if desc == nodename then -- matches description
return key
end
if string_endswith(desc, " block") and desc == nodename.." block" then
-- fuzzy description match (e.g. "Steel" == "Steel Block")
return key return key
end end
end end
local match = nil
for key, value in pairs(minetest.registered_nodes) do
if value.description:lower():find(nodename, 1, true) ~= nil then
if match ~= nil then
return nil return nil
end end
match = key -- substring description match (only if no ambiguities)
end
end
return match
end
-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1) -- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
function worldedit.player_axis(name) function worldedit.player_axis(name)