Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local backgroundColor = colors.blue
- local altBackgroundColor = colors.lightBlue
- local greetingOne = "active link: "
- --"Please choose your"
- local greetingTwo = "no active link"
- --"destination"
- local timerDelay = 8
- local refreshScreenDelay = 4
- -- computer front points northward
- local dirs = {front="north",left="east",back="south",right="west"}
- local dir = nil
- local mon = peripheral.find("monitor")
- local chest = nil
- local slot = nil
- for i,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "ender_chest" then
- chest = peripheral.wrap(v)
- dir = dirs[v]
- elseif peripheral.getType(v) == "book_receptacle" then
- slot = peripheral.wrap(v)
- end
- end
- 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
- function removeActiveBook()
- if slot.getStackInSlot(1) then
- if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book to chest") end
- end
- end
- removeActiveBook()
- sleep(1)
- local nDest = nil
- --4
- local destTable = {}
- --local destNames = {}
- local timer = nil
- function padStr(str, len)
- if #str > len then do return string.sub(str,1,len) end
- else do return str .. string.rep(' ', len - #str) end
- end
- end
- function waitForTouchEvent()
- local refreshScreenTimer = os.startTimer(refreshScreenDelay)
- repeat
- event, side, monX, monY = os.pullEvent() --"monitor_touch")
- if event == "timer" and (side == timer or side == refreshScreenTimer) then
- if side == timer then removeActiveBook() end
- drawUserInterface()
- if side == refreshScreenTimer then refreshScreenTimer = os.startTimer(refreshScreenDelay) end
- end
- until event == "monitor_touch"
- --removeActiveBook()
- --drawUserInterface()
- if nDest > 0 and (monY-3 > 0) and (monY-3 <= nDest) then
- --if slot.pullItem(dir,destTable[monY - 3][1],1) ~= 1 then print("Error while moving book to slot") end
- local destination = destTable[monY-3][1][2]
- drawUserInterface()
- if destination == destTable[monY-3][1][2] then
- removeActiveBook()
- for _,destinationID in pairs(destTable[monY - 3]) do if slot.pullItem(dir,destinationID[1],1) == 1 then break end end
- timer = os.startTimer(timerDelay)
- end
- else
- removeActiveBook()
- end
- end
- function drawUserInterface()
- destTable = {}
- -- destNames = {}
- local items = chest.getAllStacks()
- local books = {}
- for n,item in pairs(items) do
- if item.rawName == "item.myst.linkbook" then
- if not books[item.destination] then
- books[item.destination] = {n}
- else
- books[item.destination][#(books[item.destination])+1] = n
- end
- end
- end
- local sortedBooks = {}
- for n in pairs(books) do table.insert(sortedBooks,n) end
- table.sort(sortedBooks)
- local monX, monY = mon.getSize()
- mon.setBackgroundColor(backgroundColor)
- mon.clear()
- mon.setCursorPos(1,1)
- local active = slot.getStackInSlot(1)
- if active and active.rawName == "item.myst.linkbook" then
- mon.write(greetingOne)
- mon.setCursorPos(1,2)
- mon.write(active.destination)
- else
- mon.write(greetingTwo)
- end
- --
- --mon.write(greetingTwo)
- nDest = 0
- local i = 0
- for _,book in ipairs(sortedBooks) do
- i = i + 1
- destTable[i] = {books[book],book}
- mon.setCursorPos(1, i + 3)
- if (i % 2 == 0) then
- mon.setBackgroundColor(altBackgroundColor)
- else
- mon.setBackgroundColor(backgroundColor)
- end
- -- destNames[#destNames+1] = book
- name = padStr(book,monX)
- mon.write(name)
- end
- if not i then
- mon.setCursorPos(1, 4)
- mon.write("No Entries")
- end
- nDest = #destTable
- end
- while true do
- drawUserInterface()
- waitForTouchEvent()
- end
Advertisement
Add Comment
Please, Sign In to add comment