Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Desc: A Composter program which automatically searches a storage drawer
- ---network and moves compostable items to a destination hopper for composting.
- ---extracts bonemeal output back into drawers
- ---does not run if bonemeal count > fir_sapling (limiter helps balance inventory)
- ---Item maps can be changed to fulfill different functions (composting, washing, cooking)
- ---review IMPORTANT comments, these are conficurables that will likely change based on your setup
- ---
- --By: hornedcommando
- -- Define peripherals
- local compostHopper = peripheral.wrap("minecraft:hopper_4") -- IMPORTANT: Change this to the hopper on top of the composter
- local outputHopper = peripheral.wrap("minecraft:hopper_5") -- IMPORTANT: Change this to the hopper where bonemeal is output
- -- List of specific items to find
- local specificItems = {
- ["minecraft:wheat_seeds"] = true,
- ["minecraft:beetroot_seeds"] = true,
- ["farmersdelight:cabbage_seeds"] = true,
- ["supplementaries:flax_seed"] = true,
- ["minecraft:tomato_seeds"] = true,
- ["biomesoplenty:fir_sapling"] = true
- }
- -- Function to dynamically find all connected storage drawers
- local function getConnectedDrawers()
- local drawers = {}
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
- local drawer = peripheral.wrap(name)
- table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
- end
- end
- return drawers
- end
- -- Function to count specific items in the drawers
- local function countItems(drawers, itemName)
- local count = 0
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- for _, item in pairs(drawerContents) do
- if item.name == itemName then
- count = count + item.count
- end
- end
- end
- return count
- end
- -- Function to push items from drawers to the composter barrel
- local function pushItemsFromDrawers(drawers)
- local boneMealCount = countItems(drawers, "minecraft:bone_meal") --IMPORTANT: Change this to adjust the limiter for different farms
- local firSaplingCount = countItems(drawers, "biomesoplenty:fir_sapling") --IMPORTANT: Change this to the ingredient you get the most of for the specific farm
- if boneMealCount > firSaplingCount then
- print("More bone meal than fir saplings. Skipping composting for 10 seconds.")
- return success -- Skip composting
- end
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- for slot, item in pairs(drawerContents) do
- if specificItems[item.name] then
- local success = drawerInfo.drawer.pushItems(peripheral.getName(compostHopper), slot, item.count)
- if success == 0 then
- print("Failed to push item: " .. item.name)
- return false -- Failed push, continue checking other drawers
- else
- print("Pushed " .. item.count .. " of " .. item.name .. " to composter barrel.")
- end
- end
- end
- end
- return true -- Successfully processed all items in the drawers
- end
- -- Function to find a drawer for a specific item or an empty drawer
- local function findDrawerForItem(drawers, itemName)
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- if drawerContents[2] and drawerContents[2].name == itemName then
- return drawerInfo -- Return the drawerInfo which includes the name
- end
- end
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- if not drawerContents[2] then
- return drawerInfo -- Return the first empty drawer
- end
- end
- return nil -- Return nil if no drawer currently contains the item and no empty drawer is found
- end
- -- Function to move bonemeal from the output hopper to the appropriate drawer
- local function moveBoneMealToDrawer(drawers)
- local outputItems = outputHopper.list()
- for slot, item in pairs(outputItems) do
- if item.name == "minecraft:bone_meal" then
- local targetDrawerInfo = findDrawerForItem(drawers, item.name)
- if targetDrawerInfo then
- local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
- print("Attempting to push bonemeal to drawer: " .. drawerName)
- local success = outputHopper.pushItems(drawerName, slot, item.count, 2)
- if success == 0 then
- print("Cannot push bonemeal to drawer: " .. drawerName .. "! Check storage capacity.")
- return false -- Failed push, continue checking other items
- else
- print("Moved " .. item.count .. " of bonemeal to drawer: " .. drawerName)
- end
- else
- print("No drawer available for bonemeal! Add or upgrade a drawer.")
- return false -- No drawer available for this item
- end
- end
- end
- return true -- Successfully processed all bonemeal items
- end
- -- Main loop function
- local function main()
- while true do
- local drawers = getConnectedDrawers()
- local success = pushItemsFromDrawers(drawers)
- os.sleep(30) -- Wait 10 seconds to allow composting to finish
- moveBoneMealToDrawer(drawers)
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement