Advertisement
hornedcommando

Minecraft ComputerCraft Modem DrawerController Manager Above and Beyond

Nov 21st, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A program which pulls items from connected peripherals into a main storage drawer controller + attached drawers
  3. ---Mainly for sending farm outputs to primary storage
  4. ---This is considered an upgrade from the Modem Drawer Controller because:
  5. ---1. you don't need a modem for every drawer 2. operation is much faster
  6. ---
  7.  
  8. --By: hornedcommando
  9.  
  10. -- Define the controller peripheral
  11. local controller = peripheral.wrap("storagedrawers:controller_0")
  12. local occultism = peripheral.wrap("occultism:storage_controller_0")
  13.  
  14. -- Set of excluded peripherals, IMPORTANT: peripherals that do not have inventories,
  15. --or should not be pulled should be listed there
  16. local excludedPeripherals = {
  17.     [peripheral.getName(controller)] = true,
  18.     [peripheral.getName(occultism)] = true,
  19.     ["back"] = true,
  20.     ["top"] = true,
  21.     ["bottom"] = true,
  22.     ["left"] = true,
  23.     ["right"] = true
  24. }
  25.  
  26. -- Function to check if a drawer contains a specific item
  27. local function findSlotForItem(itemName)
  28.     local controllerContents = controller.list()
  29.     for slot, item in pairs(controllerContents) do
  30.         if item.name == itemName then
  31.             return slot
  32.         end
  33.     end
  34.     return nil
  35. end
  36.  
  37. -- Function to push items from a single peripheral to the controller
  38. local function pushItemsFromPeripheral(peripheralName)
  39.     local peripheralDevice = peripheral.wrap(peripheralName)
  40.     if not peripheralDevice then
  41.         print("Peripheral " .. peripheralName .. " not found!")
  42.         return false
  43.     end
  44.  
  45.     if not peripheralDevice.list then
  46.         print("Peripheral " .. peripheralName .. " does not have an inventory!")
  47.         return false
  48.     end
  49.  
  50.     local peripheralContents = peripheralDevice.list()
  51.     if not peripheralContents or next(peripheralContents) == nil then
  52.         print("Peripheral " .. peripheralName .. " is empty.")
  53.         return true -- No items in peripheral, nothing to process
  54.     end
  55.  
  56.     for slot, item in pairs(peripheralContents) do
  57.         print("Processing item: " .. item.name .. " from slot: " .. slot)
  58.         local targetSlot = findSlotForItem(item.name)
  59.  
  60.         -- If no specific slot for the item, find an empty one
  61.         if not targetSlot then
  62.             for i = 1, controller.size() do
  63.                 if not controller.getItemDetail(i) then
  64.                     targetSlot = i
  65.                     break
  66.                 end
  67.             end
  68.         end
  69.  
  70.         -- Attempt to push items to the controller
  71.         if targetSlot then
  72.             print("Pushing item: " .. item.name .. " to controller slot: " .. targetSlot)
  73.             local success = peripheralDevice.pushItems(peripheral.getName(controller), slot, item.count, targetSlot)
  74.             if success == 0 then
  75.                 print("Cannot push item to controller slot: " .. targetSlot .. " from peripheral " .. peripheralName .. "! Check storage capacity.")
  76.                 return false -- Failed push, continue checking other peripherals
  77.             end
  78.         else
  79.             print("No slot available for item " .. item.name .. " in controller! Add or upgrade a drawer.")
  80.             return false -- No slot available for this item
  81.         end
  82.     end
  83.     return true -- Successfully processed all items in this peripheral
  84. end
  85.  
  86. -- Main loop function
  87. local function main()
  88.     while true do
  89.         local allSuccess = true
  90.  
  91.         for _, name in ipairs(peripheral.getNames()) do
  92.             if not excludedPeripherals[name] then
  93.                 print("Checking peripheral: " .. name)
  94.                 -- Try pushing items from the current peripheral
  95.                 local success = pushItemsFromPeripheral(name)
  96.                 if not success then
  97.                     allSuccess = false -- Mark as false if any peripheral fails
  98.                 end
  99.             end
  100.         end
  101.  
  102.         -- Since this can pull a whole chest at once i've made the wait fairly long
  103.         -- IMPORTANT: if the program cannot keep up with your inputs reduce the wait
  104.         if allSuccess then
  105.             print("All peripherals processed successfully. Waiting 10 seconds before next check.")
  106.             os.sleep(60) -- Wait 10 seconds before checking again
  107.         else
  108.             print("Some peripherals failed to process. Retrying in 2 seconds.")
  109.             os.sleep(10) -- Short delay to allow immediate retry if a peripheral fails
  110.         end
  111.     end
  112. end
  113.  
  114. -- Start the main function
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement