make //lua work with expressions as well

This commit is contained in:
Imre Péntek 2024-03-25 15:44:34 +01:00
parent f75700ed76
commit 145a9ba0aa
2 changed files with 20 additions and 14 deletions

View File

@ -3,16 +3,22 @@
--- Executes `code` as a Lua chunk in the global namespace. --- Executes `code` as a Lua chunk in the global namespace.
-- @return An error message if the code fails, or nil on success. -- @return An error message if the code fails, or nil on success.
function worldedit.lua(code) function worldedit.lua(code, name)
local func, err = loadstring(code) local factory, err = loadstring("return function(p) " .. code .. " end")
if not func then -- Syntax error if not factory then -- Syntax error
return err return false, err
end end
local good, err = pcall(func) local func=factory()
if not good then -- Runtime error local player=minetest.get_player_by_name(name)
return err local p={name=name, player=player}
if player then
p["pos"]=vector.round(player:get_pos())
end end
return nil local good, err = pcall(func, p)
if good then
err=dump(err)
end
return good, err
end end

View File

@ -1528,13 +1528,13 @@ worldedit.register_command("lua", {
return true, param return true, param
end, end,
func = function(name, param) func = function(name, param)
local err = worldedit.lua(param) local good, ret = worldedit.lua(param, name)
if err then if good then
worldedit.player_notify(name, "code error: " .. err) worldedit.player_notify(name, "code successfully executed, returns with " .. ret, false)
minetest.log("action", name.." tried to execute "..param) minetest.log("action", name .. " executed " .. param)
else else
worldedit.player_notify(name, "code successfully executed", false) worldedit.player_notify(name, "code error: " .. dump(ret))
minetest.log("action", name.." executed "..param) minetest.log("action", name .. " tried to execute " .. param)
end end
end, end,
}) })