From 73568e1e66ef99bf3a4f1eb4db5d46311cc0e418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imre=20P=C3=A9ntek?= Date: Thu, 28 Mar 2024 17:22:13 +0100 Subject: [PATCH] as per https://github.com/Uberi/Minetest-WorldEdit/pull/243#discussion_r1539073612 --- worldedit/code.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/worldedit/code.lua b/worldedit/code.lua index 4f52f29..87a94ec 100644 --- a/worldedit/code.lua +++ b/worldedit/code.lua @@ -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} - if player then - p["pos"]=vector.round(player:get_pos()) + local player + if name then + player=minetest.get_player_by_name(name) 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 err=dump(err) end