minor adjustments

This commit is contained in:
sfan5 2024-04-20 19:52:40 +02:00
parent 8a3f46153c
commit 64195fd8f9
2 changed files with 28 additions and 24 deletions

View File

@ -4,31 +4,28 @@
--- Executes `code` as a Lua chunk in the global namespace. --- Executes `code` as a Lua chunk in the global namespace.
-- the code will be encapsulated into a function with parameters -- the code will be encapsulated into a function with parameters
-- * name (the name of the player issuing the //lua command) -- * name (the name of the player issuing the //lua command)
-- * player (the player object of the above player if applicable) -- * player (the player object of the player)
-- * pos (the position of the aforementioned player (if applicable) rounded to integers -- * pos (the position of the player rounded to integers)
-- @return string in case of error, tuple of nil, return of code as string in case of success -- @return string in case of error, tuple of nil, return of code as string in case of success
function worldedit.lua(code, name) function worldedit.lua(code, name)
if string.sub(code, 1, 1) == "=" then local factory, err = loadstring("return function(name, player, pos)\n" .. code .. "\nend")
code = "return " .. string.sub(code, 2) if not factory then -- Syntax error
end
local factory, err = loadstring("return function(name, player, pos) " .. code .. " end")
if not factory then -- Syntax error
return err return err
end end
local func=factory() local func = factory()
local player local player, pos
if name then if name then
player = minetest.get_player_by_name(name) player = minetest.get_player_by_name(name)
end if player then
local pos pos = vector.round(player:get_pos())
if player then end
pos = vector.round(player:get_pos())
end end
local good, err = pcall(func, name, player, pos) local good, err = pcall(func, name, player, pos)
if good then if not good then -- Runtime error
return nil, dump(err) return tostring(err)
end end
return err return nil, dump(err)
end end

View File

@ -1525,20 +1525,27 @@ worldedit.register_command("lua", {
description = S("Executes <code> as a Lua chunk in the global namespace"), description = S("Executes <code> as a Lua chunk in the global namespace"),
privs = {worldedit=true, server=true}, privs = {worldedit=true, server=true},
parse = function(param) parse = function(param)
if param == "" then
return false
end
return true, param return true, param
end, end,
func = function(name, param) func = function(name, param)
local ret = {worldedit.lua(param, name)} -- shorthand like in the Lua interpreter
if type(ret[1]) == "nil" then if param:sub(1, 1) == "=" then
if ret[2] ~= "nil" then param = "return " .. param:sub(2)
worldedit.player_notify(name, "code successfully executed, returns with " .. ret[2], false) end
else local err, ret = worldedit.lua(param, name)
worldedit.player_notify(name, "code successfully executed", false) if err == nil then
end
minetest.log("action", name .. " executed " .. param) minetest.log("action", name .. " executed " .. param)
if ret ~= "nil" then
worldedit.player_notify(name, "code successfully executed, returned " .. ret)
else
worldedit.player_notify(name, "code successfully executed")
end
else else
worldedit.player_notify(name, "code error: " .. dump(ret[1]))
minetest.log("action", name .. " tried to execute " .. param) minetest.log("action", name .. " tried to execute " .. param)
worldedit.player_notify(name, "code error: " .. err)
end end
end, end,
}) })