Merge pull request 'milan2018' (#12) from milan2018 into master

This commit is contained in:
Milan Meduna 2020-05-20 21:32:53 +02:00
commit 22025c2a22
2 changed files with 60 additions and 53 deletions

View File

@ -141,7 +141,7 @@ local function commandblock_action_on(pos, node)
end
local commands = resolve_commands(meta:get_string("commands"), pos)
for _, command in pairs(commands:split("\n")) do
for _, command in pairs(commands:split("\n/")) do
local pos = command:find(" ")
local cmd, param = command, ""
if pos then

View File

@ -1,52 +1,59 @@
--- Lua code execution functions.
-- @module worldedit.code
--- Executes `code` as a Lua chunk in the global namespace.
-- @return An error message if the code fails, or nil on success.
function worldedit.lua(code)
local func, err = loadstring(code)
if not func then -- Syntax error
return err
end
local good, err = pcall(func)
if not good then -- Runtime error
return err
end
return nil
end
--- Executes `code` as a Lua chunk in the global namespace with the variable
-- pos available, for each node in a region defined by positions `pos1` and
-- `pos2`.
-- @return An error message if the code fails, or nil on success.
function worldedit.luatransform(pos1, pos2, code)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local factory, err = loadstring("return function(pos) " .. code .. " end")
if not factory then -- Syntax error
return err
end
local func = factory()
worldedit.keep_loaded(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local good, err = pcall(func, pos)
if not good then -- Runtime error
return err
end
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
return nil
end
--- Lua code execution functions.
-- @module worldedit.code
--- Executes `code` as a Lua chunk in the global namespace.
-- @return An error message if the code fails, or nil on success.
function worldedit.lua(code)
local func, err = loadstring(code)
if not func then -- Syntax error
return err
end
local good, err = pcall(func)
if not good then -- Runtime error
return err
end
return nil
end
--- Executes `code` as a Lua chunk in the global namespace with the variable
-- pos available, for each node in a region defined by positions `pos1` and
-- `pos2`.
-- @return An error message if the code fails, or nil on success.
function worldedit.luatransform(pos1, pos2, code)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local factory, err = loadstring("return function(pos) " .. code .. " end")
if not factory then -- Syntax error
return err
end
local func = factory()
worldedit.keep_loaded(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local good, err = pcall(func, pos)
if not good then -- Runtime error
return err
end
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
return nil
end
local input = io.open(minetest.get_worldpath().."/init.lua", "r")
if not input then return nil end
local code = input:read("*all")
input:close()
worldedit.lua(code)