Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
imre84 committed Mar 28, 2024
1 parent b9232f0 commit 734d1a9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions worldedit/code.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@
-- @module worldedit.code

--- 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)
if string.sub(code,1,1)=="=" then
code="return "..string.sub(code,2)
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
return false, err
end
local func=factory()
local player=minetest.get_player_by_name(name)
local p={name=name, player=player}
local player
if name then
player=minetest.get_player_by_name(name)
end
local pos
if player then
p["pos"]=vector.round(player:get_pos())
pos=vector.round(player:get_pos())
end
local good, err = pcall(func, p)
local good, err = pcall(func, name, player, pos)
if good then
err=dump(err)
end
Expand Down

0 comments on commit 734d1a9

Please sign in to comment.