mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-13 16:56:49 +00:00
037cbabb32
Unfortunately we can't apply the config changes due to backwards compatibility. This'll be something we may need to PR into Forge. CraftTweaker support still needs to be added.
67 lines
1.9 KiB
Lua
67 lines
1.9 KiB
Lua
|
|
local function create( ... )
|
|
local tFns = table.pack(...)
|
|
local tCos = {}
|
|
for i = 1, tFns.n, 1 do
|
|
local fn = tFns[i]
|
|
if type( fn ) ~= "function" then
|
|
error( "bad argument #" .. i .. " (expected function, got " .. type( fn ) .. ")", 3 )
|
|
end
|
|
|
|
tCos[i] = coroutine.create(fn)
|
|
end
|
|
|
|
return tCos
|
|
end
|
|
|
|
local function runUntilLimit( _routines, _limit )
|
|
local count = #_routines
|
|
local living = count
|
|
|
|
local tFilters = {}
|
|
local eventData = { n = 0 }
|
|
while true do
|
|
for n = 1, count do
|
|
local r = _routines[n]
|
|
if r then
|
|
if tFilters[r] == nil or tFilters[r] == eventData[1] or eventData[1] == "terminate" then
|
|
local ok, param = coroutine.resume( r, table.unpack( eventData, 1, eventData.n ) )
|
|
if not ok then
|
|
error( param, 0 )
|
|
else
|
|
tFilters[r] = param
|
|
end
|
|
if coroutine.status( r ) == "dead" then
|
|
_routines[n] = nil
|
|
living = living - 1
|
|
if living <= _limit then
|
|
return n
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for n = 1, count do
|
|
local r = _routines[n]
|
|
if r and coroutine.status( r ) == "dead" then
|
|
_routines[n] = nil
|
|
living = living - 1
|
|
if living <= _limit then
|
|
return n
|
|
end
|
|
end
|
|
end
|
|
eventData = table.pack( os.pullEventRaw() )
|
|
end
|
|
end
|
|
|
|
function waitForAny( ... )
|
|
local routines = create( ... )
|
|
return runUntilLimit( routines, #routines - 1 )
|
|
end
|
|
|
|
function waitForAll( ... )
|
|
local routines = create( ... )
|
|
runUntilLimit( routines, 0 )
|
|
end
|