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
-------------------------------------------------
-- Compostable items (pushed into composters)
-- Each item has a chance to raise the compost level:
-- 1.0 = always raises level, 0.3 = 30% chance etc.
-- CC:Tweaked composters accept pushItems; the
-- composter itself decides whether the item counts.
-- Compostable items (pushed into dropper_10,
-- which distributes to composters via hoppers).
-- Bone meal returns through hopper_0.
-------------------------------------------------
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
local COMPOST_RESERVE = 16
local COMPOST_DROPPER = "minecraft:dropper_10"
local COMPOST_HOPPER = "minecraft:hopper_0"
-------------------------------------------------
-- Low-stock alerts
@@ -1804,84 +1804,79 @@ end
-- 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 composters = getComposters()
if #composters == 0 then return end
local chests = getChests()
local catalogue = cache.catalogue
local chests = getChests()
local didWork = false
for _, cname in ipairs(composters) do
local composter = peripheral.wrap(cname)
if composter then
-- 1) Pull output (bone meal, slot 1 when full) back to chests
local contents = composter.list()
if contents then
for slot, item in pairs(contents) do
if item.name == "minecraft:bone_meal" then
for _, chest in ipairs(chests) do
local n = composter.pushItems(chest, slot)
if n and n > 0 then
adjustCache("minecraft:bone_meal", chest, n)
print(string.format("[COMPOST] Bone meal x%d -> %s", n, chest))
didWork = true
break
end
end
-- 1) Pull bone meal from hopper back to chests
local hopper = peripheral.wrap(COMPOST_HOPPER)
if hopper then
local contents = hopper.list()
if contents then
for slot, item in pairs(contents) do
for _, chest in ipairs(chests) do
local n = hopper.pushItems(chest, slot)
if n and n > 0 then
adjustCache(item.name, chest, n)
print(string.format("[COMPOST] %s x%d -> %s", item.name, n, chest))
didWork = true
break
end
end
end
end
end
-- 2) Feed compostable items (push 1 at a time — composter has 1 slot)
contents = composter.list()
local hasInput = contents and next(contents)
if not hasInput then
for _, itemName in ipairs(COMPOSTABLE) do
if catalogue[itemName] then
-- Count total in storage
local totalInStorage = 0
for _, src in ipairs(catalogue[itemName]) do
totalInStorage = totalInStorage + src.total
end
-- 2) Feed compostable items into dropper
local dropper = peripheral.wrap(COMPOST_DROPPER)
if not dropper then return didWork end
local available = totalInStorage - COMPOST_RESERVE
if available > 0 then
-- Push 1 item at a time to the composter
local fed = false
for _, source in ipairs(catalogue[itemName]) do
local chest = peripheral.wrap(source.chest)
if chest then
for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then
local n = chest.pushItems(cname, slot, 1)
if n and n > 0 then
adjustCache(itemName, source.chest, -n)
print(string.format("[COMPOST] Fed %s x%d -> %s",
itemName, n, cname))
didWork = true
fed = true
break
end
end
end
-- 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
if dropperFree <= 0 then break end
if catalogue[itemName] then
-- Count total in storage
local totalInStorage = 0
for _, src in ipairs(catalogue[itemName]) do
totalInStorage = totalInStorage + src.total
end
local available = totalInStorage - COMPOST_RESERVE
if available > 0 then
local toFeed = math.min(available, dropperFree, 16)
local fed = 0
for _, source in ipairs(catalogue[itemName]) do
local chest = peripheral.wrap(source.chest)
if chest then
for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then
local batch = math.min(slotItem.count, toFeed - fed)
local n = chest.pushItems(COMPOST_DROPPER, slot, batch)
if n and n > 0 then
adjustCache(itemName, source.chest, -n)
fed = fed + n
didWork = true
print(string.format("[COMPOST] Fed %s x%d -> dropper",
itemName, n))
if fed >= toFeed then break end
end
if fed then break end
end
if fed then break end -- composter now has item, move to next composter
end
end
if fed >= toFeed then break end
end
dropperFree = dropperFree - fed
end
end
end
@@ -2180,12 +2175,16 @@ local function main()
end
print(string.format("[INIT] %d/%d recipes enabled", enabledCount, totalRecipeCount))
-- Detect composters
local composters = getComposters()
if #composters > 0 then
print(string.format("[OK] Composters: %d", #composters))
-- Detect compost peripherals
if peripheral.wrap(COMPOST_DROPPER) then
print("[OK] Compost dropper: " .. COMPOST_DROPPER)
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
print(string.format("[INIT] Tracking %d low-stock alerts", #LOW_STOCK_ALERTS))