1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-11-16 23:50:18 +01:00

Allow the wielded item to be specified in commands

This commit is contained in:
marius david 2024-05-16 13:31:17 +02:00
parent 5a00c07c68
commit 22728067e9
6 changed files with 75 additions and 45 deletions

View File

@ -39,7 +39,7 @@ local brush_on_use = function(itemstack, placer)
worldedit.marker_update(name) worldedit.marker_update(name)
assert(cmddef.require_pos < 2) assert(cmddef.require_pos < 2)
local parsed = {cmddef.parse(meta:get_string("params"))} local parsed = {cmddef.parse(meta:get_string("params"), name)}
if not table.remove(parsed, 1) then return false end -- shouldn't happen if not table.remove(parsed, 1) then return false end -- shouldn't happen
-- discard success messages -- discard success messages
@ -104,7 +104,7 @@ worldedit.register_command("brush", {
end end
-- Try parsing command params so we can give the user feedback -- Try parsing command params so we can give the user feedback
local ok, err = cmddef.parse(params) local ok, err = cmddef.parse(params, name)
if not ok then if not ok then
err = err or S("invalid usage") err = err or S("invalid usage")
return false, S("Error with command: @1", err) return false, S("Error with command: @1", err)

View File

@ -218,7 +218,7 @@ worldedit.register_command("cubeapply", {
description = S("Select a cube with side length <size> around position 1 and run <command> on region"), description = S("Select a cube with side length <size> around position 1 and run <command> on region"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 1, require_pos = 1,
parse = function(param) parse = function(param, playername)
local found, _, sidex, sidey, sidez, cmd, args = local found, _, sidex, sidey, sidez, cmd, args =
param:find("^(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s*(.*)$") param:find("^(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s*(.*)$")
if found == nil then if found == nil then
@ -241,7 +241,7 @@ worldedit.register_command("cubeapply", {
minetest.colorize("#00ffff", "//"..cmd)) minetest.colorize("#00ffff", "//"..cmd))
end end
-- run parsing of target command -- run parsing of target command
local parsed = {cmddef.parse(args)} local parsed = {cmddef.parse(args, playername)}
if not table.remove(parsed, 1) then if not table.remove(parsed, 1) then
return false, parsed[1] return false, parsed[1]
end end

View File

@ -42,7 +42,7 @@ local function chatcommand_handler(cmd_name, name, param)
end end
end end
local parsed = {def.parse(param)} local parsed = {def.parse(param, name)}
local success = table.remove(parsed, 1) local success = table.remove(parsed, 1)
if not success then if not success then
worldedit.player_notify(name, parsed[1] or S("invalid usage"), "error") worldedit.player_notify(name, parsed[1] or S("invalid usage"), "error")

View File

@ -42,10 +42,10 @@ worldedit.register_command("set", {
category = S("Node manipulation"), category = S("Node manipulation"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 2, require_pos = 2,
parse = function(param) parse = function(param, player)
local node = worldedit.normalize_nodename(param) local node, err = worldedit.normalize_nodename(param, player)
if not node then if not node then
return false, S("invalid node name: @1", param) return false, err
end end
return true, node return true, node
end, end,
@ -84,7 +84,7 @@ worldedit.register_command("mix", {
category = S("Node manipulation"), category = S("Node manipulation"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 2, require_pos = 2,
parse = function(param) parse = function(param, playername)
local nodes = {} local nodes = {}
for nodename in param:gmatch("[^%s]+") do for nodename in param:gmatch("[^%s]+") do
if tonumber(nodename) ~= nil and #nodes > 0 then if tonumber(nodename) ~= nil and #nodes > 0 then
@ -93,9 +93,9 @@ worldedit.register_command("mix", {
nodes[#nodes + 1] = last_node nodes[#nodes + 1] = last_node
end end
else else
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
nodes[#nodes + 1] = node nodes[#nodes + 1] = node
end end
@ -113,18 +113,18 @@ worldedit.register_command("mix", {
end, end,
}) })
local check_replace = function(param) local check_replace = function(param, playername)
local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$") local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local newsearchnode = worldedit.normalize_nodename(searchnode) local newsearchnode, err = worldedit.normalize_nodename(searchnode, playername)
if not newsearchnode then if not newsearchnode then
return false, S("invalid search node name: @1", searchnode) return false, err
end end
local newreplacenode = worldedit.normalize_nodename(replacenode) local newreplacenode, err = worldedit.normalize_nodename(replacenode, playername)
if not newreplacenode then if not newreplacenode then
return false, S("invalid replace node name: @1", replacenode) return false, err
end end
return true, newsearchnode, newreplacenode return true, newsearchnode, newreplacenode
end end
@ -318,10 +318,10 @@ worldedit.register_command("suppress", {
category = S("Node manipulation"), category = S("Node manipulation"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 2, require_pos = 2,
parse = function(param) parse = function(param, playername)
local node = worldedit.normalize_nodename(param) local node, err = worldedit.normalize_nodename(param, playername)
if not node then if not node then
return false, S("invalid node name: @1", param) return false, err
end end
return true, node return true, node
end, end,
@ -338,10 +338,10 @@ worldedit.register_command("highlight", {
category = S("Node manipulation"), category = S("Node manipulation"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 2, require_pos = 2,
parse = function(param) parse = function(param, playername)
local node = worldedit.normalize_nodename(param) local node, err = worldedit.normalize_nodename(param, playername)
if not node then if not node then
return false, S("invalid node name: @1", param) return false, err
end end
return true, node return true, node
end, end,

View File

@ -1,3 +1,5 @@
local S = minetest.get_translator("worldedit_commands")
-- Strips any kind of escape codes (translation, colors) from a string -- Strips any kind of escape codes (translation, colors) from a string
-- https://github.com/minetest/minetest/blob/53dd7819277c53954d1298dfffa5287c306db8d0/src/util/string.cpp#L777 -- https://github.com/minetest/minetest/blob/53dd7819277c53954d1298dfffa5287c306db8d0/src/util/string.cpp#L777
local function strip_escapes(input) local function strip_escapes(input)
@ -32,11 +34,34 @@ end
local description_cache = nil local description_cache = nil
-- normalizes node "description" `nodename`, returning a string (or nil) -- normalizes node "description" `nodename`. Return a string (or nil), with the second return value being the error message if the first value is nil.
worldedit.normalize_nodename = function(nodename) -- playername contain the name of the player. It may be nil, in which case it wont detect the wielded item when appropriate
worldedit.normalize_nodename = function(nodename, playername)
nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces
if nodename == "" then return nil end if nodename == "" then return nil end
if nodename == "wielded" or nodename == "w" then
if not playername then
return nil, S("no player executing command")
end
local player = minetest.get_player_by_name(playername)
if not player then
return nil, S("no player found: @1", playername)
end
local wielded = player:get_wielded_item()
if not wielded then
return nil, S("no wielded item: @1", playername)
end
if minetest.registered_nodes[wielded:get_name()] then
return wielded:get_name()
else
return nil, S("not a node: @1", wielded:get_name())
end
end
local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases 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] or fullname == "air" then -- full name
return fullname return fullname
@ -76,10 +101,15 @@ worldedit.normalize_nodename = function(nodename)
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 ~= nil then
return nil return nil, S("invalid node name: @1", nodename)
end end
match = key -- substring description match (only if no ambiguities) match = key -- substring description match (only if no ambiguities)
end end
end end
return match
if match then
return match
end
return nil, S("invalid node name: @1", nodename)
end end

View File

@ -1,14 +1,14 @@
local S = minetest.get_translator("worldedit_commands") local S = minetest.get_translator("worldedit_commands")
local check_cube = function(param) local check_cube = function(param, playername)
local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, tonumber(w), tonumber(h), tonumber(l), node return true, tonumber(w), tonumber(h), tonumber(l), node
end end
@ -45,14 +45,14 @@ worldedit.register_command("cube", {
end, end,
}) })
local check_sphere = function(param) local check_sphere = function(param, playername)
local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, tonumber(radius), node return true, tonumber(radius), node
end end
@ -89,14 +89,14 @@ worldedit.register_command("sphere", {
end, end,
}) })
local check_dome = function(param) local check_dome = function(param, playername)
local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, tonumber(radius), node return true, tonumber(radius), node
end end
@ -133,7 +133,7 @@ worldedit.register_command("dome", {
end, end,
}) })
local check_cylinder = function(param) local check_cylinder = function(param, playername)
-- two radii -- two radii
local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$") local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then if found == nil then
@ -144,9 +144,9 @@ local check_cylinder = function(param)
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, axis, tonumber(length), tonumber(radius1), tonumber(radius2), node return true, axis, tonumber(length), tonumber(radius1), tonumber(radius2), node
end end
@ -195,14 +195,14 @@ worldedit.register_command("cylinder", {
end, end,
}) })
local check_pyramid = function(param) local check_pyramid = function(param, playername)
local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$") local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, axis, tonumber(height), node return true, axis, tonumber(height), node
end end
@ -255,14 +255,14 @@ worldedit.register_command("spiral", {
category = S("Shapes"), category = S("Shapes"),
privs = {worldedit=true}, privs = {worldedit=true},
require_pos = 1, require_pos = 1,
parse = function(param) parse = function(param, playername)
local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then if found == nil then
return false return false
end end
local node = worldedit.normalize_nodename(nodename) local node, err = worldedit.normalize_nodename(nodename, playername)
if not node then if not node then
return false, S("invalid node name: @1", nodename) return false, err
end end
return true, tonumber(length), tonumber(height), tonumber(space), node return true, tonumber(length), tonumber(height), tonumber(space), node
end, end,