Advertisement
Savage_Me55iah

CC EnderChest Control

Jun 28th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.19 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local scriptName, version, pastebinCode = "CCECC", "v1.0", "Jb6qjsL4"
  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.  
  14. -- SETUP --
  15.  
  16. local tArgs = {...}
  17.  
  18. do
  19.     update = function(script_name, pastebincode)
  20.         print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  21.         if fs.exists("update") then
  22.             fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  23.         else
  24.             print("UPDATE FAILED")
  25.         end
  26.         sleep(2) os.reboot()
  27.     end
  28. end
  29.  
  30. if tArgs[1] == "update" then update(scriptName, pastebinCode) end
  31.  
  32. dir = "top"
  33.  
  34. colorsRange = {}
  35. function addColor(paramName, paramColor, paramDec, paramHex, paramBin, paramPaint, paramDis)
  36.     colorsRange[paramName] = {["color"] = paramColor,["decimal"] = paramDec,["hexadecimal"] = paramHex,["binary"] = paramBin,["paint"] = paramPaint,["display"] = paramDis}
  37. end
  38. addColor("white", colors.white, 1, "0x1", 0000000000000001, 0, "#F0F0F0")
  39. addColor("orange", colors.orange, 2, "0x2", 0000000000000010, 0, "#F2B233")
  40. addColor("magenta", colors.magenta, 4, "0x4", 0000000000000100, 0, "#E57FD8")
  41. addColor("lightBlue", colors.lightBlue, 8, "0x8", 0000000000001000, 0, "#99B2F2")
  42. addColor("yellow", colors.yellow, 16, "0x10", 0000000000010000, 0, "#DEDE6C")
  43. addColor("lime", colors.lime, 32, "0x20", 0000000000100000, 0, "#7FCC19")
  44. addColor("pink", colors.pink, 64, "0x40", 0000000001000000, 0, "#F2B2CC")
  45. addColor("gray", colors.gray, 128, "0x80", 0000000010000000, 0, "#4C4C4C")
  46. addColor("lightGray", colors.lightGray, 256, "0x100", 0000000100000000, 0, "#999999")
  47. addColor("cyan", colors.cyan, 512, "0x200", 0000001000000000, 0, "#4C99B2")
  48. addColor("purple", colors.purple, 1024, "0x400", 0000010000000000, 0, "#B266E5")
  49. addColor("blue", colors.blue, 2048, "0x800", 0000100000000000, 0, "#3366CC")
  50. addColor("brown", colors.brown, 4096, "0x1000", 0001000000000000, 0, "#7F664C")
  51. addColor("green", colors.green, 8192, "0x2000", 0010000000000000, 0, "#57A64E")
  52. addColor("red", colors.red, 16384, "0x4000", 0100000000000000, 0, "#CC4C4C")
  53. addColor("black", colors.black, 32768, "0x8000", 1000000000000000, 0, "#191919")
  54.  
  55. if fs.exists("dataStore") then
  56.     local fh = fs.open("dataStore", 'r')
  57.     data = textutils.unserialize(fh:readAll())
  58.     fh.close()
  59. end
  60. data = data or {}
  61.  
  62. function wrapper(param)
  63.     local result = false
  64.     result = peripheral.wrap(param) or false
  65.     if result == false then print("missing chest @ "..param) end
  66.     return result
  67. end
  68.  
  69. eChest = wrapper(dir)
  70.  
  71. -- BACKSLASH WAS MESSING WITH MY SCRIPT EDITOR
  72. function BS() local result = "\\" return result end
  73.  
  74. -- booleanCheck(functionNum, variable) -- [1] returns true if function ran correctly OR if false variable was nil [2] returns variable OR false if nil
  75. -- used for bypassing nil errors
  76. function booleanCheck(param_functionNum, param_value)
  77.     local result, functionNum = {false, nil}, param_functionNum.."/001"
  78.     if tostring(param_value) ~= "nil" then result = {true, param_value} else result = {false, false} end
  79.     return result
  80. end
  81.  
  82. -- quickChoice(functionNum, boolean, value, value) -- [1] returns function ran correctly [2] returns result
  83. -- removes alot of standard "if" blocks
  84. function quickChoice(param_functionNum, param_boolean, param_true, param_false)
  85.     local result, functionNum = {false, false}, param_functionNum.."/002"
  86.     if type(booleanCheck(functionNum, param_boolean)[2]) == "boolean" then
  87.         if booleanCheck(functionNum, param_boolean)[2] == true then result = {true, param_true or false} else result = {true, param_false or true} end
  88. --  else
  89. --      logger.logger(functionNum, "Function Error, Expected: quickChoice(functionNum, boolean, value, value)", 2)
  90.     end
  91.     return result
  92. end
  93.  
  94. -- massBooleanCheck(functionNum, "and", {boolean, boolean, etc}) -- [1] returns function ran correctly [2] returns result
  95. -- removes alot of "and/or"
  96. function massBooleanCheck(param_functionNum, param_function, param_booleans)
  97.     local result, functionNum ={false, false}, param_functionNum.."/003"
  98.     if (tostring(param_function) == "and" or tostring(param_function) == "or") and type(param_booleans) == "table" then
  99.         if param_function == "and" then
  100.             result = {false, true}
  101.             for a,b in pairs(param_booleans) do result = {true, quickChoice(functionNum, b, result[2], false)[2]} end
  102.         else
  103.             for a,b in pairs(param_booleans) do result = {true, quickChoice(functionNum, b, true, result[2])[2]} end
  104.         end
  105. --  else
  106. --      logger.logger(functionNum, "Function Error, Expected: massBooleanCheck(functionNum, (and/or), {boolean, boolean, etc})", 2)
  107.     end
  108.     return result
  109. end
  110.  
  111. -- multiCompare(functionNum, "and", variable, {variable, variable, etc}) -- [1] returns function ran correctly [2] returns result
  112. -- compares multiple variables to 1 variable
  113. function multiCompare(param_functionNum, param_function, param_arg, param_variables)
  114.     local result, functionNum = {false, false}, param_functionNum.."/004"
  115.     if massBooleanCheck(functionNum, "and", {massBooleanCheck(functionNum, "or", {tostring(param_function) == "and", tostring(param_function) == "or"})[2], booleanCheck(functionNum, param_arg)[1], type(param_variables) == "table"})[2] then
  116.         if param_function == "and" then
  117.             result = {false, true}
  118.             for a,b in pairs(param_variables) do result = {true, quickChoice(functionNum, (param_arg == booleanCheck(functionNum, b)[2]), result[2], false)[2]} end
  119.         else
  120.             for a,b in pairs(param_variables) do result = {true, quickChoice(functionNum, (param_arg == booleanCheck(functionNum, b)[2]), true, result[2])[2]} end
  121.         end
  122. --  else
  123. --      logger.logger(functionNum, "Function Error, Expected: multiCompare(functionNum, (and/or), variable, {variable, variable, etc})", 2)
  124.     end
  125.     return result
  126. end
  127.  
  128. -- tolerance(param_functionNum, "relative", 10, 5, 6, -1) -- [1] returns function ran correctly [2] returns result
  129. -- returns boolean if value is within tolerance
  130. function tolerance(param_functionNum, param_function, param_target, param_value, param_tolerancePositive, param_toleranceNegative)
  131.     local result, functionNum = {false, false}, param_functionNum.."/005"
  132.     local tolerancePositive, toleranceNegative = param_tolerancePositive or 0, param_toleranceNegative or 0
  133.     if multiCompare(functionNum, "and", "number", {type(booleanCheck(functionNum, param_target)[2]), type(booleanCheck(functionNum, param_value)[2])})[2] and type(booleanCheck(functionNum, param_function)[2]) == "string" then
  134.         if param_function == "relative" then
  135.             tolerancePositive, toleranceNegative = param_target + tolerancePositive, param_target + toleranceNegative
  136.         end
  137.         result = {true, quickChoice(functionNum, massBooleanCheck(functionNum, "and", {(param_value <= tolerancePositive), (param_value >= toleranceNegative)})[2], true, false)[2]}
  138. --  else
  139. --      logger.logger(functionNum, "Function Error, Expected: tolerance(functionNum, relative, number, number, number, number)", 2)
  140.     end
  141.     return result
  142. end
  143.  
  144. function changeAColor(...)
  145.     local param = ...
  146. --  term.clearLine()
  147. --  screenWrite("changeAColor: "..param[1]..":"..param[2], colors.white, colors.black, 1,1)
  148.     local currColors = {eChest.getColorNames()}
  149.     currColors[param[1]] = param[2]
  150.     eChest.setColorNames(currColors[1], currColors[2], currColors[3])
  151. end
  152.  
  153. scroll = 1
  154.  
  155. function presavedScroll(param)
  156. --  term.clearLine()
  157. --  screenWrite("scrolling: "..param, colors.white, colors.black, 1,1)
  158.     local tempScroll = scroll
  159.     if booleanCheck("001", data[tempScroll])[1] then
  160.     if param == "<" then tempScroll = tempScroll - 1 else tempScroll = tempScroll + 1 end
  161.         if (tempScroll) > table.maxn(data) then tempScroll = 1 elseif (tempScroll) < 1 then tempScroll = table.maxn(data) end
  162.         if booleanCheck("001", data[tempScroll])[1] then
  163.             eChest.setColorNames(data[tempScroll]["colors"][1], data[tempScroll]["colors"][2], data[tempScroll]["colors"][3])
  164.             scroll = tempScroll
  165.         end
  166.     end
  167. end
  168.  
  169. function changeFrequency(param)
  170.     local currFrequency = nil
  171.     if param == "<" then currFrequency = tonumber(math.floor(eChest.getFrequency())) - 1 else currFrequency = tonumber(math.floor(eChest.getFrequency())) + 1 end
  172.     if currFrequency > 4095 then currFrequency = 0 elseif currFrequency < 0 then currFrequency = 4095 end
  173.     eChest.setFrequency(currFrequency)
  174. --  term.clearLine()
  175. --  screenWrite("setting frequency to: "..currFrequency..param, colors.white, colors.black, 1,1)
  176. end
  177.  
  178. function removeFrequency(param)
  179.     if tostring(data[1]) ~= "nil" then
  180.         for num=scroll,table.maxn(data) do
  181.             data[num] = data[(num + 1)] or nil
  182.         end
  183.         local fh = fs.open("dataStore", 'w')
  184.         fh.write(textutils.serialize(data))
  185.         fh.close()
  186.         scroll = 1
  187.         term.setCursorPos(1,1) term.clearLine()
  188.         print("Removed")
  189.     end
  190. end
  191.  
  192. function saveFrequency(param)
  193.     local currColors, result, exists = {eChest.getColorNames()}, nil, false
  194.     for a,b in pairs(data) do
  195.         result = {}
  196.         for num=1,3 do if data[a]["colors"][num] == currColors[num] then result[num] = true else result[num] = false end end
  197.         exists = 0
  198.         for a,b in pairs(result) do if b then exists = exists + 1 end end
  199.         if exists == 3 then exists = true else exists = false end
  200.     end
  201.     if exists then
  202.         term.setCursorPos(1,1) term.clearLine()
  203.         print("Error already exists")
  204.     else
  205.         term.setCursorPos(1,2) term.clearLine()
  206.         term.setCursorPos(1,1) term.clearLine()
  207.         print("Type Name then hit Enter: ")
  208.         local name = read()
  209.         for a,b in pairs(data) do
  210.             if tostring(data[a]["name"]) == name then exists = true end
  211.         end
  212.         if exists then
  213.             term.setCursorPos(1,2) term.clearLine()
  214.             term.setCursorPos(1,1) term.clearLine()
  215.             print("Error already exists")
  216.         else
  217.             table.insert(data, {["name"] = name, ["colors"] = {eChest.getColorNames()}})
  218.             local fh = fs.open("dataStore", 'w')
  219.             fh.write(textutils.serialize(data))
  220.             fh.close()
  221.             term.setCursorPos(1,2) term.clearLine()
  222.             term.setCursorPos(1,1) term.clearLine()
  223.             print("Saved")
  224.         end
  225.     end
  226. end
  227.  
  228. buttonList = {}
  229. function buttonListAdd(paramText, paramTColor, paramBColor, paramX, paramY, paramFunc, paramArg)
  230.     table.insert(buttonList, {["text"] = paramText, ["textColor"] = paramTColor, ["BackGroundColor"] = paramBColor, ["x"] = paramX, ["y"] = paramY, ["func"] = paramFunc, ["arg"] = paramArg})
  231. end
  232.  
  233. termX, termY = term.getSize()
  234. sections = {["mathY"] = math.floor((termY - 4) / 2) - 1}
  235. sections = {["mathY"] = sections["mathY"], [1] = math.floor((termX / 2) / 2) - 2, [2] = math.floor(termX / 2) - 2, [3] = math.floor(((termX / 2) / 2) * 3) - 2}
  236.  
  237.  
  238. for num=1,3 do
  239.     x, y = sections[num], sections["mathY"]
  240.     for a,b in pairs(colorsRange) do
  241.         buttonListAdd(" ", colors.white, colorsRange[a]["color"], x, y, changeAColor, {num, a})
  242.         if y == (sections["mathY"] + 3) then
  243.             x, y = x + 1, sections["mathY"]
  244.         else
  245.             y = y + 1
  246.         end
  247.     end
  248. end
  249.  
  250. buttonListAdd(" < ", colors.white, colors.red, sections[1], sections["mathY"] - 2, presavedScroll, "<")
  251. buttonListAdd(" > ", colors.white, colors.red, sections[3] + 1, sections["mathY"] - 2, presavedScroll, ">")
  252. buttonListAdd(" < ", colors.white, colors.red, sections[1], sections["mathY"] + 5, changeFrequency, "<")
  253. buttonListAdd(" > ", colors.white, colors.red, sections[3] + 1, sections["mathY"] + 5, changeFrequency, ">")
  254. buttonListAdd(" Save Frequency ", colors.white, colors.green, (termX / 2 - 8), sections["mathY"] - 4, saveFrequency, 1)
  255. buttonListAdd(" Remove Frequency ", colors.white, colors.green, (termX / 2 - 9), sections["mathY"] + 7, removeFrequency, 1)
  256.  
  257. function scanButtons(param)
  258.     local result = false
  259. --  print(param[3].." "..param[4])
  260.     for a,b in pairs(buttonList) do
  261.         if param[3] >= buttonList[a]["x"] and param[3] <= (buttonList[a]["x"] + (string.len(buttonList[a]["text"]) - 1)) and buttonList[a]["y"] == param[4] then
  262.             buttonList[a]["func"](buttonList[a]["arg"])
  263. --          print(true)
  264.             result = true
  265.         end
  266.     end
  267.     return result
  268. end
  269.  
  270. function screenWrite(paramText, paramTColor, paramBColor, paramX, paramY)
  271.     term.setCursorPos(paramX, paramY)
  272.     term.setTextColor(paramTColor)
  273.     term.setBackgroundColor(paramBColor)
  274.     term.write(paramText)
  275.     term.setTextColor(colors.white)
  276.     term.setBackgroundColor(colors.black)
  277.     term.setCursorPos(1, 1)
  278. end
  279.  
  280. function drawScreen()
  281.     clear, clearSpaces = sections[3] - 1 - sections[1] + 3, " "
  282.     for num=2,clear do clearSpaces = clearSpaces.." " end
  283.     screenWrite(clearSpaces, colors.white, colors.black, (sections[1] + 3), sections["mathY"] + 5)
  284.     screenWrite(clearSpaces, colors.white, colors.black, (sections[1] + 3), sections["mathY"] - 2)
  285.     for a,b in pairs(buttonList) do
  286.         screenWrite(buttonList[a]["text"], buttonList[a]["textColor"], buttonList[a]["BackGroundColor"], buttonList[a]["x"], buttonList[a]["y"])
  287.     end
  288.     text = " "..eChest.getFrequency().." "
  289.     screenWrite(text, colors.white, colors.blue, math.floor(termX / 2 - math.floor(string.len(text) / 2)), sections["mathY"] + 5)
  290.     if booleanCheck("001", data[scroll])[1] then
  291.         text = " "..data[scroll]["name"].." "
  292.         screenWrite(text, colors.white, colors.blue, math.floor(termX / 2 - math.floor(string.len(text) / 2)), sections["mathY"] - 2)
  293.     end
  294.     text = " Current Setting "
  295.     screenWrite(text, colors.white, colors.blue, math.floor(termX / 2 - math.floor(string.len(text) / 2)), sections["mathY"] + 9)
  296.     local currColors = {eChest.getColors()}
  297.     text = "      "
  298.     for num=(sections["mathY"] + 11),(termY - 1) do
  299.         screenWrite(text, colors.white, currColors[1], (math.floor(termX / 2) - 10), num)
  300.         screenWrite(text, colors.white, currColors[2], (math.floor(termX / 2) - 3), num)
  301.         screenWrite(text, colors.white, currColors[3], (math.floor(termX / 2) + 4), num)
  302.     end
  303.     screenWrite("Savage's Ender Hacks: https://pastebin.com/Jb6qjsL4", colors.white, colors.cyan, 1, termY)
  304. end
  305.  
  306. local updateScreen, event = true, {}
  307.  
  308. term.clear()
  309.  
  310. while true do
  311.     if tostring(event[1]) == "mouse_click" then updateScreen = scanButtons(event) end
  312.     if updateScreen then drawScreen() updateScreen = false end
  313.     event = {}
  314.     event = {os.pullEvent()}
  315. --  term.clearLine()
  316. --  screenWrite(tostring(event[1])..":"..tostring(event[3])..":"..tostring(event[4]).."   ", colors.white, colors.black, 1,1)
  317. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement