Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SECTION_CHAR = string.sub(utf8.char(167),2,2)
- --Why are these even different smh
- local MOTD_TO_COLOR = {
- ["0"] = colors.black,
- ["1"] = colors.blue,
- ["2"] = colors.green,
- ["3"] = colors.cyan,
- ["4"] = colors.red,
- ["5"] = colors.magenta,
- ["6"] = colors.orange,
- ["7"] = colors.lightGray,
- ["8"] = colors.gray,
- ["9"] = colors.blue,
- ["a"] = colors.lime,
- ["b"] = colors.cyan,
- ["c"] = colors.red,
- ["d"] = colors.pink,
- ["e"] = colors.yellow,
- ["f"] = colors.white
- }
- --Prints the current computer's name, with corresponding colors
- --@OptionalArg string name: The name to write. If not specified, will use the computer's current label
- --@OptionalArg boolean stayOnSameLine: Whether it should print a newline character after or not
- local function writeCurrentName(name,stayOnSameLine)
- name = name or os.getComputerLabel()
- if not name then return end
- local color = string.match(name,SECTION_CHAR.."%x")
- local text = string.gsub(name,SECTION_CHAR.."%x","")
- if color then term.setTextColor(MOTD_TO_COLOR[string.lower(string.gsub(color,SECTION_CHAR,""))]) end
- term.write(text)
- term.setTextColor(colors.white)
- if not stayOnSameLine then print() end
- end
- --Generates a random name from names.json
- --@Returns string name: A random name, or "errored name"
- local function getRandomName()
- if not fs.exists("names.json") then return "errored name" end
- local file = fs.open("names.json","r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- if type(data) ~= "table" then return "errored name" end
- return data[ math.random(#data) ] or "errored name"
- end
- --Generates a new computer label from scratch, with a random name and random color
- function generateNewLabel()
- local name = getRandomName()
- local color = string.format("%x",math.random(0,15))
- os.setComputerLabel(SECTION_CHAR .. color .. name)
- writeCurrentName()
- end
- --Changes the computer's label color to to the specified color, preserving the computer's name
- --@OptionalArg string color: the color to update the computer's label to. If not specified, chooses a random color
- function changeLabelColor(color)
- local label = os.getComputerLabel()
- if not label then return generateNewLabel() end
- color = color or string.format("%x",math.random(0,15))
- color = "" .. color
- color = string.sub(color,1,1)
- str = string.gsub(label,SECTION_CHAR.."%x","")
- os.setComputerLabel(SECTION_CHAR..color..str)
- term.write("Label ")
- writeCurrentName(label,true)
- term.write(" was updated to ")
- writeCurrentName()
- end
- --Changes the label's name to the specified name, preserving the color
- --@OptionalArg string name: The new name for the computer. If not specified, chooses a random name
- function changeLabelName(name)
- local label = os.getComputerLabel()
- if not label then return generateNewLabel() end
- local color = string.match(label,SECTION_CHAR.."%x")
- name = name or getRandomName()
- if not color or #color < 1 then color = string.format("%x",math.random(0,15)) end
- os.setComputerLabel(color..name)
- term.write("Label ")
- writeCurrentName(label,true)
- term.write(" was updated to ")
- writeCurrentName()
- end
Advertisement
Add Comment
Please, Sign In to add comment