Spoiler
--find block position
local _POSITION_ = { commands.getBlockPosition() }
local tArgs = { ... }
local platform = {
vector.new( 0, 0, 0 ),
vector.new( 1, 0, 0 ),
vector.new( 0, 0, 1 ),
vector.new( 1, 0, 1 ),
vector.new( -1, 0, 0 ),
vector.new( 0, 0, -1 ),
vector.new( -1, 0, -1 ),
vector.new( 1, 0, -1 ),
vector.new( -1, 0, 1 ),
vector.new( 2, 0, 0 ),
vector.new( 0, 0, 2 ),
vector.new( -2, 0, 0 ),
vector.new( 0, 0, -2 ),
}
local function getPlayerPosition( p )
local success, t = commands.exec( "/tp " .. p .. " ~ ~ ~" )
if not success then
return false
end
local x, y, z = t[1]:match( "to (%-?%d+%.?%d*),(%-?%d+%.?%d*),(%-?%d+%.?%d*)" )
return math.floor( x ), math.floor( y ), math.floor( z )
end
local last = {}
local id = os.startTimer( 0.7 )
while true do
local x, y, z = getPlayerPosition( "KingofGamesYami" )
local pos = vector.new( x, y - 1, z )
local current = {}
local toExecute = {}
local time = os.clock()
for i, v in ipairs( platform ) do
local b = pos + v
local bstr = b:tostring()
local name = commands.getBlockInfo( b.x, b.y, b.z ).name
if name == "minecraft:air" then
current[ bstr ] = true
toExecute[ #toExecute + 1 ] = "setblock " .. b.x .. " " .. b.y .. " " .. b.z .. " minecraft:glass"
elseif last[ bstr ] then
current[ bstr ] = true
end
end
for k, v in pairs( last ) do
local b = vector.new( loadstring( "return " .. k )() )
if not current[ k ] then
toExecute[ #toExecute + 1 ] = "setblock " .. b.x .. " " .. b.y .. " " .. b.z .. " minecraft:air"
end
end
for i, v in ipairs( toExecute ) do
commands.execAsync( v )
end
last = current
print( os.clock() - time )
while true do
local event, tid = os.pullEvent( "timer" )
if id == tid then
break
end
end
id = os.startTimer( 0.7 )
end
This program is intended to generate a glass platform underneath the player, automatically adjusting it as needed. The problem is, the main body of the code takes 0.65 seconds to execute - far too long to be of use for a running player.
I'm wondering what I could possibly do to reduce this chunk of time, if anything.