Imre Péntek 2024-03-28 17:22:13 +01:00
parent d4ca3db924
commit 73568e1e66
1 changed files with 14 additions and 7 deletions

View File

@ -2,22 +2,29 @@
-- @module worldedit.code -- @module worldedit.code
--- 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. -- the code will be encapsulated into a function with parameters
-- * name (the name of the player issuing the //lua command)
-- * player (the player object of the above player if applicable)
-- * pos (the position of the aforementioned player (if applicable) rounded to integers
-- @return tuple of success, return of code as string (error message in case of failure)
function worldedit.lua(code, name) function worldedit.lua(code, name)
if string.sub(code,1,1)=="=" then if string.sub(code,1,1)=="=" then
code="return "..string.sub(code,2) code="return "..string.sub(code,2)
end end
local factory, err = loadstring("return function(p) " .. code .. " end") local factory, err = loadstring("return function(name, player, pos) " .. code .. " end")
if not factory then -- Syntax error if not factory then -- Syntax error
return false, err return false, err
end end
local func=factory() local func=factory()
local player=minetest.get_player_by_name(name) local player
local p={name=name, player=player} if name then
if player then player=minetest.get_player_by_name(name)
p["pos"]=vector.round(player:get_pos())
end end
local good, err = pcall(func, p) local pos
if player then
pos=vector.round(player:get_pos())
end
local good, err = pcall(func, name, player, pos)
if good then if good then
err=dump(err) err=dump(err)
end end