Advertisement
akihex

mystPortalDial

Jul 17th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1. local backgroundColor = colors.blue
  2. local altBackgroundColor = colors.lightBlue
  3. local greetingOne = "Please choose your"
  4. local greetingTwo = "destination"
  5. local timerDelay = 8
  6.  
  7. -- computer front points northward
  8. local dirs = {front="north",left="east",back="south",right="west"}
  9. local dir = nil
  10.  
  11. local mon = peripheral.find("monitor")
  12.  
  13. local chest = nil
  14. local slot = nil
  15.  
  16. for i,v in pairs(rs.getSides()) do
  17.   if peripheral.getType(v) == "ender_chest" then
  18.     chest = peripheral.wrap(v)
  19.     dir = dirs[v]
  20.   elseif peripheral.getType(v) == "book_receptacle" then
  21.     slot = peripheral.wrap(v)
  22.   end
  23. end
  24.  
  25. 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
  26.  
  27. function removeActiveBook()
  28.   if slot.getStackInSlot(1) then
  29.     if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book to chest"); error() end
  30.   end
  31. end
  32.  
  33. removeActiveBook()
  34. sleep(1)
  35.  
  36. local nDest = 4
  37. local destTable = {}
  38. local timer = nil
  39.  
  40. function padStr(str, len)
  41.   if #str > len then do return string.sub(str,1,len) end
  42.   else do return str .. string.rep(' ', len - #str) end
  43.   end
  44. end
  45.  
  46. function waitForTouchEvent()
  47.   repeat
  48.     event, side, monX, monY = os.pullEvent() --"monitor_touch")
  49.     if event == "timer" and side == timer then
  50.       removeActiveBook()
  51.       drawUserInterface()
  52.     end
  53.   until event == "monitor_touch"
  54.   removeActiveBook()
  55.   if nDest > 0 and (monY-3 > 0) and (monY-3 <= nDest) then
  56.     if slot.pullItem(dir,destTable[monY - 3],1) ~= 1 then print("Error while moving book to slot") end
  57.     timer = os.startTimer(timerDelay)
  58.   end
  59. end
  60.  
  61. function drawUserInterface()
  62.   destTable = {}
  63.   local books = chest.getAllStacks()
  64.   local monX, monY = mon.getSize()
  65.   mon.setBackgroundColor(backgroundColor)  
  66.   mon.clear()
  67.   mon.setCursorPos(1,1)
  68.   mon.write(greetingOne)
  69.   mon.setCursorPos(1,2)
  70.   mon.write(greetingTwo)
  71.   nDest = 0
  72.   local i = 0
  73.   for n,book in pairs(books) do
  74.     if book.rawName == "item.myst.linkbook" then
  75.       i = i + 1
  76.       destTable[i] = n
  77.       mon.setCursorPos(1, i + 3)
  78.       if (i % 2 == 0) then
  79.         mon.setBackgroundColor(altBackgroundColor)
  80.       else
  81.         mon.setBackgroundColor(backgroundColor)
  82.       end
  83.       name = padStr(book.destination,monX)
  84.       mon.write(name)        
  85.     end
  86.   end
  87.   if not i then
  88.     mon.setCursorPos(1, 4)
  89.     mon.write("No Entries")
  90.   end
  91.   nDest = #destTable
  92. end
  93.  
  94. while true do
  95.   drawUserInterface()
  96.   waitForTouchEvent()
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement