Advertisement
hornedcommando

Minecraft ComputerCraft Modem Washer Above and Beyond

Nov 15th, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.23 KB | Gaming | 0 0
  1. ---
  2. ---Program to automate the washing of items using encased fan and depot
  3. ---Exports washable items from drawers to depot
  4. ---Imports washed items from depot to drawers
  5. ---Inventory balancing prevents processing if ingredients < outputs
  6. ---washable and washed lists can be modified to include/exclude items
  7. ---review IMPORTANT comments, these are configurables that will likely change based on your setup
  8. ---
  9.  
  10. --By: hornedcommando
  11.  
  12. -- Define peripherals
  13. local depot = peripheral.wrap("create:depot_0") -- IMPORTANT: Change this to the depot where washed items are stored
  14.  
  15. -- List of washable items
  16. local washable = {
  17.     ["biomesoplenty:orange_sand"] = true,
  18.     ["biomesoplenty:white_sand"] = true,
  19.     ["minecraft:sand"] = true,
  20.     ["create:wheat_flour"] = true,
  21.     ["minecraft:red_sand"] = true,
  22.     ["minecraft:gravel"] = true,
  23.     ["minecraft:soul_sand"] = true,
  24. }
  25.  
  26. -- List of washed items
  27. local washedItems = {
  28.     ["minecraft:clay_ball"] = true,
  29.     ["minecraft:gold_nugget"] = true,
  30.     ["minecraft:iron_nugget"] = true,
  31.     ["minecraft:quartz"] = true,
  32. }
  33.  
  34. -- Function to dynamically find all connected storage drawers
  35. local function getConnectedDrawers()
  36.     local drawers = {}
  37.     for _, name in ipairs(peripheral.getNames()) do
  38.         if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
  39.             local drawer = peripheral.wrap(name)
  40.             table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
  41.         end
  42.     end
  43.     return drawers
  44. end
  45.  
  46. -- Function to count specific items in the drawers
  47. local function countItems(drawers, itemName)
  48.     local count = 0
  49.     for _, drawerInfo in ipairs(drawers) do
  50.         local drawerContents = drawerInfo.drawer.list()
  51.         for slot, item in pairs(drawerContents) do
  52.             print("Checking slot " .. slot .. " in drawer " .. drawerInfo.name .. " for item " .. itemName)
  53.             if item.name == itemName then
  54.                 count = count + item.count
  55.                 print("Found " .. item.count .. " of " .. itemName .. " in slot " .. slot)
  56.             end
  57.         end
  58.     end
  59.     return count
  60. end
  61.  
  62. -- Function to push items from drawers to the depot
  63. local function pushItems(drawers, itemName)
  64.     for _, drawerInfo in ipairs(drawers) do
  65.         local drawerContents = drawerInfo.drawer.list()
  66.         for slot, item in pairs(drawerContents) do
  67.             if item.name == itemName then
  68.                 local success = drawerInfo.drawer.pushItems(peripheral.getName(depot), slot, item.count)
  69.                 if success == 0 then
  70.                     print("Failed to push item: " .. item.name)
  71.                     return false -- Failed push, continue checking other drawers
  72.                 else
  73.                     print("Pushed " .. item.count .. " of " .. item.name .. " to depot.")
  74.                 end
  75.             end
  76.         end
  77.     end
  78.     return true -- Successfully processed all items in the drawers
  79. end
  80.  
  81. -- Function to process sand
  82. local function processSand(drawers)
  83.     print("Processing sand...")
  84.  
  85.     -- Count the items in the drawers
  86.     local clayCount = countItems(drawers, "minecraft:clay_ball")
  87.  
  88.     local sandTypes = {
  89.         "minecraft:sand",
  90.         "biomesoplenty:white_sand",
  91.         "biomesoplenty:orange_sand"
  92.     }
  93.  
  94.     local highestSandCount = 0
  95.     local suitableSandType = nil
  96.  
  97.     for _, sandType in ipairs(sandTypes) do
  98.         local sandCount = countItems(drawers, sandType)
  99.         print("Clay count: " .. clayCount .. ", " .. sandType .. " count: " .. sandCount)
  100.         if sandCount > highestSandCount then
  101.             highestSandCount = sandCount
  102.             suitableSandType = sandType
  103.         end
  104.     end
  105.  
  106.     if highestSandCount > clayCount then
  107.         return pushItems(drawers, suitableSandType)
  108.     else
  109.         print("Skipping sand processing as no suitable sand type is available.")
  110.         return true
  111.     end
  112. end
  113.  
  114. -- Function to process red sand
  115. local function processRedSand(drawers)
  116.     print("Processing red sand...")
  117.     local goldNuggetCount = countItems(drawers, "minecraft:gold_nugget")
  118.     local redSandCount = countItems(drawers, "minecraft:red_sand")
  119.     print("Gold nugget count: " .. goldNuggetCount .. ", Red sand count: " .. redSandCount)
  120.     if goldNuggetCount > redSandCount then
  121.         print("Skipping red sand processing as gold nugget count is greater than red sand count.")
  122.         return true
  123.     end
  124.     return pushItems(drawers, "minecraft:red_sand")
  125. end
  126.  
  127. -- Function to process gravel
  128. local function processGravel(drawers)
  129.     print("Processing gravel...")
  130.     local ironNuggetCount = countItems(drawers, "minecraft:iron_nugget")
  131.     local gravelCount = countItems(drawers, "minecraft:gravel")
  132.     print("Iron nugget count: " .. ironNuggetCount .. ", Gravel count: " .. gravelCount)
  133.     if ironNuggetCount > gravelCount then
  134.         print("Skipping gravel processing as iron nugget count is greater than gravel count.")
  135.         return true
  136.     end
  137.     return pushItems(drawers, "minecraft:gravel")
  138. end
  139.  
  140. -- Function to process soul sand
  141. local function processSoulSand(drawers)
  142.     print("Processing soul sand...")
  143.     local quartzCount = countItems(drawers, "minecraft:quartz")
  144.     local soulSandCount = countItems(drawers, "minecraft:soul_sand")
  145.     print("Quartz count: " .. quartzCount .. ", Soul sand count: " .. soulSandCount)
  146.     if quartzCount > soulSandCount then
  147.         print("Skipping soul sand processing as quartz count is greater than soul sand count.")
  148.         return true
  149.     end
  150.     return pushItems(drawers, "minecraft:soul_sand")
  151. end
  152.  
  153. -- Function to find a drawer for a specific item or an empty drawer
  154. local function findDrawerForItem(drawers, itemName)
  155.     for _, drawerInfo in ipairs(drawers) do
  156.         local drawerContents = drawerInfo.drawer.list()
  157.         if drawerContents[2] and drawerContents[2].name == itemName then
  158.             return drawerInfo -- Return the drawerInfo which includes the name
  159.         end
  160.     end
  161.     for _, drawerInfo in ipairs(drawers) do
  162.         local drawerContents = drawerInfo.drawer.list()
  163.         if not drawerContents[2] then
  164.             return drawerInfo -- Return the first empty drawer
  165.         end
  166.     end
  167.     return nil -- Return nil if no drawer currently contains the item and no empty drawer is found
  168. end
  169.  
  170. -- Function to move washed items from the depot to the appropriate drawer
  171. local function moveWashedItemsToDrawer(drawers)
  172.     local depotItems = depot.list()
  173.     for slot, item in pairs(depotItems) do
  174.         if washedItems[item.name] then
  175.             local targetDrawerInfo = findDrawerForItem(drawers, item.name)
  176.             if targetDrawerInfo then
  177.                 local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
  178.                 print("Attempting to push washed item to drawer: " .. drawerName)
  179.                 local success = depot.pushItems(drawerName, slot, item.count, 2)
  180.                 if success == 0 then
  181.                     print("Cannot push washed item to drawer: " .. drawerName .. "! Check storage capacity.")
  182.                     return false -- Failed push, continue checking other items
  183.                 else
  184.                     print("Moved " .. item.count .. " of " .. item.name .. " to drawer: " .. drawerName)
  185.                 end
  186.             else
  187.                 print("No drawer available for washed item! Add or upgrade a drawer.")
  188.                 return false -- No drawer available for this item
  189.             end
  190.         else
  191.             print("Item " .. item.name .. " is not a washed item.")
  192.         end
  193.     end
  194.     return true -- Successfully processed all washed items
  195. end
  196.  
  197. -- Main loop function
  198. local function main()
  199.     while true do
  200.         local drawers = getConnectedDrawers()
  201.  
  202.         -- Process each type of item separately
  203.         local sandSuccess = processSand(drawers)
  204.         local redSandSuccess = processRedSand(drawers)
  205.         local gravelSuccess = processGravel(drawers)
  206.         local soulSandSuccess = processSoulSand(drawers)
  207.  
  208.         os.sleep(10) -- washing happens in bulk, program can be lazy
  209.         print("Moving washed items to drawers...")
  210.         moveWashedItemsToDrawer(drawers)
  211.     end
  212. end
  213.  
  214. -- Start the main function
  215. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement