Files
opus-apps/milo/plugins/limitTask.lua
kepler155c@gmail.com b03b0d366b app tweaks
2019-04-25 14:43:55 -04:00

37 lines
812 B
Lua

local itemDB = require('core.itemDB')
local Milo = require('milo')
local LimitTask = {
name = 'limiter',
priority = 50,
}
function LimitTask:cycle(context)
local trashcan = context.storage:filterActive('trashcan')()
if trashcan then
for key,res in pairs(context.resources) do
if res.limit then
local items, count = Milo:getMatches(itemDB:splitKey(key), res)
if count > res.limit then
local total = count - res.limit
for _, item in pairs(items) do
local amount = context.storage:export(
trashcan,
nil,
math.min(total, item.count),
item)
total = total - amount
-- amount == 0 means that trashcan is full
if amount <= 0 or total <= 0 then
break
end
end
end
end
end
end
end
Milo:registerTask(LimitTask)