Make //lua work with expressions as well (#243)

This commit is contained in:
imre84 2024-04-20 20:00:41 +02:00 committed by GitHub
parent 41d53180b1
commit 883caff58d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 18 deletions

View File

@ -1,6 +1,7 @@
read_globals = {"minetest", "vector", "VoxelArea", "ItemStack", read_globals = {"minetest", "vector", "VoxelArea", "ItemStack",
"table", "table",
"unified_inventory", "sfinv", "smart_inventory", "inventory_plus" "unified_inventory", "sfinv", "smart_inventory", "inventory_plus",
"dump"
} }
globals = {"worldedit"} globals = {"worldedit"}
-- Ignore these errors until someone decides to fix them -- Ignore these errors until someone decides to fix them

View File

@ -227,11 +227,19 @@ Code
---- ----
Contained in code.lua, this module allows arbitrary Lua code to be used with WorldEdit. Contained in code.lua, this module allows arbitrary Lua code to be used with WorldEdit.
### error = worldedit.lua(code) ### error = worldedit.lua(code, name)
Executes `code` as a Lua chunk in the global namespace. the given code gets encapsulated into a function with parameters `name`, `player`, `pos`
where
* `name` is a playername or `nil`
* `player` is the player object of the above player if applicable, otherwise `nil`
* `pos` is the position of the aforementioned player (if applicable, otherwise `nil`) rounded to integers
Returns an error if the code fails or nil otherwise. the resulting function is then executed as a Lua chunk in the global namespace.
The return is
* a string in case of an error
* a tuple of `nil` and return of the function converted to a string in case of success
### error = worldedit.luatransform(pos1, pos2, code) ### error = worldedit.luatransform(pos1, pos2, code)

View File

@ -2,17 +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
function worldedit.lua(code) -- * name (the name of the player issuing the //lua command)
local func, err = loadstring(code) -- * player (the player object of the player)
if not func then -- Syntax error -- * 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)
local factory, err = loadstring("return function(name, player, pos)\n" .. code .. "\nend")
if not factory then -- Syntax error
return err return err
end end
local good, err = pcall(func) local func = factory()
local player, pos
if name then
player = minetest.get_player_by_name(name)
if player then
pos = vector.round(player:get_pos())
end
end
local good, err = pcall(func, name, player, pos)
if not good then -- Runtime error if not good then -- Runtime error
return err return tostring(err)
end end
return nil return nil, dump(err)
end end

View File

@ -1555,16 +1555,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 err = worldedit.lua(param) -- shorthand like in the Lua interpreter
if err then if param:sub(1, 1) == "=" then
worldedit.player_notify(name, "code error: " .. err) param = "return " .. param:sub(2)
minetest.log("action", name.." tried to execute "..param) 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 else
worldedit.player_notify(name, "code successfully executed", false) worldedit.player_notify(name, "code successfully executed")
minetest.log("action", name.." executed "..param) end
else
minetest.log("action", name .. " tried to execute " .. param)
worldedit.player_notify(name, "code error: " .. err)
end end
end, end,
}) })