Refactor autoCompost function to utilize dropper and hopper for composting, improving item management and efficiency

This commit is contained in:
MayaTheShy
2026-03-15 23:28:18 -04:00
parent e1dc56921f
commit 40141c8422

View File

@@ -211,11 +211,9 @@ local FUEL_SET = {}
for _, f in ipairs(FUEL_LIST) do FUEL_SET[f.name] = true end for _, f in ipairs(FUEL_LIST) do FUEL_SET[f.name] = true end
------------------------------------------------- -------------------------------------------------
-- Compostable items (pushed into composters) -- Compostable items (pushed into dropper_10,
-- Each item has a chance to raise the compost level: -- which distributes to composters via hoppers).
-- 1.0 = always raises level, 0.3 = 30% chance etc. -- Bone meal returns through hopper_0.
-- CC:Tweaked composters accept pushItems; the
-- composter itself decides whether the item counts.
------------------------------------------------- -------------------------------------------------
local COMPOSTABLE = { local COMPOSTABLE = {
@@ -333,6 +331,8 @@ for _, name in ipairs(COMPOSTABLE) do COMPOSTABLE_SET[name] = true end
-- Reserve: keep at least this many of each compostable before composting the rest -- Reserve: keep at least this many of each compostable before composting the rest
local COMPOST_RESERVE = 16 local COMPOST_RESERVE = 16
local COMPOST_DROPPER = "minecraft:dropper_10"
local COMPOST_HOPPER = "minecraft:hopper_0"
------------------------------------------------- -------------------------------------------------
-- Low-stock alerts -- Low-stock alerts
@@ -1804,37 +1804,22 @@ end
-- Auto-compost -- Auto-compost
------------------------------------------------- -------------------------------------------------
local function getComposters()
local composters = {}
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "minecraft:composter" then
table.insert(composters, name)
end
end
return composters
end
local function autoCompost() local function autoCompost()
local composters = getComposters()
if #composters == 0 then return end
local chests = getChests()
local catalogue = cache.catalogue local catalogue = cache.catalogue
local chests = getChests()
local didWork = false local didWork = false
for _, cname in ipairs(composters) do -- 1) Pull bone meal from hopper back to chests
local composter = peripheral.wrap(cname) local hopper = peripheral.wrap(COMPOST_HOPPER)
if composter then if hopper then
-- 1) Pull output (bone meal, slot 1 when full) back to chests local contents = hopper.list()
local contents = composter.list()
if contents then if contents then
for slot, item in pairs(contents) do for slot, item in pairs(contents) do
if item.name == "minecraft:bone_meal" then
for _, chest in ipairs(chests) do for _, chest in ipairs(chests) do
local n = composter.pushItems(chest, slot) local n = hopper.pushItems(chest, slot)
if n and n > 0 then if n and n > 0 then
adjustCache("minecraft:bone_meal", chest, n) adjustCache(item.name, chest, n)
print(string.format("[COMPOST] Bone meal x%d -> %s", n, chest)) print(string.format("[COMPOST] %s x%d -> %s", item.name, n, chest))
didWork = true didWork = true
break break
end end
@@ -1843,11 +1828,23 @@ local function autoCompost()
end end
end end
-- 2) Feed compostable items (push 1 at a time — composter has 1 slot) -- 2) Feed compostable items into dropper
contents = composter.list() local dropper = peripheral.wrap(COMPOST_DROPPER)
local hasInput = contents and next(contents) if not dropper then return didWork end
if not hasInput then
-- Check how much space the dropper has
local dropperContents = dropper.list()
local dropperUsed = 0
if dropperContents then
for _ in pairs(dropperContents) do dropperUsed = dropperUsed + 1 end
end
local dropperSize = dropper.size()
local dropperFree = dropperSize - dropperUsed
if dropperFree <= 0 then return didWork end
for _, itemName in ipairs(COMPOSTABLE) do for _, itemName in ipairs(COMPOSTABLE) do
if dropperFree <= 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
@@ -1857,31 +1854,29 @@ local function autoCompost()
local available = totalInStorage - COMPOST_RESERVE local available = totalInStorage - COMPOST_RESERVE
if available > 0 then if available > 0 then
-- Push 1 item at a time to the composter local toFeed = math.min(available, dropperFree, 16)
local fed = false 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)
if chest then if chest then
for slot, slotItem in pairs(chest.list()) do for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then if slotItem.name == itemName then
local n = chest.pushItems(cname, slot, 1) local batch = math.min(slotItem.count, toFeed - fed)
local n = chest.pushItems(COMPOST_DROPPER, slot, batch)
if n and n > 0 then if n and n > 0 then
adjustCache(itemName, source.chest, -n) adjustCache(itemName, source.chest, -n)
print(string.format("[COMPOST] Fed %s x%d -> %s", fed = fed + n
itemName, n, cname))
didWork = true didWork = true
fed = true print(string.format("[COMPOST] Fed %s x%d -> dropper",
break itemName, n))
if fed >= toFeed then break end
end end
end end
end end
end end
if fed then break end if fed >= toFeed then break end
end
if fed then break end -- composter now has item, move to next composter
end
end
end end
dropperFree = dropperFree - fed
end end
end end
end end
@@ -2180,12 +2175,16 @@ local function main()
end end
print(string.format("[INIT] %d/%d recipes enabled", enabledCount, totalRecipeCount)) print(string.format("[INIT] %d/%d recipes enabled", enabledCount, totalRecipeCount))
-- Detect composters -- Detect compost peripherals
local composters = getComposters() if peripheral.wrap(COMPOST_DROPPER) then
if #composters > 0 then print("[OK] Compost dropper: " .. COMPOST_DROPPER)
print(string.format("[OK] Composters: %d", #composters))
else else
print("[INFO] No composters on network") print("[WARN] Compost dropper not found: " .. COMPOST_DROPPER)
end
if peripheral.wrap(COMPOST_HOPPER) then
print("[OK] Compost hopper: " .. COMPOST_HOPPER)
else
print("[WARN] Compost hopper not found: " .. COMPOST_HOPPER)
end end
print(string.format("[INIT] Tracking %d low-stock alerts", #LOW_STOCK_ALERTS)) print(string.format("[INIT] Tracking %d low-stock alerts", #LOW_STOCK_ALERTS))