akihex

mystPortal

Jul 18th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local backgroundColor = colors.blue
  2. local altBackgroundColor = colors.lightBlue
  3. local greetingOne = "active link: "
  4. --"Please choose your"
  5. local greetingTwo = "no active link"
  6. --"destination"
  7. local timerDelay = 8
  8. local refreshScreenDelay = 4
  9.  
  10. -- computer front points northward
  11. local dirs = {front="north",left="east",back="south",right="west"}
  12. local dir = nil
  13.  
  14. local mon = peripheral.find("monitor")
  15.  
  16. local chest = nil
  17. local slot = nil
  18.  
  19. for i,v in pairs(rs.getSides()) do
  20.   if peripheral.getType(v) == "ender_chest" then
  21.     chest = peripheral.wrap(v)
  22.     dir = dirs[v]
  23.   elseif peripheral.getType(v) == "book_receptacle" then
  24.     slot = peripheral.wrap(v)
  25.   end
  26. end
  27.  
  28. if not chest or not dir or not slot or not mon then print("Error while trying to find chest, slot or monitor"); error() end
  29.  
  30. function removeActiveBook()
  31.   if slot.getStackInSlot(1) then
  32.     if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book to chest") end
  33.   end
  34. end
  35.  
  36. removeActiveBook()
  37. sleep(1)
  38.  
  39. local nDest = nil
  40. --4
  41. local destTable = {}
  42. --local destNames = {}
  43. local timer = nil
  44.  
  45. function padStr(str, len)
  46.   if #str > len then do return string.sub(str,1,len) end
  47.   else do return str .. string.rep(' ', len - #str) end
  48.   end
  49. end
  50.  
  51. function waitForTouchEvent()
  52.   local refreshScreenTimer = os.startTimer(refreshScreenDelay)
  53.   repeat
  54.     event, side, monX, monY = os.pullEvent() --"monitor_touch")
  55.     if event == "timer" and (side == timer or side == refreshScreenTimer) then
  56.       if side == timer then removeActiveBook() end
  57.       drawUserInterface()
  58.       if side == refreshScreenTimer then refreshScreenTimer = os.startTimer(refreshScreenDelay) end
  59.     end
  60.   until event == "monitor_touch"
  61.   --removeActiveBook()
  62.   --drawUserInterface()
  63.   if nDest > 0 and (monY-3 > 0) and (monY-3 <= nDest) then
  64.     --if slot.pullItem(dir,destTable[monY - 3][1],1) ~= 1 then print("Error while moving book to slot") end
  65.     local destination = destTable[monY-3][1][2]
  66.     drawUserInterface()
  67.     if destination == destTable[monY-3][1][2] then
  68.       removeActiveBook()
  69.       for _,destinationID in pairs(destTable[monY - 3]) do if slot.pullItem(dir,destinationID[1],1) == 1 then break end end
  70.       timer = os.startTimer(timerDelay)
  71.     end
  72.   else
  73.     removeActiveBook()
  74.   end
  75. end
  76.  
  77. function drawUserInterface()
  78.   destTable = {}
  79.  -- destNames = {}
  80.   local items = chest.getAllStacks()
  81.   local books = {}
  82.   for n,item in pairs(items) do
  83.     if item.rawName == "item.myst.linkbook" then
  84.       if not books[item.destination] then
  85.         books[item.destination] = {n}
  86.       else
  87.         books[item.destination][#(books[item.destination])+1] = n
  88.       end
  89.     end
  90.   end
  91.   local sortedBooks = {}
  92.   for n in pairs(books) do table.insert(sortedBooks,n) end
  93.   table.sort(sortedBooks)
  94.  
  95.   local monX, monY = mon.getSize()
  96.   mon.setBackgroundColor(backgroundColor)  
  97.   mon.clear()
  98.   mon.setCursorPos(1,1)
  99.   local active = slot.getStackInSlot(1)
  100.   if active and active.rawName == "item.myst.linkbook" then
  101.     mon.write(greetingOne)
  102.     mon.setCursorPos(1,2)
  103.     mon.write(active.destination)
  104.   else
  105.     mon.write(greetingTwo)
  106.   end
  107.   --
  108.   --mon.write(greetingTwo)
  109.   nDest = 0
  110.   local i = 0
  111.   for _,book in ipairs(sortedBooks) do
  112.     i = i + 1
  113.     destTable[i] = {books[book],book}
  114.     mon.setCursorPos(1, i + 3)
  115.     if (i % 2 == 0) then
  116.       mon.setBackgroundColor(altBackgroundColor)
  117.     else
  118.       mon.setBackgroundColor(backgroundColor)
  119.     end
  120.    -- destNames[#destNames+1] = book
  121.     name = padStr(book,monX)
  122.     mon.write(name)        
  123.   end
  124.   if not i then
  125.     mon.setCursorPos(1, 4)
  126.     mon.write("No Entries")
  127.   end
  128.   nDest = #destTable
  129. end
  130.  
  131. while true do
  132.   drawUserInterface()
  133.   waitForTouchEvent()
  134. end
Advertisement
Add Comment
Please, Sign In to add comment