Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Desc: A program which attemps to balance the inventories of a storage controller and occultism
- ---by first putting 999 of an item into occultism, and then the rest overflow into storage drawers
- ---
- ---
- --by hornedcommando
- -- Define the storage peripherals IMPORTANT: if you don't update this it may not work for your specific system
- local drawerController = peripheral.wrap("storagedrawers:controller_0")
- local occultismController = peripheral.wrap("occultism:storage_controller_0")
- -- Function to get the contents of a storage
- local function getStorageContents(storage)
- local contents = {}
- for slot, item in pairs(storage.list()) do
- contents[item.name] = (contents[item.name] or 0) + item.count
- end
- return contents
- end
- -- Function to move items between storages
- local function moveItems(source, target, itemName, amount)
- for slot, item in pairs(source.list()) do
- if item.name == itemName then
- local moved = source.pushItems(peripheral.getName(target), slot, amount)
- amount = amount - moved
- if amount <= 0 then
- break
- end
- end
- end
- end
- -- Main function to balance storage
- local function balanceStorage()
- local drawerContents = getStorageContents(drawerController)
- local occultismContents = getStorageContents(occultismController)
- -- Move items from drawers to occultism storage if not present or less than 999
- for itemName, count in pairs(drawerContents) do
- if not occultismContents[itemName] or occultismContents[itemName] < 999 then
- local amountToMove = math.min(999 - (occultismContents[itemName] or 0), count)
- moveItems(drawerController, occultismController, itemName, amountToMove)
- end
- end
- -- Move excess items from occultism storage to drawers
- for itemName, count in pairs(occultismContents) do
- if count > 999 then
- local excessAmount = count - 999
- moveItems(occultismController, drawerController, itemName, excessAmount)
- end
- end
- end
- -- Main loop function
- local function main()
- while true do
- balanceStorage()
- os.sleep(10) -- Adjust the sleep time as needed
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement