Advertisement
cragrim

Direwolf20mystcraft-portal

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