Advertisement
CreeperNukeBoom

CGBCoreLib

Jan 19th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.77 KB | None | 0 0
  1. --CreeperGoBoom (CGB) Core Library API
  2. --A bunch of useful functions
  3.  
  4. local funcs = {}
  5. local doOnceNoColorWarning = {
  6.   ["colorPrint"] = true,
  7.   ["errorPrint"] = true
  8. }  -- For displaying a one time warning about no color capability
  9.  
  10.  
  11. --Writes data to a file, data can be anything except a function
  12. function funcs.fileWrite(stringFileName,data)
  13.   local file = fs.open(stringFileName, "w")
  14.   file.write(data)
  15.   file.close()
  16. end
  17.  
  18. --Gets user input for a prompt
  19. function funcs.getUserInput(stringPrompt)  
  20.   print(stringPrompt)
  21.   local result = io.read()
  22.   return result
  23. end
  24.  
  25. --Checks a string against a table of answers and prints available answers if incorrect
  26. function funcs.checkAnswer(stringInput,tableAnswers)
  27.   for _, key in pairs(tableAnswers) do
  28.     if key == stringInput then
  29.       return true
  30.     end
  31.   end
  32.   print("Answer not available, please use one of: ", table.concat(tableAnswers, ", "), ".")
  33.   return false
  34. end
  35.  
  36. --Checks a string against a table of answers.
  37. function funcs.checkAnswerSilent(stringInput, tableAnswers)
  38.   for _, key in pairs(tableAnswers) do
  39.     if key == stringInput then
  40.       return true
  41.     end
  42.   end
  43.   return false
  44. end
  45.  
  46. --Gets user input and checks input against answer table.
  47. function funcs.getAnswer(stringPrompt, tableAnswers)  
  48.   local input
  49.   repeat
  50.     input = funcs.getUserInput(stringPrompt)
  51.   until funcs.checkAnswer(input, tableAnswers)
  52.   return input
  53. end
  54.  
  55. --Gets user input and checks against answer table while showing available answers in prompt
  56. function funcs.getAnswerWithPrompts(stringPrompt, tableAnswers)
  57.   local input
  58.   repeat
  59.     input = funcs.getUserInput(stringPrompt .. " " .. table.concat(tableAnswers, ", "), ".")
  60.   until funcs.checkAnswer(input, tableAnswers)
  61.   return input
  62. end
  63.  
  64. --Returns wrap if peripheral name found
  65. function funcs.findPeripheral(stringName,stringAltName)  
  66.   if type(stringName) ~= "string" then return end
  67.   if type(stringAltName) ~= "string" then return end
  68.   return peripheral.find(stringName) or peripheral.wrap(stringAltName)
  69. end
  70.  
  71. --Returns entered name or altname if peripheral found
  72. function funcs.peripheralCheck(stringName, stringAltName)
  73.   if type(stringName) ~= "string" then return end
  74.   if type(stringAltName) ~= "string" then return end
  75.   if peripheral.find(stringName) then return stringName elseif peripheral.wrap(stringAltName) then return stringAltName end
  76. end
  77.  
  78. --Returns a table of all connected peripheral names containing Name
  79. --Function idea by FatBoyChummy
  80. function funcs.getPeripherals(stringName)
  81.   local peripherals = {}
  82.   local n = 0
  83.   for _, name in pairs(peripheral.getNames()) do
  84.     if name:find(stringName) then
  85.       n = n + 1
  86.       peripherals[n] = name
  87.     end
  88.   end
  89.   return peripherals
  90. end
  91.  
  92. --Returns a table of color names as string
  93. function funcs.getColorNames()
  94.   local doNotAddIfContain = {"test","pack","rgb","combine","subtract"}
  95.   local colorNames = {}
  96.   local doNotAdd = false
  97.   for k, v in pairs(colors) do
  98.     for _ , check in pairs(doNotAddIfContain) do
  99.       if k:find(check) then  --check k against list of do not adds.
  100.         doNotAdd = true
  101.         break
  102.       end
  103.     end
  104.     if not doNotAdd then
  105.       table.insert(colorNames,k)      -- Test did not find a match in do not add, add checked color name.
  106.     else
  107.       doNotAdd = false   -- Test found a match containing a do not add. Reset for next check.
  108.     end  
  109.   end
  110.   return colorNames
  111. end
  112.  
  113. local colorNamesList = funcs.getColorNames()
  114.  
  115. function funcs.loadConfig(configFileName)
  116.   local file = fs.open(configFileName, "r")
  117.   local fData = file.readAll()
  118.   local config = textutils.unserialize(fData)
  119.   file.close()
  120.   return config
  121. end
  122.  
  123. function funcs.saveConfig(configFileName,data)
  124.   local sData = textutils.serialize(data)
  125.   funcs.fileWrite(configFileName,sData)
  126. end
  127.  
  128. function funcs.tablePrint(tableVar)
  129.   if tableVar then local sData = textutils.serialize(tableVar)
  130.   print(sData)
  131.   end
  132. end
  133.  
  134. function funcs.getTableSize(table)
  135.   local count = 0
  136.   for i , _ in pairs(table) do
  137.     count = count + 1
  138.   end
  139.   return count
  140. end
  141.  
  142. function funcs.getAnswerAsNumbers(stringPrompt,tableAnswers) --returns answer chosen from table.
  143.   --Lists entries
  144.   local answer
  145.   local tableVar = {} --stores key and value depending if key is a number or not
  146.   repeat
  147.     local count = 0
  148.     print(stringPrompt)
  149.     for key , value in pairs(tableAnswers) do
  150.       if type(key) == "number" then --key isnt always a number
  151.         print(key,value)
  152.         tableVar[key]=value --What you see come up on screen is what is saved.
  153.       else
  154.         count = count + 1
  155.         print(count,key)
  156.         tableVar[count]=key
  157.       end
  158.     end
  159.     print("Please enter the corresponding number for your selection.")
  160.     answer = tonumber(io.read())
  161.     if answer then          --Answer must be a number else it is a string
  162.       if answer > funcs.getTableSize(tableVar) then
  163.         print("That's out of range, please try again!")
  164.       end
  165.     else  --in case someone enters something other than a number
  166.       print("That's not a number. Please only answer with a number!")
  167.     end
  168.   until answer and answer <= funcs.getTableSize(tableVar)  --must have this check else it will error if answer is string
  169.   return tableVar[answer]
  170. end
  171.  
  172. function funcs.getAnswerAsNumbersGrouped(stringPrompt,tableAnswers) --returns answer chosen from table.
  173.   --groups entries as per num per line, recommend no more than 3
  174.   local answer
  175.   local tableVar = {}  --creates a searchable table. since not all tables that are entered have numbers as keys
  176.   local tableVarNumbered = {}
  177.   repeat
  178.     local count = 0
  179.     print(stringPrompt)
  180.     for key , value in pairs(tableAnswers) do
  181.       if type(key) == "number" then --key isnt always a number
  182.         tableVar[key] = value --What you see come up on screen is what is saved.
  183.         tableVarNumbered[key] = key .. " " .. value
  184.       else
  185.         count = count + 1
  186.         tableVar[count] = key
  187.         tableVarNumbered[count] = count .. " " .. key
  188.       end
  189.     end
  190.     local tableSize = funcs.getTableSize(tableVar)
  191.     textutils.tabulate(tableVarNumbered)
  192.     print("Please enter the corresponding number for your selection.")
  193.     answer = tonumber(io.read())
  194.     if answer then          --Answer must be a number else it is a string
  195.       if answer > tableSize then
  196.         print("That's out of range, please try again!")
  197.       end
  198.     else  --in case someone enters something other than a number
  199.       print("That's not a number. Please only answer with a number!")
  200.     end
  201.   until answer and answer <= tableSize  --must have this check else it will error if answer is string
  202.   return tableVar[answer]
  203. end
  204.  
  205. function funcs.findPeripheralOnSide(stringPeripheral) --returns side. Ignores peripherals behind modems etc.
  206.   local sides = redstone.getSides()
  207.   for _ , side in pairs(sides) do
  208.     if peripheral.getType(side) == stringPeripheral then
  209.       local peripheralSide = side --This is needed for check below
  210.       return side
  211.     end
  212.   end
  213.   if not peripheralSide then  --peripheral not found as such is nil
  214.     return nil
  215.   end
  216. end
  217.  
  218. function funcs.peripheralCall(stringName,stringMethod,...)  --An enhanced peripheral.call
  219.   --Can check for name or side
  220.   --Also checks network for peripheral
  221.   local periph = peripheral.wrap(stringName)
  222.   -- Check that stringName peripheral exists
  223.   if not periph then
  224.     error("'" .. stringName .. "' is not a peripheral, check spelling and try again.")
  225.   end
  226.   -- Check that peripheral supports stringMethod
  227.   if not periph[stringMethod] then
  228.     print("'" .. stringName .. "' Methods:")
  229.     print(textutils.pagedTabulate(peripheral.getMethods(stringName)))
  230.     error("'" .. stringMethod .. "' is not a method of: '" .. stringName .. "', check method names listed above and try again.")
  231.   else
  232.   -- Peripheral exists and method supported, run it.
  233.     periph[stringMethod](...)
  234.     periph = nil   --peripheral interaction successful. clear wrap from memory
  235.   end
  236. end
  237.  
  238. -- A simplified table.insert
  239. function funcs.tableInsert(tableVar,numOrStringKey,value)
  240.   if type(value) == "nil"  then
  241.     table.insert(tableVar,numOrStringKey)
  242.   else
  243.     local key = numOrStringKey or #tableVar + 1
  244.     tableVar[key]=value
  245.   end
  246. end
  247.  
  248. function funcs.colorPrint(stringColorName,string)
  249.   if term.isColor() then
  250.     if not pcall(term.setTextColour,colors[stringColorName]) then
  251.       print(textutils.pagedTabulate(colorNamesList))
  252.       error("Error: colorPrint: incorrect color name entered: '" .. stringColorName .. "'. please check with above list")
  253.     else
  254.       print(string)
  255.       term.setTextColour(colors.white)
  256.     end
  257.   else
  258.     if doOnceNoColorWarning.colorPrint == true then
  259.       print("Warning: colorPrint: Cannot output color here! This message only shows once!")
  260.       doOnceNoColorWarning.colorPrint = false
  261.     end
  262.     print(string)
  263.   end
  264. end
  265.  
  266. function funcs.errorPrint(string)
  267.   if term.isColor() then
  268.     term.setTextColour(colors.red)
  269.     print(string)
  270.     term.setTextColour(colors.white)
  271.   else
  272.     if doOnceNoColorWarning.errorPrint == true then
  273.       print("Warning: errorPrint: Cannot output color here! This message only shows once!")
  274.       doOnceNoColorWarning.errorPrint = false
  275.     end
  276.     print(string)
  277.   end
  278. end
  279.  
  280. --Checks a string against a list of strings. Only returns true or false
  281. function funcs.isInList(stringToCheck,tableList)
  282.   local isListed = false
  283.   for k,v in pairs(tableList) do
  284.     if v == stringToCheck then
  285.       isListed = true
  286.       return true
  287.     end
  288.   end
  289.   if not isListed then
  290.     return false
  291.   end
  292. end
  293.  
  294. --Turns a string into a table
  295. --ignores non alphanumeric chars
  296. function funcs.stringToTable(stringInput)
  297.   local tableOutput = {}
  298.   local n=0
  299.   for i in string.gmatch(stringInput, "%w+") do
  300.     n = n+1
  301.     tableOutput[n] = i
  302.   end
  303.   return tableOutput
  304. end
  305.  
  306. --Turns a string into a table
  307. --ignores only spaces
  308. function funcs.stringToTable(stringInput)
  309.   local tableOutput = {}
  310.   local n=0
  311.   for i in string.gmatch(stringInput, "%S+") do
  312.     n = n+1
  313.     tableOutput[n] = i
  314.   end
  315.   return tableOutput
  316. end
  317.  
  318. --Splits a string into seperate vars
  319. --Example: this, string = stringToVars("this string")
  320. --ignores non alphanumeric characters and counts them as spaces (-=+:;>?/) etc
  321. function funcs.stringToVars(stringInput)
  322.   local tableOutput = {}
  323.   local n=0
  324.   for i in string.gmatch(stringInput, "%w+") do
  325.     n = n+1
  326.     tableOutput[n] = i
  327.   end
  328.   return table.unpack(tableOutput)
  329. end
  330.  
  331. --Splits a string into seperate vars
  332. --Example: this, string = stringToVars("this string")
  333. --More inclusive, only ignores spaces
  334. function funcs.stringToVarsAll(stringInput)
  335.   local tableOutput = {}
  336.   local n=0
  337.   for i in string.gmatch(stringInput, "%S+") do
  338.     n = n+1
  339.     tableOutput[n] = i
  340.   end
  341.   return table.unpack(tableOutput)
  342. end
  343.  
  344. return funcs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement