Advertisement
hornedcommando

Minecraft ComputerCraft Modem Composter Above and Beyond

Nov 8th, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.59 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A Composter program which automatically searches a storage drawer
  3. ---network and moves compostable items to a destination hopper for composting.
  4. ---extracts bonemeal output back into drawers
  5. ---does not run if bonemeal count > fir_sapling (limiter helps balance inventory)
  6. ---Item maps can be changed to fulfill different functions (composting, washing, cooking)
  7. ---review IMPORTANT comments, these are conficurables that will likely change based on your setup
  8. ---
  9.  
  10. --By: hornedcommando
  11.  
  12. -- Define peripherals
  13. local compostHopper = peripheral.wrap("minecraft:hopper_4") -- IMPORTANT: Change this to the hopper on top of the composter
  14. local outputHopper = peripheral.wrap("minecraft:hopper_5") -- IMPORTANT: Change this to the hopper where bonemeal is output
  15.  
  16. -- List of specific items to find
  17. local specificItems = {
  18.     ["minecraft:wheat_seeds"] = true,
  19.     ["minecraft:beetroot_seeds"] = true,
  20.     ["farmersdelight:cabbage_seeds"] = true,
  21.     ["supplementaries:flax_seed"] = true,
  22.     ["minecraft:tomato_seeds"] = true,
  23.     ["biomesoplenty:fir_sapling"] = true
  24. }
  25.  
  26. -- Function to dynamically find all connected storage drawers
  27. local function getConnectedDrawers()
  28.     local drawers = {}
  29.     for _, name in ipairs(peripheral.getNames()) do
  30.         if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
  31.             local drawer = peripheral.wrap(name)
  32.             table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
  33.         end
  34.     end
  35.     return drawers
  36. end
  37.  
  38. -- Function to count specific items in the drawers
  39. local function countItems(drawers, itemName)
  40.     local count = 0
  41.     for _, drawerInfo in ipairs(drawers) do
  42.         local drawerContents = drawerInfo.drawer.list()
  43.         for _, item in pairs(drawerContents) do
  44.             if item.name == itemName then
  45.                 count = count + item.count
  46.             end
  47.         end
  48.     end
  49.     return count
  50. end
  51.  
  52. -- Function to push items from drawers to the composter barrel
  53. local function pushItemsFromDrawers(drawers)
  54.     local boneMealCount = countItems(drawers, "minecraft:bone_meal") --IMPORTANT: Change this to adjust the limiter for different farms
  55.     local firSaplingCount = countItems(drawers, "biomesoplenty:fir_sapling") --IMPORTANT: Change this to the ingredient you get the most of for the specific farm
  56.  
  57.     if boneMealCount > firSaplingCount then
  58.         print("More bone meal than fir saplings. Skipping composting for 10 seconds.")
  59.         return success -- Skip composting
  60.     end
  61.  
  62.     for _, drawerInfo in ipairs(drawers) do
  63.         local drawerContents = drawerInfo.drawer.list()
  64.         for slot, item in pairs(drawerContents) do
  65.             if specificItems[item.name] then
  66.                 local success = drawerInfo.drawer.pushItems(peripheral.getName(compostHopper), slot, item.count)
  67.                 if success == 0 then
  68.                     print("Failed to push item: " .. item.name)
  69.                     return false -- Failed push, continue checking other drawers
  70.                 else
  71.                     print("Pushed " .. item.count .. " of " .. item.name .. " to composter barrel.")
  72.                 end
  73.             end
  74.         end
  75.     end
  76.     return true -- Successfully processed all items in the drawers
  77. end
  78.  
  79. -- Function to find a drawer for a specific item or an empty drawer
  80. local function findDrawerForItem(drawers, itemName)
  81.     for _, drawerInfo in ipairs(drawers) do
  82.         local drawerContents = drawerInfo.drawer.list()
  83.         if drawerContents[2] and drawerContents[2].name == itemName then
  84.             return drawerInfo -- Return the drawerInfo which includes the name
  85.         end
  86.     end
  87.     for _, drawerInfo in ipairs(drawers) do
  88.         local drawerContents = drawerInfo.drawer.list()
  89.         if not drawerContents[2] then
  90.             return drawerInfo -- Return the first empty drawer
  91.         end
  92.     end
  93.     return nil -- Return nil if no drawer currently contains the item and no empty drawer is found
  94. end
  95.  
  96. -- Function to move bonemeal from the output hopper to the appropriate drawer
  97. local function moveBoneMealToDrawer(drawers)
  98.     local outputItems = outputHopper.list()
  99.     for slot, item in pairs(outputItems) do
  100.         if item.name == "minecraft:bone_meal" then
  101.             local targetDrawerInfo = findDrawerForItem(drawers, item.name)
  102.             if targetDrawerInfo then
  103.                 local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
  104.                 print("Attempting to push bonemeal to drawer: " .. drawerName)
  105.                 local success = outputHopper.pushItems(drawerName, slot, item.count, 2)
  106.                 if success == 0 then
  107.                     print("Cannot push bonemeal to drawer: " .. drawerName .. "! Check storage capacity.")
  108.                     return false -- Failed push, continue checking other items
  109.                 else
  110.                     print("Moved " .. item.count .. " of bonemeal to drawer: " .. drawerName)
  111.                 end
  112.             else
  113.                 print("No drawer available for bonemeal! Add or upgrade a drawer.")
  114.                 return false -- No drawer available for this item
  115.             end
  116.         end
  117.     end
  118.     return true -- Successfully processed all bonemeal items
  119. end
  120.  
  121. -- Main loop function
  122. local function main()
  123.     while true do
  124.         local drawers = getConnectedDrawers()
  125.         local success = pushItemsFromDrawers(drawers)
  126.  
  127.         os.sleep(30) -- Wait 10 seconds to allow composting to finish
  128.         moveBoneMealToDrawer(drawers)
  129.     end
  130. end
  131.  
  132. -- Start the main function
  133. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement