]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit_commands/init.lua
Fix invalid node error message of //set and //mix
[worldedit.git] / worldedit_commands / init.lua
index 61231c321b3b93fc497951d30e19a13d9a93af5c..a102dbcbf43b7bcb96e699d5204cbabd36d3c6ac 100644 (file)
@@ -23,6 +23,7 @@ local function get_position(name) --position 1 retrieval function for when not u
        return pos1\r
 end\r
 \r
+-- normalize_nodename wrapper for convenience purposes\r
 local function get_node(name, nodename)\r
        local node = worldedit.normalize_nodename(nodename)\r
        if not node then\r
@@ -36,26 +37,46 @@ function worldedit.player_notify(name, message)
        minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)\r
 end\r
 \r
---determines whether `nodename` is a valid node name, returning a boolean\r
+local function string_endswith(full, part)\r
+       return full:find(part, 1, true) == #full - #part + 1\r
+end\r
+\r
+-- normalizes node "description" `nodename`, returning a string (or nil)\r
 worldedit.normalize_nodename = function(nodename)\r
-       nodename = nodename:gsub("^%s*(.-)%s*$", "%1")\r
+       nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces\r
        if nodename == "" then return nil end\r
-       local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names\r
-       if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename\r
+\r
+       local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases\r
+       if minetest.registered_nodes[fullname] or fullname == "air" then -- full name\r
                return fullname\r
        end\r
        for key, value in pairs(minetest.registered_nodes) do\r
-               if key:find(":" .. nodename, 1, true) then --found in mod\r
+               if string_endswith(key, ":" .. nodename) then -- matches name (w/o mod part)\r
                        return key\r
                end\r
        end\r
-       nodename = nodename:lower() --lowercase both for case insensitive comparison\r
+       nodename = nodename:lower() -- lowercase both for case insensitive comparison\r
        for key, value in pairs(minetest.registered_nodes) do\r
-               if value.description:lower() == nodename then --found in description\r
+               local desc = value.description:lower()\r
+               if desc == nodename then -- matches description\r
                        return key\r
                end\r
+               if string_endswith(desc, " block") and desc == nodename.." block" then\r
+                       -- fuzzy description match (e.g. "Steel" == "Steel Block")\r
+                       return key\r
+               end\r
+       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
+                       if match ~= nil then\r
+                               return nil\r
+                       end\r
+                       match = key -- substring description match (only if no ambiguities)\r
+               end\r
        end\r
-       return nil\r
+       return match\r
 end\r
 \r
 -- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)\r
@@ -377,16 +398,32 @@ minetest.register_chatcommand("/set", {
        privs = {worldedit=true},\r
        func = safe_region(function(name, param)\r
                local node = get_node(name, param)\r
-               if not node then\r
-                       worldedit.player_notify(name, "Could not identify node \"" .. param .. "\"")\r
-                       return\r
-               end\r
+               if not node then return end\r
 \r
                local count = worldedit.set(worldedit.pos1[name], worldedit.pos2[name], node)\r
                worldedit.player_notify(name, count .. " nodes set")\r
        end, check_region),\r
 })\r
 \r
+minetest.register_chatcommand("/param2", {\r
+       params = "<param2>",\r
+       description = "Set param2 of all nodes in the current WorldEdit region to <param2>",\r
+       privs = {worldedit=true},\r
+       func = safe_region(function(name, param)\r
+               local param2 = tonumber(param)\r
+               if not param2 then\r
+                       worldedit.player_notify(name, "Invalid or missing param2 argument")\r
+                       return\r
+               elseif param2 < 0 or param2 > 255 then\r
+                       worldedit.player_notify(name, "Param2 is out of range (must be between 0 and 255 inclusive)!")\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.set_param2(worldedit.pos1[name], worldedit.pos2[name], param2)\r
+               worldedit.player_notify(name, count .. " nodes altered")\r
+       end, check_region),\r
+})\r
+\r
 minetest.register_chatcommand("/mix", {\r
        params = "<node1> ...",\r
        description = "Fill the current WorldEdit region with a random mix of <node1>, ...",\r
@@ -395,10 +432,7 @@ minetest.register_chatcommand("/mix", {
                local nodes = {}\r
                for nodename in param:gmatch("[^%s]+") do\r
                        local node = get_node(name, nodename)\r
-                       if not node then\r
-                               worldedit.player_notify(name, "Could not identify node \"" .. name .. "\"")\r
-                               return\r
-                       end\r
+                       if not node then return end\r
                        nodes[#nodes + 1] = node\r
                end\r
 \r