Advertisement
kurruk

Sorting System (part1)

Feb 13th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. p = peripheral.wrap("back") -- Change side as needed.
  2. rednet.open("left") -- Change side as needed.
  3.  
  4. -- Variables that you have to set.
  5.  
  6. item = "cobble" -- Item that is in your storage media.
  7. lower = 1 -- Computer id for exracting computer.
  8.  
  9.  
  10. -- First startup
  11.  
  12. if not fs.exists("itemlist") then
  13.   h = fs.open("itemlist", "w")
  14.   h.write("0")
  15.   h.close()
  16. end
  17.  
  18.  
  19. -- Retreives number of items in storage.
  20.  
  21. f = fs.open("itemlist", "r")
  22. count = f.readAll()
  23. count = tonumber(count)
  24. f.close()
  25.  
  26.  
  27. -- Adds to number of items in storgae.
  28.  
  29. function3 = function()
  30.   f = fs.open("itemlist", "w")
  31.   f.writeLine(tostring(count))
  32.   f.close()
  33. end
  34.  
  35.  
  36. -- The function for sorting the item
  37.  
  38. function2 = function()
  39.   event, id, amount = os.pullEvent("isort_item")
  40.  
  41.   count = count + amount
  42.    
  43.   function3()
  44.  
  45.   p.sort(0)
  46. end
  47.  
  48.  
  49. -- The function for updating the monitor.
  50.  
  51. function1 = function()
  52.   sender, msg = rednet.receive()
  53.     if msg == "update" then
  54.       rednet.send(sender, item..": "..tostring(count))
  55.     else if sender == lower then
  56.             count = count - msg
  57.             function3()
  58.             print(count.." remaining")
  59.          end
  60.     end      
  61. end
  62.  
  63.  
  64. -- Main running code.
  65.  
  66. while true do
  67.  
  68.   parallel.waitForAny(function1, function2)
  69.  
  70. /* For anyone who does not know this function
  71. allows for two or more functions to run at once */
  72.  
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement