Advertisement
MrFinn

Untitled

Dec 2nd, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. -- Define the peripherals
  2. local inputInventory = peripheral.wrap("sophisticatedstorage:chest_1")
  3. local outputInventories = {}
  4.  
  5. -- Automatically find and sort connected output inventories
  6. for i = 1, 25 do
  7. local outputName = "create:mechanical_crafter_" .. i
  8. if peripheral.isPresent(outputName) then
  9. table.insert(outputInventories, peripheral.wrap(outputName))
  10. else
  11. error("Output inventory " .. outputName .. " not found!")
  12. end
  13. end
  14.  
  15. -- Sort outputInventories by number
  16. table.sort(outputInventories, function(a, b)
  17. local aName = peripheral.getName(a)
  18. local bName = peripheral.getName(b)
  19. local aNumber = tonumber(aName:match("_(%d+)$"))
  20. local bNumber = tonumber(bName:match("_(%d+)$"))
  21. return aNumber < bNumber
  22. end)
  23.  
  24. -- Constants
  25. local IGNORED_ITEM = "securitycraft:reinforced_red_stained_glass_pane"
  26. local DELAY_SECONDS = 5
  27. local REDSTONE_SIDES = {"top", "bottom", "left", "right", "front", "back"} -- Add all possible sides
  28.  
  29. -- Function to check if any side has redstone input
  30. local function isRedstoneActive()
  31. for _, side in ipairs(REDSTONE_SIDES) do
  32. if redstone.getInput(side) then
  33. return true
  34. end
  35. end
  36. return false
  37. end
  38.  
  39. -- Function to run the transfer logic
  40. local function distributeItems()
  41. local outputInventoriesIndex = 1
  42.  
  43. -- Loop through each slot in the input inventory
  44. for slot = 1, inputInventory.size() do
  45. if outputInventoriesIndex > #outputInventories then
  46. print("All output inventories are full or processed.")
  47. break
  48. end
  49.  
  50. local item = inputInventory.getItemDetail(slot)
  51.  
  52. if item then
  53. if item.name == IGNORED_ITEM then
  54. -- Ignore this item
  55. print("Ignored item in slot " .. slot)
  56. else
  57. -- Transpose the item to the corresponding output inventory using pullItems
  58. local transferred = outputInventories[outputInventoriesIndex].pullItems(
  59. peripheral.getName(inputInventory),
  60. slot,
  61. item.count
  62. )
  63. if transferred > 0 then
  64. outputInventoriesIndex = outputInventoriesIndex + 1
  65. end
  66. end
  67. else
  68. -- Empty slot, move to the next output inventory
  69. outputInventoriesIndex = outputInventoriesIndex + 1
  70. end
  71. end
  72. end
  73.  
  74. -- Main loop
  75. while true do
  76. if isRedstoneActive() then
  77. distributeItems()
  78. end
  79. sleep(DELAY_SECONDS)
  80. end
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement