Refactor autoCompost function to improve dropper capacity checks and item management
This commit is contained in:
@@ -1832,19 +1832,24 @@ local function autoCompost()
|
|||||||
local dropper = peripheral.wrap(COMPOST_DROPPER)
|
local dropper = peripheral.wrap(COMPOST_DROPPER)
|
||||||
if not dropper then return didWork end
|
if not dropper then return didWork end
|
||||||
|
|
||||||
-- Check how much space the dropper has
|
-- Check how much item capacity the dropper has (slots * 64 - current items)
|
||||||
local dropperContents = dropper.list()
|
local dropperContents = dropper.list()
|
||||||
local dropperUsed = 0
|
local dropperUsedSlots = 0
|
||||||
|
local dropperUsedItems = 0
|
||||||
if dropperContents then
|
if dropperContents then
|
||||||
for _ in pairs(dropperContents) do dropperUsed = dropperUsed + 1 end
|
for _, item in pairs(dropperContents) do
|
||||||
|
dropperUsedSlots = dropperUsedSlots + 1
|
||||||
|
dropperUsedItems = dropperUsedItems + item.count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local dropperSize = dropper.size()
|
local dropperSize = dropper.size()
|
||||||
local dropperFree = dropperSize - dropperUsed
|
local dropperFreeSlots = dropperSize - dropperUsedSlots
|
||||||
|
local dropperFreeItems = (dropperSize * 64) - dropperUsedItems
|
||||||
|
|
||||||
if dropperFree <= 0 then return didWork end
|
if dropperFreeItems <= 0 then return didWork end
|
||||||
|
|
||||||
for _, itemName in ipairs(COMPOSTABLE) do
|
for _, itemName in ipairs(COMPOSTABLE) do
|
||||||
if dropperFree <= 0 then break end
|
if dropperFreeItems <= 0 then break end
|
||||||
if catalogue[itemName] then
|
if catalogue[itemName] then
|
||||||
-- Count total in storage
|
-- Count total in storage
|
||||||
local totalInStorage = 0
|
local totalInStorage = 0
|
||||||
@@ -1854,7 +1859,7 @@ local function autoCompost()
|
|||||||
|
|
||||||
local available = totalInStorage - COMPOST_RESERVE
|
local available = totalInStorage - COMPOST_RESERVE
|
||||||
if available > 0 then
|
if available > 0 then
|
||||||
local toFeed = math.min(available, dropperFree, 64)
|
local toFeed = math.min(available, dropperFreeItems)
|
||||||
local fed = 0
|
local fed = 0
|
||||||
for _, source in ipairs(catalogue[itemName]) do
|
for _, source in ipairs(catalogue[itemName]) do
|
||||||
local chest = peripheral.wrap(source.chest)
|
local chest = peripheral.wrap(source.chest)
|
||||||
@@ -1876,7 +1881,7 @@ local function autoCompost()
|
|||||||
end
|
end
|
||||||
if fed >= toFeed then break end
|
if fed >= toFeed then break end
|
||||||
end
|
end
|
||||||
dropperFree = dropperFree - fed
|
dropperFreeItems = dropperFreeItems - fed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user