Advertisement
hornedcommando

Minecraft ComputerCraft Modem Storage Balancer Above and Beyond

Nov 22nd, 2024
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A program which attemps to balance the inventories of a storage controller and occultism
  3. ---by first putting 999 of an item into occultism, and then the rest overflow into storage drawers
  4. ---
  5. ---
  6.  
  7. --by hornedcommando
  8.  
  9. -- Define the storage peripherals IMPORTANT: if you don't update this it may not work for your specific system
  10. local drawerController = peripheral.wrap("storagedrawers:controller_0")
  11. local occultismController = peripheral.wrap("occultism:storage_controller_0")
  12.  
  13. -- Function to get the contents of a storage
  14. local function getStorageContents(storage)
  15.     local contents = {}
  16.     for slot, item in pairs(storage.list()) do
  17.         contents[item.name] = (contents[item.name] or 0) + item.count
  18.     end
  19.     return contents
  20. end
  21.  
  22. -- Function to move items between storages
  23. local function moveItems(source, target, itemName, amount)
  24.     for slot, item in pairs(source.list()) do
  25.         if item.name == itemName then
  26.             local moved = source.pushItems(peripheral.getName(target), slot, amount)
  27.             amount = amount - moved
  28.             if amount <= 0 then
  29.                 break
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. -- Main function to balance storage
  36. local function balanceStorage()
  37.     local drawerContents = getStorageContents(drawerController)
  38.     local occultismContents = getStorageContents(occultismController)
  39.  
  40.     -- Move items from drawers to occultism storage if not present or less than 999
  41.     for itemName, count in pairs(drawerContents) do
  42.         if not occultismContents[itemName] or occultismContents[itemName] < 999 then
  43.             local amountToMove = math.min(999 - (occultismContents[itemName] or 0), count)
  44.             moveItems(drawerController, occultismController, itemName, amountToMove)
  45.         end
  46.     end
  47.  
  48.     -- Move excess items from occultism storage to drawers
  49.     for itemName, count in pairs(occultismContents) do
  50.         if count > 999 then
  51.             local excessAmount = count - 999
  52.             moveItems(occultismController, drawerController, itemName, excessAmount)
  53.         end
  54.     end
  55. end
  56.  
  57. -- Main loop function
  58. local function main()
  59.     while true do
  60.         balanceStorage()
  61.         os.sleep(10) -- Adjust the sleep time as needed
  62.     end
  63. end
  64.  
  65. -- Start the main function
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement