Advertisement
DubstepDentist

Computer Craft - Portal

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