Advertisement
hornedcommando

Minecraft ComputerCraft Modem DrawerController Pull From Barrels Above and Beyond

Nov 22nd, 2024
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A program which pulls items from connected barrels into a main storage drawer controller + attached drawers
  3. ---Difference between this Controller and pastebin.com/WhCSpiNr is Whitelisting Barrels, instead of Blacklisting
  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. -- Function to check if a drawer contains a specific item
  15. local function findSlotForItem(itemName)
  16.     local controllerContents = controller.list()
  17.     for slot, item in pairs(controllerContents) do
  18.         if item.name == itemName then
  19.             return slot
  20.         end
  21.     end
  22.     return nil
  23. end
  24.  
  25. -- Function to push items from a single peripheral to the controller
  26. local function pushItemsFromPeripheral(peripheralName)
  27.     local peripheralDevice = peripheral.wrap(peripheralName)
  28.     if not peripheralDevice then
  29.         print("Peripheral " .. peripheralName .. " not found!")
  30.         return false
  31.     end
  32.  
  33.     if not peripheralDevice.list then
  34.         print("Peripheral " .. peripheralName .. " does not have an inventory!")
  35.         return false
  36.     end
  37.  
  38.     local peripheralContents = peripheralDevice.list()
  39.     if not peripheralContents or next(peripheralContents) == nil then
  40.         print("Peripheral " .. peripheralName .. " is empty.")
  41.         return true -- No items in peripheral, nothing to process
  42.     end
  43.  
  44.     for slot, item in pairs(peripheralContents) do
  45.         print("Processing item: " .. item.name .. " from slot: " .. slot)
  46.         local targetSlot = findSlotForItem(item.name)
  47.  
  48.         -- If no specific slot for the item, find an empty one
  49.         if not targetSlot then
  50.             for i = 1, controller.size() do
  51.                 if not controller.getItemDetail(i) then
  52.                     targetSlot = i
  53.                     break
  54.                 end
  55.             end
  56.         end
  57.  
  58.         -- Attempt to push items to the controller
  59.         if targetSlot then
  60.             print("Pushing item: " .. item.name .. " to controller slot: " .. targetSlot)
  61.             local success = peripheralDevice.pushItems(peripheral.getName(controller), slot, item.count, targetSlot)
  62.             if success == 0 then
  63.                 print("Cannot push item to controller slot: " .. targetSlot .. " from peripheral " .. peripheralName .. "! Check storage capacity.")
  64.                 return false -- Failed push, continue checking other peripherals
  65.             end
  66.         else
  67.             print("No slot available for item " .. item.name .. " in controller! Add or upgrade a drawer.")
  68.             return false -- No slot available for this item
  69.         end
  70.     end
  71.     return true -- Successfully processed all items in this peripheral
  72. end
  73.  
  74. -- Main loop function
  75. local function main()
  76.     while true do
  77.         local allSuccess = true
  78.  
  79.         for _, name in ipairs(peripheral.getNames()) do
  80.             if peripheral.getType(name) == "minecraft:barrel" then
  81.                 print("Checking peripheral: " .. name)
  82.                 -- Try pushing items from the current peripheral
  83.                 local success = pushItemsFromPeripheral(name)
  84.                 if not success then
  85.                     allSuccess = false -- Mark as false if any peripheral fails
  86.                 end
  87.             end
  88.         end
  89.  
  90.         -- Since this can pull a whole chest at once i've made the wait fairly long
  91.         -- IMPORTANT: if the program cannot keep up with your inputs reduce the wait
  92.         if allSuccess then
  93.             print("All peripherals processed successfully. Waiting 60 seconds before next check.")
  94.             os.sleep(60) -- Wait 60 seconds before checking again
  95.         else
  96.             print("Some peripherals failed to process. Retrying in 2 seconds.")
  97.             os.sleep(10) -- Short delay to allow immediate retry if a peripheral fails
  98.         end
  99.     end
  100. end
  101.  
  102. -- Start the main function
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement