Skip to content

Commit affe867

Browse files
imre84MoNTE48
authored andcommitted
Make //lua work with expressions as well (#243)
1 parent 54740da commit affe867

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

.luacheckrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
read_globals = {"vector", "VoxelArea", "ItemStack",
22
"table",
3-
"unified_inventory", "sfinv", "smart_inventory", "inventory_plus"
3+
"unified_inventory", "sfinv", "smart_inventory", "inventory_plus",
4+
"dump"
45
}
56
globals = {"minetest", "worldedit"}
67
-- Ignore these errors until someone decides to fix them

WorldEdit API.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,19 @@ Code
227227
----
228228
Contained in code.lua, this module allows arbitrary Lua code to be used with WorldEdit.
229229

230-
### error = worldedit.lua(code)
230+
### error = worldedit.lua(code, name)
231231

232-
Executes `code` as a Lua chunk in the global namespace.
232+
the given code gets encapsulated into a function with parameters `name`, `player`, `pos`
233+
where
234+
* `name` is a playername or `nil`
235+
* `player` is the player object of the above player if applicable, otherwise `nil`
236+
* `pos` is the position of the aforementioned player (if applicable, otherwise `nil`) rounded to integers
233237

234-
Returns an error if the code fails or nil otherwise.
238+
the resulting function is then executed as a Lua chunk in the global namespace.
239+
240+
The return is
241+
* a string in case of an error
242+
* a tuple of `nil` and return of the function converted to a string in case of success
235243

236244
### error = worldedit.luatransform(pos1, pos2, code)
237245

worldedit/code.lua

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
-- @module worldedit.code
33

44
--- Executes `code` as a Lua chunk in the global namespace.
5-
-- @return An error message if the code fails, or nil on success.
6-
function worldedit.lua(code)
7-
local func, err = loadstring(code)
8-
if not func then -- Syntax error
5+
-- the code will be encapsulated into a function with parameters
6+
-- * name (the name of the player issuing the //lua command)
7+
-- * player (the player object of the player)
8+
-- * pos (the position of the player rounded to integers)
9+
-- @return string in case of error, tuple of nil, return of code as string in case of success
10+
function worldedit.lua(code, name)
11+
local factory, err = loadstring("return function(name, player, pos)\n" .. code .. "\nend")
12+
if not factory then -- Syntax error
913
return err
1014
end
11-
local good, err = pcall(func)
12-
if not good then -- Runtime error
13-
return err
15+
local func = factory()
16+
local player, pos
17+
if name then
18+
player = minetest.get_player_by_name(name)
19+
if player then
20+
pos = vector.round(player:get_pos())
21+
end
1422
end
15-
return nil
23+
local good, err = pcall(func, name, player, pos)
24+
if not good then -- Runtime error
25+
return tostring(err)
26+
end
27+
return nil, dump(err)
1628
end
1729

1830

0 commit comments

Comments
 (0)