Savage_Me55iah

CC_TELEPOSER

Mar 12th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local scriptName, version, pastebinCode = "TELEPOSER", "v1.0", "C2XQCaNr"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. --V1.0
  13. --V1.1 -
  14.  
  15. -- SETUP --
  16.  
  17. local tArgs = {...}
  18. local update_request = tostring(tArgs[1]) or nil
  19.  
  20. do
  21. update = function(script_name, pastebincode)
  22. print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  23. if fs.exists("update") then
  24. fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  25. else
  26. print("UPDATE FAILED")
  27. end
  28. sleep(2) os.reboot()
  29. end
  30. end
  31.  
  32. if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
  33.  
  34. -- label_check(script) --
  35. label_check = function(param_scriptName)
  36. local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
  37. while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
  38. term.clear() term.setCursorPos(1,1) print(param_text)
  39. param_labelB = tostring(read())
  40. if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
  41. os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
  42. if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
  43. end
  44. end
  45. label_check(scriptName)
  46.  
  47. id = os.getComputerID()
  48. label = os.getComputerLabel() or false
  49. if label == false then
  50. print("Please Type label here: ")
  51. os.setComputerLabel(tostring(read()))
  52. end
  53.  
  54. function boolean_check(params)
  55. local answer = nil
  56. if tostring(params) == "true" then
  57. answer = true
  58. else
  59. answer = false
  60. end
  61. return answer
  62. end
  63.  
  64. local settings_file = "config"
  65.  
  66. local settings_template = [[-- adjust these configuration options as necessary.
  67. -- IMPORTANT!, API won't load if there are spaces in the values
  68. COMPUTER_LABEL = "${label}"
  69. COMPUTER_ID = ${id}
  70.  
  71. TURTLEID = ${turtleid}
  72. TEXT_SCALE = ${text_scale}
  73. TELEPOSER_SIDE = "${teleposer_side}"
  74. OUTPUT_SIDE = "${output_side}"
  75. ]]
  76.  
  77. local function interpolate(s, params)
  78. return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  79. end
  80.  
  81. do
  82. save_settings = function()
  83. local filehandler = fs.open(tostring(settings_file), "w")
  84.  
  85. if tostring(CONFIG) == "nil" then
  86. settings = {
  87. label = os.getComputerLabel(),
  88. id = os.getComputerID(),
  89. turtleid = 1,
  90. text_scale = 1,
  91. teleposer_side = "down",
  92. output_side = "top"
  93. }
  94. else
  95. settings = {
  96. label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  97. id = os.getComputerID(),
  98. turtleid = CONFIG.TURTLEID or 1,
  99. text_scale = CONFIG.TEXT_SCALE or 1,
  100. teleposer_side = CONFIG.TELEPOSER_SIDE or "down",
  101. output_side = CONFIG.OUTPUT_SIDE or "top"
  102. }
  103. end
  104.  
  105. filehandler.write(interpolate(settings_template, settings))
  106. filehandler.close()
  107. end
  108. end
  109.  
  110. if fs.exists(settings_file) == false then
  111. save_settings()
  112. return
  113. else
  114. os.unloadAPI(settings_file)
  115. if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  116. CONFIG = nil
  117. for k, v in pairs(_G) do
  118. if k == settings_file then CONFIG = v break end
  119. end
  120. if CONFIG == nil then log_add("CONFIG came up nil", true) end
  121. end
  122.  
  123. do
  124. load_one = function(command)
  125. local answer = nil
  126. if fs.exists(tostring(command)) then
  127. local fileHandler = fs.open(command, 'r')
  128. answer = textutils.unserialize(fileHandler.readAll())
  129. fileHandler.close()
  130. end
  131. return answer
  132. end
  133.  
  134. save_one = function(command1, command2)
  135. local fileHandler = fs.open(tostring(command1), 'w')
  136. fileHandler.write(textutils.serialize(command2))
  137. fileHandler.close()
  138. end
  139. end
  140.  
  141. do
  142. -- modems = peripheral_find_type("modem", "wireless") .. returns a table of all peripherals of the same type --
  143. peripheral_find_type = function(peripheral_type, modem_type)
  144. local peripheralList, returnTable, modem_type = peripheral.getNames() or false, {}, modem_type or false
  145. if type(peripheralList) == "table" then
  146. for a,b in pairs(peripheralList) do
  147. local testType = peripheral.getType(b)
  148. if testType == tostring(peripheral_type) then
  149. if tostring(modem_type) == false then
  150. table.insert(returnTable, peripheral.wrap(b))
  151. else
  152. local Peripheral = peripheral.wrap(b)
  153. -- local wireless = boolean_check(Peripheral.isWireless())
  154. if modem_type == "wireless" and wireless then table.insert(returnTable, peripheral.wrap(b))
  155. elseif modem_type ~= "wireless" and not wireless then table.insert(returnTable, peripheral.wrap(b)) end
  156. end end end
  157. if tostring(returnTable[1]) == "nil" then returnTable = false end
  158. return returnTable
  159. end end
  160. end
  161.  
  162. local monitor = peripheral_find_type("monitor")
  163. local chests = peripheral_find_type("tile_extrautils_chestfull_name")
  164. local mon, chest = monitor[1], chests[1]
  165. local displayTable = {}
  166.  
  167. local button_loc, current_page = {}, 1
  168.  
  169. do
  170. item_list = function()
  171. chest.condenseItems()
  172. displayTable = {}
  173. for num=1,chest.getInventorySize() do
  174. local item = chest.getStackInSlot(num)
  175. if tostring(item) ~= "nil" then table.insert(displayTable, item["display_name"]) end
  176. end
  177. end
  178.  
  179. button_length = function()
  180. local length = 1
  181. for k,v in ipairs(displayTable) do
  182. if tonumber(string.len(v) + 2) > length then length = string.len(v) + 2 end
  183. end
  184. return length
  185. end
  186.  
  187. -- screenColor(colors.green, colors.blue)
  188. screenColor = function(param_commandb, param_colorBackground, param_colorText)
  189. local colorBackground, colorText = param_colorBackground or colors.black, param_colorText or colors.white
  190. if monitor[1].isColor() == false then
  191. if param_colorBackground ~= colors.black then colorBackground, colorText = colors.white, colors.black
  192. else colorBackground, colorText = colors.black, colors.white end
  193. end
  194. monitor[1].setTextColor(colorText)
  195. monitor[1].setBackgroundColor(colorBackground)
  196. end
  197.  
  198. -- screenWrite(mon[1], "hello world":upper(), 5, 3, colors.red, colors.black)
  199. screenWrite = function(param_command, param_text, param_x, param_y, param_colorBackground, param_colorText)
  200. param_command.setCursorPos(param_x, param_y)
  201. screenColor(param_command, param_colorBackground, param_colorText)
  202. param_command.write(param_text)
  203. screenColor()
  204. end
  205.  
  206. screen_drawbutton = function(command, text, num, x, y, num2)
  207. local params = {[1] = command, [2] = text or "FAIL", [3] = num, [4] = x, [5] = y}
  208. button_loc[num] = {}
  209. local text = " "..params[2].." "
  210. while string.len(tostring(text)) < tonumber(button_length()) and tostring(text) ~= " <<< SCROLL " and tostring(text) ~= " SCROLL >>> " do text = tostring(text).." " end
  211. button_loc[num]["x1"], button_loc[num]["y"] = x, y
  212. if num2 == num then color1 = colors.green else color1 = colors.red end
  213. screenWrite(params[1], text, params[4], params[5], color1, colors.white)
  214. button_loc[num]["x2"] = params[1].getCursorPos()
  215.  
  216. end
  217.  
  218. display = function(param)
  219. local start, x, y, selected = 1, 2, 4, param or "nil"
  220. button_loc = {}
  221. mon.setTextScale(tonumber(CONFIG.TEXT_SCALE))
  222. mon.clear()
  223. mon_x, mon_y = mon.getSize()
  224. mon_x, mon_y = mon_x - 2, mon_y - 4
  225. border_x1, border_y1, border_x2, border_y2 = 2, 4, mon_x + 1, mon_y + 2
  226. rows, mon_cols = math.floor(mon_y / 2), math.floor(mon_x / button_length())
  227. mon_total, page_length = rows * mon_cols, rows
  228. if current_page > 1 then start = (start + ((current_page - 1) * page_length)) end
  229. for a,b in pairs(displayTable) do
  230. y = 4
  231. for num2=1,rows do
  232. if x >= border_x1 and (x + button_length()) <= border_x2 and y <= border_y2 then
  233. if tostring(displayTable[start]) ~= "nil" then
  234. screen_drawbutton(mon, tostring(displayTable[start]), start, x, y, selected)
  235. end
  236. end
  237. start, y = start + 1, y + 2
  238. end
  239. x = x + button_length() + 1
  240. end
  241. screen_drawbutton(mon, "SCROLL >>>", "SCROLL >>>", (mon_x - 10), 2, selected)
  242. screen_drawbutton(mon, "<<< SCROLL", "<<< SCROLL", 2, 2, selected)
  243. end
  244.  
  245. run = function(param_num)
  246. chest.pushItem(CONFIG.TELEPOSER_SIDE, param_num, 1, 1)
  247. redstone.setOutput(CONFIG.OUTPUT_SIDE, true)
  248. sleep(2)
  249. redstone.setOutput(CONFIG.OUTPUT_SIDE, false)
  250. chest.pullItem(CONFIG.TELEPOSER_SIDE, 1)
  251. end
  252.  
  253. end
  254.  
  255.  
  256. while true do
  257. item_list()
  258. display()
  259. local Event = {os.pullEvent()}
  260. print(Event[1]..tostring(Event[2])..tostring(Event[3])..tostring(Event[4]))
  261. if Event[1] == "monitor_touch" then
  262. local button = nil
  263. for a,b in pairs(button_loc) do
  264. if Event[3] >= button_loc[a]["x1"] and Event[3] <= button_loc[a]["x2"] then
  265. if Event[4] == button_loc[a]["y"] or Event[4] == (button_loc[a]["y"] + 1) then
  266. button = a
  267. end end end
  268. if tostring(button) == "SCROLL >>>" then
  269. current_page = current_page + 1
  270. elseif tostring(button) == "<<< SCROLL" and current_page ~= 1 then
  271. current_page = current_page - 1
  272. end
  273. if tostring(button) ~= "nil" and tostring(button) ~= "<<< SCROLL" and tostring(button) ~= "SCROLL >>>" then display(button) run(button) end
  274. print(tostring(button))
  275. end
  276. print(Event[1])
  277. end
Advertisement
Add Comment
Please, Sign In to add comment