Advertisement
ftibo

scan.lua

Aug 21st, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. os.loadAPI("matchItem.lua")
  2. mi = _G["matchItem.lua"]
  3.  
  4. curBrowse = "db"
  5. curPage = 1
  6. lineSel = 0
  7. db = {}
  8.  
  9. function initDb()
  10. file = fs.open("blockId.db", "r")
  11. data = file.readAll()
  12. file.close()
  13. return textutils.unserialize(data)
  14. end
  15.  
  16.  
  17. function getId()
  18. key=nil
  19. id=""
  20. term.setCursorPos(34, lineSel)
  21. term.blit(" ", "ffffff", "555555")
  22. term.setCursorPos(34, lineSel)
  23. while true do
  24. local event,key = os.pullEvent("key")
  25. if key>=2 and key <=11 then
  26. if key==11 then
  27. key=0
  28. else
  29. key=key-1
  30. end
  31.  
  32. if string.len(id) < 4 then
  33. term.blit(tostring(key), "f","5")
  34. id = id .. tostring(key)
  35. end
  36. end
  37. if key==28 then
  38. if confirmChange() then
  39. saveChanges(id)
  40. end
  41. drawScreen(id)
  42. break
  43. end
  44. end
  45. end
  46.  
  47. function saveChanges(id)
  48. if id=="" or id==nil then return end
  49.  
  50. if curBrowse == "db" then
  51. db[(lineSel-1)+((curPage-1)*12)].id = tonumber(id)
  52. else
  53. newItem = { newId = db[(lineSel-1)+((curPage-1)*12)].newId, id = id }
  54. db = initDb()
  55. table.insert(db, newItem)
  56. curBrowse = "db"
  57. end
  58. local file = fs.open("blockId.db", "w")
  59. data = textutils.serialize(db)
  60. file.write(data)
  61. file.close()
  62.  
  63. end
  64.  
  65. function confirmChange()
  66. local win = window.create(term.current(), 12, 6, 18, 4, true)
  67. win.setBackgroundColor(colors.lightBlue)
  68. win.setTextColor(colors.black)
  69. win.clear()
  70. win.setCursorPos(2,2)
  71. win.write("Confirm changes?")
  72. win.setCursorPos(4,3)
  73. win.write("(y)es / (n)o")
  74. key=0
  75.  
  76. while true do
  77. local _,key = os.pullEvent("key")
  78. if key == 21 then
  79. return true
  80. end
  81. if key == 49 then
  82. return false
  83. end
  84. end
  85.  
  86. end
  87.  
  88. function selectLine(y, inverse)
  89. if (y-1)+((curPage-1)*12)>table.getn(db) then return end
  90. if inverse then
  91. term.setBackgroundColor(colors.yellow)
  92. term.setTextColor(colors.black)
  93. end
  94. term.setCursorPos(1, y)
  95. term.clearLine()
  96. term.write(db[(y-1)+((curPage-1)*12)].newId)
  97. term.setCursorPos(34, y)
  98. term.write(db[(y-1)+((curPage-1)*12)].id)
  99.  
  100. term.setBackgroundColor(colors.black)
  101. term.setTextColor(colors.white)
  102. lineSel=y
  103. end
  104.  
  105.  
  106. function drawScreen()
  107. term.setBackgroundColor(colors.black)
  108. term.setTextColor(colors.white)
  109. term.clear()
  110.  
  111. term.setCursorPos(1,1)
  112. if curBrowse == nil then curBrowse = "db" end
  113. if curPage == nil then curPage = 1 end
  114. if curBrowse=="db" then
  115. term.blit("Scan chest","0000000000", "3333333333")
  116. else
  117. term.blit("Scan db","0000000", "3333333")
  118. end
  119. term.setCursorPos(12,1)
  120. term.blit("Next","0000", "3333")
  121. term.setCursorPos(17,1)
  122. term.blit("Previous","00000000", "33333333")
  123. term.setCursorPos(26,1)
  124. term.blit("Modify Id","000000000", "333333333")
  125. term.setCursorPos(36,1)
  126. term.blit("Quit","0000", "3333")
  127.  
  128.  
  129. for i=1,12 do
  130. if i+((curPage-1)*12)>table.getn(db) then break end
  131. term.setCursorPos(1, i+1)
  132. term.write(db[i+((curPage-1)*12)].newId)
  133. term.setCursorPos(34, i+1)
  134. term.write(db[i+((curPage-1)*12)].id)
  135. end
  136.  
  137. end
  138.  
  139.  
  140. db = initDb()
  141.  
  142. drawScreen()
  143. local chest = peripheral.wrap("bottom")
  144.  
  145. quit=false
  146.  
  147. while quit==false do
  148. local _,_,x,y = os.pullEvent("mouse_click")
  149. if y==1 then
  150. -- NEXT BUTTON
  151. if x>=12 and x<=15 then
  152.  
  153. if (curPage+1)*12 < table.getn(db)+12 then
  154. curPage=curPage+1
  155. print(curPage)
  156. lineSel=0
  157. drawScreen()
  158. end
  159. end
  160. -- PREVIOUS BUTTON
  161. if x>=17 and x<=24 then
  162. if curPage>1 then
  163. curPage=curPage-1
  164. lineSel=0
  165. drawScreen()
  166. end
  167. end
  168. -- MODIFY ID BUTTON
  169. if x>=26 and x<=34 then
  170. if lineSel>0 then
  171. if curBrowse == "db" then
  172. getId()
  173. else
  174. if db[lineSel-1].id == nil then
  175. getId()
  176. end
  177. end
  178. end
  179. end
  180. -- SCAN CHEST/DB NUTTON
  181. if x>=1 and x<=10 then
  182. if curBrowse == "db" then
  183. db = mi.getAllStacks(chest)
  184. curBrowse = "chest"
  185. else
  186. db = initDb()
  187. curBrowse = "db"
  188. end
  189. curPage = 1
  190. lineSel = 0
  191. drawScreen()
  192. end
  193.  
  194. -- QUIT BUTTON
  195. if x>=36 and x<=39 then
  196. term.clear()
  197. quit=true
  198. end
  199. else
  200. if lineSel > 0 then
  201. selectLine(lineSel, false)
  202. end
  203. selectLine(y, true)
  204. end
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement