Advertisement
Guest User

portal

a guest
Feb 11th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | None | 0 0
  1. os.loadAPI("button")
  2. m = peripheral.wrap("right") --you may need to change this
  3. m.clear()
  4. rednet.open("bottom") --you may need to change this
  5. local page = 1
  6. local pages = 0
  7. local names = {}
  8. local turtles = {}
  9. local remove = false
  10.  
  11. function fillTurtles()
  12.    turtles[1] = 10 --you will need to change this
  13. --   turtles[2] = 114
  14. --   turtles[3] = 117
  15. end
  16.  
  17. function fillTable()
  18.    m.clear()
  19.    button.clearTable()
  20.    local totalrows = 0
  21.    local numNames = 0
  22.    local col = 2
  23.    local row = 12
  24.    local countRow = 1
  25.    local currName = 0
  26.    local npp = 12 --names per page
  27.    for turt, data in pairs(names) do
  28.       for i,j in pairs(data) do
  29.          totalrows = totalrows+1
  30.       end
  31.    end
  32.    pages = math.ceil(totalrows/npp)
  33.    print(totalrows)
  34.    for turt, data in pairs(names) do
  35.       currName = 0
  36.       for slot, name in pairs(data) do
  37.        currName = currName + 1
  38.        if currName > npp*(page-1) and currName < npp*page+1 then
  39.          row = 4+(countRow)
  40.          names[turt][slot] = string.sub(name, 0, 17)
  41.          button.setTable(string.sub(name, 0, 17), runStuff, turt..":"..slot, col, col+17 , row, row)
  42.          if col == 21 then
  43.            col = 2
  44.            countRow = countRow + 2
  45.          else
  46.            col = col+19
  47.          end
  48.        end
  49.       end
  50.    end
  51.    button.setTable("Next Page", nextPage, "", 21, 38, 1, 1)
  52.    button.setTable("Prev Page", prevPage, "", 2, 19, 1, 1)
  53.    button.setTable("Refresh", checkNames, "", 21, 38, 19, 19)
  54.    button.setTable("Remove Book", removeIt, "", 2, 19, 19, 19)
  55.    button.label(15,3, "Page: "..tostring(page).." of "..tostring(pages))
  56.    button.screen()
  57. end      
  58.  
  59. function nextPage()
  60.    if page+1 <= pages then page = page+1 end
  61.    fillTable()
  62. end
  63.  
  64. function prevPage()
  65.    if page-1 >= 1 then page = page-1 end
  66.    fillTable()
  67. end  
  68.                            
  69. function getNames()
  70.    names = {}
  71.    for x, y in pairs(turtles) do
  72.       names[y] = {}
  73.       rednet.send(y, "getNames")
  74.       local id, msg, dist = rednet.receive(2)
  75.       print(msg)
  76.       names[y] = textutils.unserialize(msg)
  77.    end
  78. end
  79.  
  80. function removeIt()
  81.    remove = not remove
  82. --   print(remove)
  83.    button.toggleButton("Remove Book")
  84. end
  85.  
  86. function runStuff(info)
  87.    if remove == true then
  88.       removeBook(info)
  89.    else
  90.       openPortal(info)
  91.    end      
  92. end
  93.  
  94. function removeBook(info)
  95.    local turt, slot = string.match(info, "(%d+):(%d+)")
  96.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  97.    data = "remove"..tostring(slot)
  98.    rednet.send(tonumber(turt), data)
  99.    rednet.receive()
  100.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  101.    remove=false
  102.    button.toggleButton("Remove Book")
  103. --   sleep(1)
  104.    checkNames()
  105. end  
  106.  
  107. function openPortal(info)
  108.    local turt,slot = string.match(info, "(%d+):(%d+)")
  109. --   print(names[tonumber(turt)][tonumber(slot)])
  110.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  111.    print(names[tonumber(turt)][tonumber(slot)])
  112.    data = "books"..tostring(slot)
  113.    rednet.send(tonumber(turt), data)
  114.    rednet.receive()
  115.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  116. end
  117.  
  118. function checkNames()
  119.    button.flash("Refresh")
  120.    for num, turt in pairs(turtles) do
  121.      rednet.send(turt, "checkSlots")
  122.      msg = ""
  123.      while msg ~= "done" do
  124.        id, msg, dist = rednet.receive()
  125.        if msg == "getName" then
  126.           m.clear()
  127.           m.setCursorPos(5, 12)
  128.           m.write("New book detected.")
  129.           m.setCursorPos(5, 14)
  130.           m.write("Please enter the name")
  131.           m.setCursorPos(5, 16)
  132.           m.write("On the computer")
  133.           m.setCursorPos(5, 18)
  134.           m.write("<<----")
  135.           term.clear()
  136.           term.write("Please enter a name for the new book: ")
  137.           name = read()
  138.           rednet.send(id, name)
  139.        end
  140.      end
  141.    end
  142.    getNames()
  143.    fillTable()
  144. end
  145.  
  146. function getClick()
  147.    event, side, x,y = os.pullEvent()
  148.    if event == "monitor_touch" then
  149.       button.checkxy(x,y)
  150.    elseif event == "redstone" then
  151.       print("redstone")
  152.       sleep(5)
  153.       checkNames()      
  154.    end
  155. end
  156.  
  157. fillTurtles()
  158. fillTable()
  159. checkNames()
  160.  
  161.  
  162. while true do
  163.    getClick()
  164. --   checkNames()
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement