Advertisement
Shaka01

itemMover

Feb 26th, 2023 (edited)
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. -- connect all inventories via wired modems
  2. -- moves items to one target inventory
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. --------------------------------------------
  12. -- Initialize
  13. local fromInventories = {}
  14. local modem = nil
  15. local dailyMoved = 0
  16. local movedCounter = 0
  17. local yPos = 1
  18.  
  19.  
  20. -- Define the inventory to move items to
  21. local targetInv = ""
  22.  
  23. -- Load target inventory from file
  24. if fs.exists("targetInv") then
  25.     local file = fs.open("targetInv", "r")
  26.     targetInv = file.readAll()
  27.     file.close()
  28. end
  29.  
  30. -- Prompt user to select target inventory if not set
  31. if targetInv == "" then
  32.     -- Find and display available inventories
  33.     local modem = nil
  34.     for i, side in ipairs(peripheral.getNames()) do
  35.         local device = peripheral.getType(side)
  36.         if device == "modem" then
  37.             modem = peripheral.wrap(side)
  38.             break
  39.         end
  40.     end
  41.  
  42.     if modem then
  43.         local options = modem.getNamesRemote()
  44.         term.clear()
  45.         term.setCursorPos(1, 1)
  46.         term.setTextColor(colors.black)
  47.         term.setBackgroundColor(colors.yellow)
  48.         print("Select a target inventory:")
  49.         term.setBackgroundColor(colors.black)
  50.         term.setTextColor(colors.white)
  51.         for i, option in ipairs(options) do
  52.             print(i .. ". " .. option)
  53.         end
  54.        
  55.         local choice = tonumber(read())
  56.         if choice and choice >= 1 and choice <= #options then
  57.             targetInv = options[choice]
  58.             local file = fs.open("targetInv", "w")
  59.             file.write(targetInv)
  60.             file.close()
  61.             term.clear()
  62.             term.setTextColor(colors.green)
  63.             term.setCursorPos(1, 1)
  64.             term.write("Set ")
  65.             term.setTextColor(colors.yellow)
  66.             term.write(targetInv)
  67.             term.setTextColor(colors.green)
  68.             print(" as target inventory.")
  69.             term.setTextColor(colors.gray)
  70.             print("\n\nPress any key to continue..")
  71.             event, key = os.pullEvent("key")
  72.         else
  73.             print("Invalid choice.")
  74.             return
  75.         end
  76.     else
  77.         print("No modem detected.")
  78.         return
  79.     end
  80. end
  81.  
  82.  
  83. local toInventory = peripheral.wrap(targetInv)
  84. term.clear()
  85. term.setCursorPos(1,1)
  86.  
  87. -- Connect peripherals
  88. local sides = {"left", "right", "top", "bottom", "front", "back"}
  89. for i, side in ipairs(sides) do
  90.   local device = peripheral.getType(side)
  91.   if device == "modem" then
  92.     modem = peripheral.wrap(side)
  93.     rednet.open(side)
  94.   elseif device then
  95.     local wrapper = peripheral.wrap(side)
  96.   end
  97. end
  98.  
  99. --initialize inventories
  100. newInventories = modem.getNamesRemote()
  101. for _, inventoryName in ipairs(newInventories) do
  102.     if inventoryName ~= targetInv then
  103.       table.insert(fromInventories, inventoryName)
  104.     end
  105. end
  106.  
  107. ---------functions
  108.  
  109. local function centerText(text, y, monitor) -- centers text on monitor or terminal
  110.   if monitor == nil then
  111.     monitor = term
  112.   end
  113.   local w, h = monitor.getSize()
  114.   local x = math.floor((w - string.len(text) + 2) / 2)
  115.   if y == nil then
  116.     local a, b = monitor.getCursorPos()
  117.     y = b
  118.   end
  119.   monitor.setCursorPos(x , y)
  120.   monitor.write(text)
  121. end
  122.  
  123. function moveAllItems(sourceName, targetName)
  124.   local sourceInv = peripheral.wrap(sourceName)
  125.   local targetInv = peripheral.wrap(targetName)
  126.   local tasks = {}
  127.   local invData = sourceInv.list()
  128.   local taskCount = 0
  129.   local totalMovedItems = 0 -- new variable to track the total moved items
  130.   for k, v in pairs(invData) do
  131.     if taskCount >= 200 then
  132.       break
  133.     end
  134.     taskCount = taskCount + 1
  135.     tasks[#tasks+1] = function()
  136.       local itemData = sourceInv.getItemDetail(k)
  137.       local movedItems = sourceInv.pushItems(targetName, k)
  138.       if movedItems < itemData.count or movedItems == 0 then
  139.         -- return false
  140.       else
  141.         totalMovedItems = totalMovedItems + movedItems -- increment total moved items
  142.       end
  143.     end
  144.   end
  145.   parallel.waitForAll(unpack(tasks))
  146.   return totalMovedItems -- return the total moved items
  147. end
  148.  
  149. function formatNumber(number)
  150.   local formatted = tostring(number)
  151.   local k = #formatted % 3
  152.   if k == 0 then k = 3 end
  153.   formatted = formatted:sub(1, k) .. formatted:sub(k+1):gsub("(%d%d%d)", ".%1")
  154.   return formatted
  155. end
  156.  
  157. local function moveItems()
  158.     local allInvItems = 0
  159.     for _, inventoryName in pairs(fromInventories) do
  160.         local invAmount = 0
  161.         invAmount = moveAllItems(inventoryName, targetInv)
  162.         allInvItems = allInvItems + invAmount
  163.         if invAmount > 0 then
  164.             term.setTextColor(colors.green)
  165.             term.setCursorPos(1, 6)
  166.             term.clearLine()
  167.             centerText("Moved " ..allInvItems.. " items.", 6)
  168.             sleep(0.1)
  169.         else
  170.             term.setTextColor(colors.white)
  171.             term.setCursorPos(1, 6)
  172.             term.clearLine()
  173.             centerText("Currently waiting for work", 6)
  174.             sleep(0.1)
  175.         end
  176.     end
  177.     return allInvItems
  178. end
  179.  
  180. function showUptime()
  181.   local uptime = os.clock()
  182.   local hours = math.floor(uptime / 3600)
  183.   local minutes = math.floor((uptime % 3600) / 60)
  184.   local seconds = math.floor(uptime % 60)
  185.  
  186.   -- Create the uptime string
  187.   local uptimeStr = ""
  188.   if hours > 0 then
  189.     uptimeStr = uptimeStr .. string.format("%d:", hours)
  190.   end
  191.   if minutes > 0 or hours > 0 then
  192.     uptimeStr = uptimeStr .. string.format("%d:", minutes)
  193.   end
  194.   uptimeStr = uptimeStr .. string.format("%d", seconds)
  195.  
  196.   -- Position the cursor and display the uptime string
  197.   local w, h = term.getSize()
  198.   term.setCursorPos(w - #uptimeStr + 1, h)
  199.   term.setTextColor(colors.gray)
  200.   term.clearLine()
  201.   term.write(uptimeStr)
  202.  
  203.   -- Wait for one second
  204.   sleep(1)
  205. end
  206.  
  207.  
  208.  
  209. local function all()
  210.     if not rs.getInput("top") then
  211.         local kk = moveItems()
  212.         term.setTextColor(colors.gray)
  213.         dailyMoved = dailyMoved + kk
  214.         showDaily = formatNumber(dailyMoved)
  215.         term.setCursorPos(1, 15)
  216.         term.clearLine()
  217.         centerText("Total items moved since last restart: " ..showDaily, 15)
  218.         term.setTextColor(colors.white)
  219.     end
  220. end
  221.  
  222. while true do
  223. parallel.waitForAny(all, showUptime)
  224. end
  225.  
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement