mirror of
https://github.com/Uberi/Minetest-WorldEdit.git
synced 2024-12-04 13:23:43 +01:00
Make //lua work with expressions as well (#243)
This commit is contained in:
parent
41d53180b1
commit
883caff58d
@ -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
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
@ -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()
|
||||||
if not good then -- Runtime error
|
local player, pos
|
||||||
return err
|
if name then
|
||||||
|
player = minetest.get_player_by_name(name)
|
||||||
|
if player then
|
||||||
|
pos = vector.round(player:get_pos())
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return nil
|
local good, err = pcall(func, name, player, pos)
|
||||||
|
if not good then -- Runtime error
|
||||||
|
return tostring(err)
|
||||||
|
end
|
||||||
|
return nil, dump(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
worldedit.player_notify(name, "code successfully executed")
|
||||||
|
end
|
||||||
else
|
else
|
||||||
worldedit.player_notify(name, "code successfully executed", false)
|
minetest.log("action", name .. " tried to execute " .. param)
|
||||||
minetest.log("action", name.." executed "..param)
|
worldedit.player_notify(name, "code error: " .. err)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user