Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Desc: A program which pulls items from connected peripherals into a main storage drawer controller + attached drawers
- ---Mainly for sending farm outputs to primary storage
- ---This is considered an upgrade from the Modem Drawer Controller because:
- ---1. you don't need a modem for every drawer 2. operation is much faster
- ---
- --By: hornedcommando
- -- Define the controller peripheral
- local controller = peripheral.wrap("storagedrawers:controller_0")
- local occultism = peripheral.wrap("occultism:storage_controller_0")
- -- Set of excluded peripherals, IMPORTANT: peripherals that do not have inventories,
- --or should not be pulled should be listed there
- local excludedPeripherals = {
- [peripheral.getName(controller)] = true,
- [peripheral.getName(occultism)] = true,
- ["back"] = true,
- ["top"] = true,
- ["bottom"] = true,
- ["left"] = true,
- ["right"] = true
- }
- -- Function to check if a drawer contains a specific item
- local function findSlotForItem(itemName)
- local controllerContents = controller.list()
- for slot, item in pairs(controllerContents) do
- if item.name == itemName then
- return slot
- end
- end
- return nil
- end
- -- Function to push items from a single peripheral to the controller
- local function pushItemsFromPeripheral(peripheralName)
- local peripheralDevice = peripheral.wrap(peripheralName)
- if not peripheralDevice then
- print("Peripheral " .. peripheralName .. " not found!")
- return false
- end
- if not peripheralDevice.list then
- print("Peripheral " .. peripheralName .. " does not have an inventory!")
- return false
- end
- local peripheralContents = peripheralDevice.list()
- if not peripheralContents or next(peripheralContents) == nil then
- print("Peripheral " .. peripheralName .. " is empty.")
- return true -- No items in peripheral, nothing to process
- end
- for slot, item in pairs(peripheralContents) do
- print("Processing item: " .. item.name .. " from slot: " .. slot)
- local targetSlot = findSlotForItem(item.name)
- -- If no specific slot for the item, find an empty one
- if not targetSlot then
- for i = 1, controller.size() do
- if not controller.getItemDetail(i) then
- targetSlot = i
- break
- end
- end
- end
- -- Attempt to push items to the controller
- if targetSlot then
- print("Pushing item: " .. item.name .. " to controller slot: " .. targetSlot)
- local success = peripheralDevice.pushItems(peripheral.getName(controller), slot, item.count, targetSlot)
- if success == 0 then
- print("Cannot push item to controller slot: " .. targetSlot .. " from peripheral " .. peripheralName .. "! Check storage capacity.")
- return false -- Failed push, continue checking other peripherals
- end
- else
- print("No slot available for item " .. item.name .. " in controller! Add or upgrade a drawer.")
- return false -- No slot available for this item
- end
- end
- return true -- Successfully processed all items in this peripheral
- end
- -- Main loop function
- local function main()
- while true do
- local allSuccess = true
- for _, name in ipairs(peripheral.getNames()) do
- if not excludedPeripherals[name] then
- print("Checking peripheral: " .. name)
- -- Try pushing items from the current peripheral
- local success = pushItemsFromPeripheral(name)
- if not success then
- allSuccess = false -- Mark as false if any peripheral fails
- end
- end
- end
- -- Since this can pull a whole chest at once i've made the wait fairly long
- -- IMPORTANT: if the program cannot keep up with your inputs reduce the wait
- if allSuccess then
- print("All peripherals processed successfully. Waiting 10 seconds before next check.")
- os.sleep(60) -- Wait 10 seconds before checking again
- else
- print("Some peripherals failed to process. Retrying in 2 seconds.")
- os.sleep(10) -- Short delay to allow immediate retry if a peripheral fails
- end
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement