Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 7pLzbuZp colcode
- -- main intended change: optimize JSON output
- local colors_names = { --for use with colors api, you see.
- ["0"] = colors.black,
- ["1"] = colors.blue,
- ["2"] = colors.green,
- ["3"] = colors.cyan,
- ["4"] = colors.red,
- ["5"] = colors.purple,
- ["6"] = colors.orange,
- ["7"] = colors.lightGray,
- ["8"] = colors.gray,
- ["9"] = colors.blue, --they don't translate perfectly, okay??
- ["a"] = colors.lime,
- ["b"] = colors.lightBlue,
- ["c"] = colors.red, --two reds?? but the colors API has as many colors!
- ["d"] = colors.magenta,
- ["e"] = colors.yellow,
- ["f"] = colors.white,
- }
- local codeNames = { --just for checking, not for any translation
- ["k"] = "obfuscate",
- ["o"] = "italic",
- ["l"] = "bold",
- ["m"] = "strikethrough",
- ["n"] = "underline",
- ["r"] = "reset",
- }
- cleanColorFormat = function(i)
- local color = "white"
- local obfuscated = false
- local bold = false
- local strikethrough = false
- local underline = false
- local italic = false
- local text = ""
- local output = {}
- local changed = false
- local opos = 1
- for a = 1, #i do
- if not output[opos] then output[opos] = {} end
- if i.color ~= color then
- changed = true
- color = i.color
- output[opos].color = color
- end
- if i.obfuscated ~= obfuscated then
- changed = true
- obfuscated = i.obfuscated
- output[opos].obfuscated = obfuscated
- end
- if i.bold ~= bold then
- changed = true
- bold = i.bold
- output[opos].bold = bold
- end
- if i.strikethrough ~= strikethrough then
- changed = true
- strikethrough = i.strikethrough
- output[opos].strikethrough = strikethrough
- end
- if i.underline ~= underline then
- changed = true
- underline = i.underline
- output[opos].underline = underline
- end
- if i.italic ~= italic then
- changed = true
- italic = i.italic
- output[opos].italic = italic
- end
- if changed then
- text = ""
- opos = opos + 1
- if not output[opos] then output[opos] = {} end
- changed = false
- end
- text = text..i[a].text
- output[opos].text = text
- end
- -- if text ~= "" then
- -- output[opos].text = text
- -- end
- -- for a = #output, 1, -1 do
- -- if #output[a] == 0 then
- -- table.remove(output,a)
- -- end
- -- end
- return output
- end
- filterColors = function(str,doprint) --takes all color codes out of a string when appropriate. having second argument true makes it write!
- local p = 1
- local output = ""
- local code = "&"
- local col = "f"
- while p <= #str do
- if str:sub(p,p) == code then
- if colors_names[str:sub(p+1,p+1)] then
- col = str:sub(p+1,p+1)
- p = p + 1
- elseif codeNames[str:sub(p+1,p+1)] then
- if str:sub(p+1,p+1) == "r" then
- col = "f"
- end
- p = p + 1
- else
- if doprint then
- term.setTextColor(colors_names[col])
- write(str:sub(p,p))
- end
- end
- p = p + 1
- else
- output = output..str:sub(p,p)
- if doprint then
- term.setTextColor(colors_names[col])
- write(str:sub(p,p))
- end
- p = p + 1
- end
- end
- return output
- end
- local colnames = {
- ["0"] = "black",
- ["1"] = "dark_blue",
- ["2"] = "dark_green",
- ["3"] = "dark_aqua",
- ["4"] = "dark_red",
- ["5"] = "dark_purple",
- ["6"] = "gold",
- ["7"] = "gray",
- ["8"] = "dark_gray",
- ["9"] = "blue",
- ["a"] = "green",
- ["b"] = "aqua",
- ["c"] = "red",
- ["d"] = "light_purple",
- ["e"] = "yellow",
- ["f"] = "white",
- }
- colorFormat = function(str,returnTable) --returns a LARGE table in JSON format for use with commands.tellraw()
- local color = false
- local obfuscated = false
- local bold = false
- local strikethrough = false
- local underline = false
- local italic = false
- local code = "&" --ONE CHARACTER
- local pos = 1
- local opos = 1
- local output = {}
- while pos <= #str do
- output[opos] = {}
- if str:sub(pos,pos) == code and pos < #str then
- local changed = false
- if colnames[str:sub(pos+1,pos+1)] then
- color = str:sub(pos+1,pos+1)
- changed = true
- else
- if str:sub(pos+1,pos+1) == "r" then
- color = false
- obfuscated = false
- bold = false
- strikethrough = false
- underline = false
- italic = false
- changed = true
- end
- if str:sub(pos+1,pos+1) == "k" then
- obfuscated = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "l" then
- bold = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "m" then
- strikethrough = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "n" then
- underline = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "o" then
- italic = true
- changed = true
- end
- end
- if changed then
- output[opos].text = ""
- pos = pos + 2
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- output[opos].color = colnames[color or "f"]
- output[opos].obfuscated = obfuscated
- output[opos].bold = bold
- output[opos].strikethrough = strikethrough
- output[opos].underline = underline
- output[opos].italic = italic
- opos = opos + 1
- end
- if returnTable then
- return output
- else
- return textutils.serialiseJSON(output)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement