1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-24 01:30:40 +01:00

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.
-- @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

View File

@ -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,
})