Refactor autoCompost function to utilize dropper and hopper for composting, improving item management and efficiency
This commit is contained in:
@@ -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,84 +1804,79 @@ 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
|
for _, chest in ipairs(chests) do
|
||||||
if item.name == "minecraft:bone_meal" then
|
local n = hopper.pushItems(chest, slot)
|
||||||
for _, chest in ipairs(chests) do
|
if n and n > 0 then
|
||||||
local n = composter.pushItems(chest, slot)
|
adjustCache(item.name, chest, n)
|
||||||
if n and n > 0 then
|
print(string.format("[COMPOST] %s x%d -> %s", item.name, n, chest))
|
||||||
adjustCache("minecraft:bone_meal", chest, n)
|
didWork = true
|
||||||
print(string.format("[COMPOST] Bone meal x%d -> %s", n, chest))
|
break
|
||||||
didWork = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
|
||||||
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
|
|
||||||
|
|
||||||
local available = totalInStorage - COMPOST_RESERVE
|
-- Check how much space the dropper has
|
||||||
if available > 0 then
|
local dropperContents = dropper.list()
|
||||||
-- Push 1 item at a time to the composter
|
local dropperUsed = 0
|
||||||
local fed = false
|
if dropperContents then
|
||||||
for _, source in ipairs(catalogue[itemName]) do
|
for _ in pairs(dropperContents) do dropperUsed = dropperUsed + 1 end
|
||||||
local chest = peripheral.wrap(source.chest)
|
end
|
||||||
if chest then
|
local dropperSize = dropper.size()
|
||||||
for slot, slotItem in pairs(chest.list()) do
|
local dropperFree = dropperSize - dropperUsed
|
||||||
if slotItem.name == itemName then
|
|
||||||
local n = chest.pushItems(cname, slot, 1)
|
if dropperFree <= 0 then return didWork end
|
||||||
if n and n > 0 then
|
|
||||||
adjustCache(itemName, source.chest, -n)
|
for _, itemName in ipairs(COMPOSTABLE) do
|
||||||
print(string.format("[COMPOST] Fed %s x%d -> %s",
|
if dropperFree <= 0 then break end
|
||||||
itemName, n, cname))
|
if catalogue[itemName] then
|
||||||
didWork = true
|
-- Count total in storage
|
||||||
fed = true
|
local totalInStorage = 0
|
||||||
break
|
for _, src in ipairs(catalogue[itemName]) do
|
||||||
end
|
totalInStorage = totalInStorage + src.total
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
if fed then break end
|
|
||||||
end
|
end
|
||||||
if fed then break end -- composter now has item, move to next composter
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if fed >= toFeed then break 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))
|
||||||
|
|||||||
Reference in New Issue
Block a user