Advertisement
Shaka01

smart stockpile receiver

Feb 2nd, 2023 (edited)
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. --Receiver
  2.  
  3. -- Define the inventory to store items in
  4. storage = "occultism:stable_wormhole_0"
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. configured = true
  13. --------------------------------------------
  14. -- Initialize
  15. local toInventory = peripheral.wrap(storage)
  16. local itemToTransfer = 0
  17. local amountToTransfer = 0
  18. fromInventories = {}
  19. modem = nil
  20. modem2 = nil
  21.  
  22.  
  23. -- Connect peripherals
  24. local sides = {"left", "right", "top", "bottom", "front", "back"}
  25. for i, side in ipairs(sides) do
  26.   local device = peripheral.getType(side)
  27.   if device == "modem" then
  28.     rednet.open(side)
  29.     if modem == nil then
  30.     modem = peripheral.wrap(side)
  31.     else
  32.     modem2 = peripheral.wrap(side)
  33.     end
  34.     -- print("Opened rednet on " .. side)
  35.   elseif device then
  36.     local wrapper = peripheral.wrap(side)
  37.     -- print("Wrapped " .. device .. " on " .. side)
  38.   end
  39. end
  40.  
  41.  
  42. function checkWireless(modemName)
  43.     for i, side in ipairs(sides) do
  44.         if modemName ~= nil then
  45.             if modemName.isWireless() then
  46.                 return true
  47.             else
  48.                 return false
  49.             end
  50.         else
  51.             return "noModem"
  52.         end
  53.     end
  54. end
  55.  
  56. if checkWireless(modem) == "noModem" or checkWireless(modem2) == "noModem" then
  57.     term.clear()
  58.     term.setCursorPos(1,1)
  59.     term.setTextColor(colors.orange)
  60.     print("Missing modems!\n\nNeed one wired and one wireless modem.")
  61.     event, key = os.pullEvent(key)
  62.     term.setTextColor(colors.red)
  63.     print("Rebooting.")
  64.     sleep(3)
  65.     os.reboot()
  66. end
  67.  
  68.  
  69. if checkWireless(modem) == false and checkWireless(modem2) == false then
  70.     term.clear()
  71.     term.setCursorPos(1,1)
  72.     term.setTextColor(colors.orange)
  73.     print("No >wireless< modem connected!\n\nConnect one and press any key.")
  74.     event, key = os.pullEvent(key)
  75.     term.setTextColor(colors.red)
  76.     print("Rebooting.")
  77.     sleep(3)
  78.     os.reboot()
  79. elseif checkWireless(modem) == true and checkWireless(modem2) == true then
  80.     term.clear()
  81.     term.setCursorPos(1,1)
  82.     term.setTextColor(colors.orange)
  83.     print("No >wired< modem connected!\n\nConnect one and press any key.")
  84.     event, key = os.pullEvent(key)
  85.     term.setTextColor(colors.red)
  86.     print("Rebooting.")
  87.     sleep(3)
  88.     os.reboot()
  89. end
  90.  
  91.  
  92.  
  93. function scanAndSendInventories(fromInventories)
  94.   local scannedItems = {}
  95.   local sentItems = {}
  96.   for i = 1, #fromInventories do
  97.     local inventory = peripheral.wrap(fromInventories[i])
  98.     for slot, item in pairs(inventory.list()) do
  99.       if not scannedItems[item.name] then
  100.         scannedItems[item.name] = item.count
  101.       else
  102.         scannedItems[item.name] = scannedItems[item.name] + item.count
  103.       end
  104.     end
  105.   end
  106.  
  107.   for itemName, itemCount in pairs(scannedItems) do
  108.     if not sentItems[itemName] then
  109.       sendRednetMessage(itemName, "stockpileupdate")
  110.       sentItems[itemName] = true
  111.     end
  112.   end
  113. end
  114.  
  115. -- Function to send rednet message
  116. function sendRednetMessage(itemName, protocoll)
  117. rednet.broadcast("saveItem " .. itemName, protocoll)
  118. end
  119.  
  120. function moveItems()
  121.   -- Loop through the list of inventories
  122.   for i, fromName in ipairs(fromInventories) do
  123.     local fromInventory = peripheral.wrap(fromName)
  124.  
  125.     -- Get a list of all items in the inventory
  126.     local items = fromInventory.list()
  127.  
  128.     -- Loop through the items in the inventory
  129.     for slot, item in pairs(items) do
  130.       -- Check if the item is the specified item to transfer
  131.       if item.name == itemToTransfer then
  132.         local transferred = fromInventory.pushItems(peripheral.getName(toInventory), slot, amountToTransfer)
  133.         amountToTransfer = amountToTransfer - transferred
  134.         if amountToTransfer <= 0 then
  135.           return
  136.         end
  137.       end
  138.     end
  139.   end
  140. end
  141.  
  142. function split(s, delimiter)
  143.     result = {};
  144.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  145.         table.insert(result, match);
  146.     end
  147.     return result;
  148. end
  149.  
  150. function receive()
  151.     senderID, message, protocol = rednet.receive()
  152.     split(message, " ")
  153.     itemToTransfer = tostring(result[1])
  154.     amountToTransfer = tonumber(result[2])
  155. end
  156.  
  157. function wholething()
  158.     receive()
  159.     if protocol == "stockpile" then
  160.     term.setTextColor(colors.white)
  161.     print(itemToTransfer, ": ",amountToTransfer)
  162.     moveItems()
  163.     end
  164. end
  165.  
  166. function updateAvailableItems()
  167.     term.clear()
  168.     term.setCursorPos(1,1)
  169.     print("Scan all attached inventories and send them to my brain?\n")
  170.     term.setTextColor(colors.red)
  171.     print("Hit Enter to confirm.")
  172.     local event, key = os.pullEvent("key")
  173.     if key == keys.enter then
  174.         scanAndSendInventories(fromInventories)
  175.         term.clear()
  176.         term.setCursorPos(1,1)
  177.         term.setTextColor(colors.lime)
  178.         print("Items transmitted!")
  179.         sleep(2)
  180.         os.reboot()
  181. end    
  182. end
  183.  
  184. function retransmitItems()
  185.     event, key = os.pullEvent("key")
  186.     if key == keys.space then
  187.         updateAvailableItems() 
  188.     end
  189. end
  190.  
  191. if configured == false then
  192.     updateAvailableItems()
  193.     local event, key = os.pullEvent("key")
  194.     shell.run("edit ", "startup")
  195. end
  196.    
  197.    
  198. --initialize inventories
  199. if checkWireless(modem) == false then
  200. newInventories = modem.getNamesRemote()
  201. else
  202. newInventories = modem2.getNamesRemote()
  203. end
  204.  
  205. for _, inventoryName in ipairs(newInventories) do
  206.     if inventoryName ~= storage then
  207.             -- print(inventoryName)
  208.       table.insert(fromInventories, inventoryName)
  209.     end
  210. end
  211.  
  212. ---loopy doo
  213. while true do
  214. parallel.waitForAny(retransmitItems, wholething)
  215. end
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement