mirror of
https://github.com/Uberi/Minetest-WorldEdit.git
synced 2024-09-27 15:10:17 +02:00
minor adjustments
This commit is contained in:
parent
8a3f46153c
commit
64195fd8f9
|
@ -4,31 +4,28 @@
|
||||||
--- Executes `code` as a Lua chunk in the global namespace.
|
--- Executes `code` as a Lua chunk in the global namespace.
|
||||||
-- the code will be encapsulated into a function with parameters
|
-- the code will be encapsulated into a function with parameters
|
||||||
-- * name (the name of the player issuing the //lua command)
|
-- * name (the name of the player issuing the //lua command)
|
||||||
-- * player (the player object of the above player if applicable)
|
-- * player (the player object of the player)
|
||||||
-- * pos (the position of the aforementioned player (if applicable) rounded to integers
|
-- * 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
|
-- @return string in case of error, tuple of nil, return of code as string in case of success
|
||||||
function worldedit.lua(code, name)
|
function worldedit.lua(code, name)
|
||||||
if string.sub(code, 1, 1) == "=" then
|
local factory, err = loadstring("return function(name, player, pos)\n" .. code .. "\nend")
|
||||||
code = "return " .. string.sub(code, 2)
|
|
||||||
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 err
|
return err
|
||||||
end
|
end
|
||||||
local func = factory()
|
local func = factory()
|
||||||
local player
|
local player, pos
|
||||||
if name then
|
if name then
|
||||||
player = minetest.get_player_by_name(name)
|
player = minetest.get_player_by_name(name)
|
||||||
end
|
|
||||||
local pos
|
|
||||||
if player then
|
if player then
|
||||||
pos = vector.round(player:get_pos())
|
pos = vector.round(player:get_pos())
|
||||||
end
|
end
|
||||||
local good, err = pcall(func, name, player, pos)
|
|
||||||
if good then
|
|
||||||
return nil, dump(err)
|
|
||||||
end
|
end
|
||||||
return err
|
local good, err = pcall(func, name, player, pos)
|
||||||
|
if not good then -- Runtime error
|
||||||
|
return tostring(err)
|
||||||
|
|
||||||
|
end
|
||||||
|
return nil, dump(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1525,20 +1525,27 @@ worldedit.register_command("lua", {
|
||||||
description = S("Executes <code> as a Lua chunk in the global namespace"),
|
description = S("Executes <code> as a Lua chunk in the global namespace"),
|
||||||
privs = {worldedit=true, server=true},
|
privs = {worldedit=true, server=true},
|
||||||
parse = function(param)
|
parse = function(param)
|
||||||
|
if param == "" then
|
||||||
|
return false
|
||||||
|
end
|
||||||
return true, param
|
return true, param
|
||||||
end,
|
end,
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
local ret = {worldedit.lua(param, name)}
|
-- shorthand like in the Lua interpreter
|
||||||
if type(ret[1]) == "nil" then
|
if param:sub(1, 1) == "=" then
|
||||||
if ret[2] ~= "nil" then
|
param = "return " .. param:sub(2)
|
||||||
worldedit.player_notify(name, "code successfully executed, returns with " .. ret[2], false)
|
|
||||||
else
|
|
||||||
worldedit.player_notify(name, "code successfully executed", false)
|
|
||||||
end
|
end
|
||||||
|
local err, ret = worldedit.lua(param, name)
|
||||||
|
if err == nil then
|
||||||
minetest.log("action", name .. " executed " .. param)
|
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
|
else
|
||||||
worldedit.player_notify(name, "code error: " .. dump(ret[1]))
|
|
||||||
minetest.log("action", name .. " tried to execute " .. param)
|
minetest.log("action", name .. " tried to execute " .. param)
|
||||||
|
worldedit.player_notify(name, "code error: " .. err)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user