DrFair

CC AppliedEnergistics Client

Nov 26th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. local Args = {...}
  2. if Args[1] == "update" then
  3. print("Updating...")
  4. program = shell.getRunningProgram()
  5. fs.delete(program)
  6. shell.run("pastebin", "get", "Wst1yuEy", program)
  7. print("Finished updating.")
  8. error()
  9. end
  10.  
  11. if not fs.exists("fui") then
  12. print( "Performing first time setup." )
  13. shell.run( "pastebin","get", "306Ns1qg", "fui" )
  14. print("Finished. Please run program again.")
  15. error()
  16. end
  17.  
  18. function safeString(input)
  19. text = tostring(input)
  20. local newText = {}
  21. for i = 1, #text do
  22. local val = text:byte(i)
  23. newText[i] = (val > 31 and val < 127) and val or 63
  24. end
  25. return string.char(unpack(newText))
  26. end
  27.  
  28. rednet.open("back")
  29.  
  30. os.loadAPI("fui")
  31. fui.setMonitorSide(nil)
  32. w,h = fui.getSize()
  33.  
  34. searchResults = {}
  35. lastSearch = ""
  36. searchText = ""
  37. selected = "n/a"
  38. selectedID = -1
  39. amountSelected = 1
  40. statusText = ""
  41. sortingID = 0
  42.  
  43. function getPacket(type)
  44. local packet = {}
  45. packet[1] = "ccaes"
  46. packet[2] = type
  47. return packet
  48. end
  49.  
  50. function drawSearchResults()
  51. for i = 1, 9 do
  52. fui.drawText(" ", 3, 3 + i, "white", "black") -- Clear field
  53. if i <= #searchResults then
  54. item = searchResults[i]
  55. fui.drawText(safeString(string.sub(item.display_name, 0, 16)), 3, 3 + i, "white", "black")
  56. fui.drawText(item.qty, 20, 3 + i, "white", "black")
  57. end
  58. end
  59. end
  60.  
  61. function drawSearchText()
  62. fui.drawText(" ", 11, 14, "white", "black") -- Clear search field
  63. fui.drawText(searchText .. "_", 11, 14, "white", "black")
  64. end
  65.  
  66. function drawAmountSelected()
  67. fui.drawText(" ", 19, 16, "white", "black") -- Clear field
  68. fui.drawText(amountSelected, 19, 16, "white", "black")
  69. end
  70.  
  71. function drawSelected()
  72. for i = 1, 9 do
  73. fui.drawText(" ", 2, 3 + i, "white", "black")
  74. if i <= #searchResults then
  75. if searchResults[i].searchID == selected then
  76. fui.drawText(">", 2, 3 + i, "white", "black")
  77. end
  78. end
  79. end
  80. end
  81.  
  82. function updateStatus(status)
  83. statusText = status
  84. fui.drawAlignText(" ", "mid", w / 2, h, "black", "white") -- Clear field
  85. fui.drawAlignText(status, "mid", w / 2, h, "black", "white")
  86. end
  87.  
  88. function requestSearch()
  89. local packet = getPacket(0)
  90. packet[3] = sortingID
  91. packet[4] = lastSearch
  92. rednet.broadcast(packet)
  93. end
  94.  
  95. function requestItems(id, amount)
  96. local packet = getPacket(2)
  97. packet[3] = amount
  98. packet[4] = id
  99. packet[5] = sortingID
  100. packet[6] = lastSearch
  101. rednet.broadcast(packet)
  102. end
  103.  
  104. function drawUI()
  105. -- getSearchResults()
  106. fui.clear()
  107. fui.resetButtons()
  108. fui.drawOutline("FAIR CC AE CLIENT", "", "black", "white")
  109. updateStatus(statusText)
  110. fui.drawBox(35, 2, 2, 17, "white")
  111. fui.drawButton("Item name", "itemname", 3, 2, 9, 1, "white", "black")
  112. fui.drawButton("QTY^", "qty", 20, 2, 4, 1, "white", "black")
  113. fui.drawText("------------------------", 2, 3, "white", "black")
  114. drawSearchResults()
  115. drawSelected()
  116. fui.drawText("------------------------", 2, 13, "white", "black")
  117. fui.drawText("Search:", 3, 14, "white", "black")
  118. fui.drawBox(2, 15, 24, 1, "white")
  119.  
  120. fui.drawButton("1", "1", 6, 16, 4, 1, "white", "cyan")
  121. fui.drawButton("--", "1-", 2, 16, 4, 1, "white", "red")
  122. fui.drawButton("++", "1+", 10, 16, 4, 1, "white", "green")
  123.  
  124. fui.drawButton("8", "8", 6, 17, 4, 1, "white", "cyan")
  125. fui.drawButton("--", "8-", 2, 17, 4, 1, "white", "red")
  126. fui.drawButton("++", "8+", 10, 17, 4, 1, "white", "green")
  127.  
  128. fui.drawButton("32", "32", 6, 18, 4, 1, "white", "cyan")
  129. fui.drawButton("--", "32-", 2, 18, 4, 1, "white", "red")
  130. fui.drawButton("++", "32+", 10, 18, 4, 1, "white", "green")
  131.  
  132. fui.drawButton("64", "64", 6, 19, 4, 1, "white", "cyan")
  133. fui.drawButton("--", "64-", 2, 19, 4, 1, "white", "red")
  134. fui.drawButton("++", "64+", 10, 19, 4, 1, "white", "green")
  135.  
  136. fui.drawBox(14, 16, 1, 4, "white")
  137.  
  138. fui.drawText("#: ", 16, 16, "white", "black")
  139. drawAmountSelected()
  140.  
  141. fui.drawButton("REQUEST", "request", 15, 17, 11, 3, "white", "green")
  142.  
  143. drawSearchText()
  144. end
  145.  
  146. function processPacket(packet)
  147. if packet[1] ~= "ccaec" then return end -- Not valid packet
  148. -- print("Got packet ID: " .. packet[2])
  149. if packet[2] == 0 then -- Update search packet
  150. searchResults = packet[3]
  151. drawSearchResults()
  152. drawSelected()
  153. elseif packet[2] == 1 then -- Status update packet
  154. updateStatus(packet[3])
  155. if packet[4] ~= nil then
  156. searchResults = packet[4]
  157. drawSearchResults()
  158. drawSelected()
  159. end
  160. end
  161. end
  162.  
  163. drawUI()
  164. requestSearch()
  165.  
  166. while true do
  167. fui.submitEvents()
  168. lastEvent = fui.getEvents()
  169. if lastEvent[1] == "rednet_message" then
  170. processPacket(lastEvent[3])
  171. end
  172. if fui.buttonPressed("1") then
  173. amountSelected = 1
  174. drawAmountSelected()
  175. elseif fui.buttonPressed("1-") then
  176. amountSelected = amountSelected - 1
  177. if amountSelected < 1 then amountSelected = 1 end
  178. drawAmountSelected()
  179. elseif fui.buttonPressed("1+") then
  180. amountSelected = amountSelected + 1
  181. if amountSelected >= 1000000 then amountSelected = 1000000 end
  182. drawAmountSelected()
  183. elseif fui.buttonPressed("8") then
  184. amountSelected = 8
  185. drawAmountSelected()
  186. elseif fui.buttonPressed("8-") then
  187. amountSelected = amountSelected - 8
  188. if amountSelected < 1 then amountSelected = 1 end
  189. drawAmountSelected()
  190. elseif fui.buttonPressed("8+") then
  191. amountSelected = amountSelected + 8
  192. if amountSelected >= 1000000 then amountSelected = 1000000 end
  193. drawAmountSelected()
  194. elseif fui.buttonPressed("32") then
  195. amountSelected = 32
  196. drawAmountSelected()
  197. elseif fui.buttonPressed("32-") then
  198. amountSelected = amountSelected - 32
  199. if amountSelected < 1 then amountSelected = 1 end
  200. drawAmountSelected()
  201. elseif fui.buttonPressed("32+") then
  202. amountSelected = amountSelected + 32
  203. if amountSelected >= 1000000 then amountSelected = 1000000 end
  204. drawAmountSelected()
  205. elseif fui.buttonPressed("64") then
  206. amountSelected = 64
  207. drawAmountSelected()
  208. elseif fui.buttonPressed("64-") then
  209. amountSelected = amountSelected - 64
  210. if amountSelected < 1 then amountSelected = 1 end
  211. drawAmountSelected()
  212. elseif fui.buttonPressed("64+") then
  213. amountSelected = amountSelected + 64
  214. if amountSelected >= 1000000 then amountSelected = 1000000 end
  215. drawAmountSelected()
  216. elseif fui.buttonPressed("request") then
  217. if selected ~= "" then
  218. updateStatus("Requesting " .. selected)
  219. requestItems(selected, amountSelected)
  220. end
  221. elseif fui.buttonPressed("itemname") then
  222. sortingID = 1
  223. fui.drawText("Item name^", 3, 2, "white", "black")
  224. fui.drawText("QTY ", 20, 2, "white", "black")
  225. requestSearch()
  226. elseif fui.buttonPressed("qty") then
  227. sortingID = 0
  228. fui.drawText("Item name ", 3, 2, "white", "black")
  229. fui.drawText("QTY^", 20, 2, "white", "black")
  230. requestSearch()
  231. end
  232. if lastEvent[1] == "monitor_touch" or lastEvent[1] == "mouse_click" then
  233. local x = lastEvent[3]
  234. local y = lastEvent[4]
  235. if x >= 2 and x <= 25 and y >= 4 and y <= 12 then
  236. selectedID = y - 3
  237. if selectedID <= #searchResults then
  238. selected = searchResults[selectedID].searchID
  239. drawSelected()
  240. end
  241. end
  242. elseif lastEvent[1] == "char" then
  243. local char = lastEvent[2]
  244. searchText = string.sub(searchText .. char, 0, 14)
  245. drawSearchText()
  246. elseif lastEvent[1] == "key" then
  247. if lastEvent[2] == 28 then -- Enter key press
  248. lastSearch = searchText
  249. requestSearch()
  250. elseif lastEvent[2] == 14 then -- Backspace key press
  251. if searchText ~= "" then
  252. searchText = string.sub(searchText, 0, string.len(searchText) - 1)
  253. drawSearchText()
  254. end
  255. elseif lastEvent[2] == 211 then -- Delete key press
  256. searchText = ""
  257. drawSearchText()
  258. end
  259. end
  260. end
Advertisement
Add Comment
Please, Sign In to add comment