diff --git a/worldedit/code.lua b/worldedit/code.lua index eb42e3d..9e3d986 100644 --- a/worldedit/code.lua +++ b/worldedit/code.lua @@ -3,16 +3,22 @@ --- Executes `code` as a Lua chunk in the global namespace. -- @return An error message if the code fails, or nil on success. -function worldedit.lua(code) - local func, err = loadstring(code) - if not func then -- Syntax error - return err +function worldedit.lua(code, name) + local factory, err = loadstring("return function(p) " .. code .. " end") + if not factory then -- Syntax error + return false, err end - local good, err = pcall(func) - if not good then -- Runtime error - return err + local func=factory() + local player=minetest.get_player_by_name(name) + local p={name=name, player=player} + if player then + p["pos"]=vector.round(player:get_pos()) end - return nil + local good, err = pcall(func, p) + if good then + err=dump(err) + end + return good, err end diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 60aa57f..44341ad 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1528,13 +1528,13 @@ worldedit.register_command("lua", { return true, param end, func = function(name, param) - local err = worldedit.lua(param) - if err then - worldedit.player_notify(name, "code error: " .. err) - minetest.log("action", name.." tried to execute "..param) + local good, ret = worldedit.lua(param, name) + if good then + worldedit.player_notify(name, "code successfully executed, returns with " .. ret, false) + minetest.log("action", name .. " executed " .. param) else - worldedit.player_notify(name, "code successfully executed", false) - minetest.log("action", name.." executed "..param) + worldedit.player_notify(name, "code error: " .. dump(ret)) + minetest.log("action", name .. " tried to execute " .. param) end end, })