t8rt0t

nameAPI.lua

Mar 16th, 2024 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. SECTION_CHAR = string.sub(utf8.char(167),2,2)
  2. --Why are these even different smh
  3. local MOTD_TO_COLOR = {
  4.     ["0"] = colors.black,
  5.     ["1"] = colors.blue,
  6.     ["2"] = colors.green,
  7.     ["3"] = colors.cyan,
  8.     ["4"] = colors.red,
  9.     ["5"] = colors.magenta,
  10.     ["6"] = colors.orange,
  11.     ["7"] = colors.lightGray,
  12.     ["8"] = colors.gray,
  13.     ["9"] = colors.blue,
  14.     ["a"] = colors.lime,
  15.     ["b"] = colors.cyan,
  16.     ["c"] = colors.red,
  17.     ["d"] = colors.pink,
  18.     ["e"] = colors.yellow,
  19.     ["f"] = colors.white
  20. }
  21.    
  22. --Prints the current computer's name, with corresponding colors
  23. --@OptionalArg string name: The name to write. If not specified, will use the computer's current label
  24. --@OptionalArg boolean stayOnSameLine: Whether it should print a newline character after or not
  25. local function writeCurrentName(name,stayOnSameLine)
  26.     name = name or os.getComputerLabel()
  27.     if not name then return end
  28.     local color = string.match(name,SECTION_CHAR.."%x")
  29.     local text = string.gsub(name,SECTION_CHAR.."%x","")
  30.     if color then term.setTextColor(MOTD_TO_COLOR[string.lower(string.gsub(color,SECTION_CHAR,""))]) end
  31.     term.write(text)
  32.     term.setTextColor(colors.white)
  33.     if not stayOnSameLine then print() end
  34. end
  35. --Generates a random name from names.json
  36. --@Returns string name: A random name, or "errored name"
  37. local function getRandomName()
  38.     if not fs.exists("names.json") then return "errored name" end
  39.     local file = fs.open("names.json","r")
  40.     local data = textutils.unserialiseJSON(file.readAll())
  41.     file.close()
  42.     if type(data) ~= "table" then return "errored name" end
  43.     return data[ math.random(#data) ] or "errored name"
  44. end
  45. --Generates a new computer label from scratch, with a random name and random color
  46. function generateNewLabel()
  47.     local name = getRandomName()
  48.     local color = string.format("%x",math.random(0,15))
  49.     os.setComputerLabel(SECTION_CHAR .. color .. name)
  50.     writeCurrentName()
  51. end
  52. --Changes the computer's label color to to the specified color, preserving the computer's name
  53. --@OptionalArg string color: the color to update the computer's label to. If not specified, chooses a random color
  54. function changeLabelColor(color)
  55.     local label = os.getComputerLabel()
  56.     if not label then return generateNewLabel() end
  57.     color = color or string.format("%x",math.random(0,15))
  58.     color = "" .. color
  59.     color = string.sub(color,1,1)
  60.     str = string.gsub(label,SECTION_CHAR.."%x","")
  61.     os.setComputerLabel(SECTION_CHAR..color..str)
  62.     term.write("Label ")
  63.     writeCurrentName(label,true)
  64.     term.write(" was updated to ")
  65.     writeCurrentName()
  66. end
  67. --Changes the label's name to the specified name, preserving the color
  68. --@OptionalArg string name: The new name for the computer. If not specified, chooses a random name
  69. function changeLabelName(name)
  70.     local label = os.getComputerLabel()
  71.     if not label then return generateNewLabel() end
  72.     local color = string.match(label,SECTION_CHAR.."%x")
  73.     name = name or getRandomName()
  74.     if not color or #color < 1 then color = string.format("%x",math.random(0,15)) end
  75.     os.setComputerLabel(color..name)
  76.     term.write("Label ")
  77.     writeCurrentName(label,true)
  78.     term.write(" was updated to ")
  79.     writeCurrentName()
  80. end
Advertisement
Add Comment
Please, Sign In to add comment