1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-11-16 23:50:18 +01:00

minor adjustments

This commit is contained in:
sfan5 2024-04-20 19:52:40 +02:00
parent 8a3f46153c
commit 64195fd8f9
2 changed files with 28 additions and 24 deletions

View File

@ -4,31 +4,28 @@
--- Executes `code` as a Lua chunk in the global namespace.
-- 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
-- * player (the player object of the player)
-- * pos (the position of the player rounded to integers)
-- @return string in case of error, tuple of nil, return of code as string in case of success
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(name, player, pos) " .. code .. " end")
if not factory then -- Syntax error
local factory, err = loadstring("return function(name, player, pos)\n" .. code .. "\nend")
if not factory then -- Syntax error
return err
end
local func=factory()
local player
local func = factory()
local player, pos
if name then
player = minetest.get_player_by_name(name)
end
local pos
if player then
pos = vector.round(player:get_pos())
if player then
pos = vector.round(player:get_pos())
end
end
local good, err = pcall(func, name, player, pos)
if good then
return nil, dump(err)
if not good then -- Runtime error
return tostring(err)
end
return err
return nil, dump(err)
end

View File

@ -1525,20 +1525,27 @@ worldedit.register_command("lua", {
description = S("Executes <code> as a Lua chunk in the global namespace"),
privs = {worldedit=true, server=true},
parse = function(param)
if param == "" then
return false
end
return true, param
end,
func = function(name, param)
local ret = {worldedit.lua(param, name)}
if type(ret[1]) == "nil" then
if ret[2] ~= "nil" then
worldedit.player_notify(name, "code successfully executed, returns with " .. ret[2], false)
else
worldedit.player_notify(name, "code successfully executed", false)
end
-- shorthand like in the Lua interpreter
if param:sub(1, 1) == "=" then
param = "return " .. param:sub(2)
end
local err, ret = worldedit.lua(param, name)
if err == nil then
minetest.log("action", name .. " executed " .. param)
if ret ~= "nil" then
worldedit.player_notify(name, "code successfully executed, returned " .. ret)
else
worldedit.player_notify(name, "code successfully executed")
end
else
worldedit.player_notify(name, "code error: " .. dump(ret[1]))
minetest.log("action", name .. " tried to execute " .. param)
worldedit.player_notify(name, "code error: " .. err)
end
end,
})