Advertisement
LDDestroier

CC JSON ColorCode API (Beta)

Sep 15th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- pastebin get 7pLzbuZp colcode
  2. -- main intended change: optimize JSON output
  3.  
  4. local colors_names = { --for use with colors api, you see.
  5.     ["0"] = colors.black,
  6.     ["1"] = colors.blue,
  7.     ["2"] = colors.green,
  8.     ["3"] = colors.cyan,
  9.     ["4"] = colors.red,
  10.     ["5"] = colors.purple,
  11.     ["6"] = colors.orange,
  12.     ["7"] = colors.lightGray,
  13.     ["8"] = colors.gray,
  14.     ["9"] = colors.blue, --they don't translate perfectly, okay??
  15.     ["a"] = colors.lime,
  16.     ["b"] = colors.lightBlue,
  17.     ["c"] = colors.red, --two reds?? but the colors API has as many colors!
  18.     ["d"] = colors.magenta,
  19.     ["e"] = colors.yellow,
  20.     ["f"] = colors.white,
  21. }
  22.  
  23. local codeNames = { --just for checking, not for any translation
  24.     ["k"] = "obfuscate",   
  25.     ["o"] = "italic",
  26.     ["l"] = "bold",
  27.     ["m"] = "strikethrough",
  28.     ["n"] = "underline",
  29.     ["r"] = "reset",
  30. }
  31.  
  32. cleanColorFormat = function(i)
  33.     local color = "white"
  34.     local obfuscated = false
  35.     local bold = false
  36.     local strikethrough = false
  37.     local underline = false
  38.     local italic = false
  39.    
  40.     local text = ""
  41.    
  42.     local output = {}
  43.     local changed = false
  44.     local opos = 1
  45.    
  46.     for a = 1, #i do
  47.         if not output[opos] then output[opos] = {} end
  48.         if i.color ~= color then
  49.             changed = true
  50.             color = i.color
  51.             output[opos].color = color
  52.         end
  53.         if i.obfuscated ~= obfuscated then
  54.             changed = true
  55.             obfuscated = i.obfuscated
  56.             output[opos].obfuscated = obfuscated
  57.         end
  58.         if i.bold ~= bold then
  59.             changed = true
  60.             bold = i.bold
  61.             output[opos].bold = bold
  62.         end
  63.         if i.strikethrough ~= strikethrough then
  64.             changed = true
  65.             strikethrough = i.strikethrough
  66.             output[opos].strikethrough = strikethrough
  67.         end
  68.         if i.underline ~= underline then
  69.             changed = true
  70.             underline = i.underline
  71.             output[opos].underline = underline
  72.         end
  73.         if i.italic ~= italic then
  74.             changed = true
  75.             italic = i.italic
  76.             output[opos].italic = italic
  77.         end
  78.        
  79.         if changed then
  80.             text = ""
  81.             opos = opos + 1
  82.             if not output[opos] then output[opos] = {} end
  83.             changed = false
  84.         end
  85.         text = text..i[a].text
  86.         output[opos].text = text
  87.     end
  88. --  if text ~= "" then
  89. --      output[opos].text = text
  90. --  end
  91. --  for a = #output, 1, -1 do
  92. --      if #output[a] == 0 then
  93. --          table.remove(output,a)
  94. --      end
  95. --  end
  96.     return output
  97. end
  98.  
  99. filterColors = function(str,doprint) --takes all color codes out of a string when appropriate. having second argument true makes it write!
  100.     local p = 1
  101.     local output = ""
  102.     local code = "&"
  103.     local col = "f"
  104.     while p <= #str do
  105.         if str:sub(p,p) == code then
  106.             if colors_names[str:sub(p+1,p+1)] then
  107.                 col = str:sub(p+1,p+1)
  108.                 p = p + 1
  109.             elseif codeNames[str:sub(p+1,p+1)] then
  110.                 if str:sub(p+1,p+1) == "r" then
  111.                     col = "f"
  112.                 end
  113.                 p = p + 1
  114.             else
  115.                 if doprint then
  116.                     term.setTextColor(colors_names[col])
  117.                     write(str:sub(p,p))
  118.                 end
  119.             end
  120.             p = p + 1
  121.         else
  122.             output = output..str:sub(p,p)
  123.             if doprint then
  124.                 term.setTextColor(colors_names[col])
  125.                 write(str:sub(p,p))
  126.             end
  127.             p = p + 1
  128.         end
  129.     end
  130.     return output
  131. end
  132.  
  133. local colnames = {
  134.     ["0"] = "black",
  135.     ["1"] = "dark_blue",
  136.     ["2"] = "dark_green",
  137.     ["3"] = "dark_aqua",
  138.     ["4"] = "dark_red",
  139.     ["5"] = "dark_purple",
  140.     ["6"] = "gold",
  141.     ["7"] = "gray",
  142.     ["8"] = "dark_gray",
  143.     ["9"] = "blue",
  144.     ["a"] = "green",
  145.     ["b"] = "aqua",
  146.     ["c"] = "red",
  147.     ["d"] = "light_purple",
  148.     ["e"] = "yellow",
  149.     ["f"] = "white",
  150. }
  151.  
  152. colorFormat = function(str,returnTable) --returns a LARGE table in JSON format for use with commands.tellraw()
  153.     local color = false
  154.     local obfuscated = false
  155.     local bold = false
  156.     local strikethrough = false
  157.     local underline = false
  158.     local italic = false
  159.    
  160.     local code = "&" --ONE CHARACTER
  161.     local pos = 1
  162.     local opos = 1
  163.     local output = {}
  164.    
  165.     while pos <= #str do
  166.         output[opos] = {}
  167.         if str:sub(pos,pos) == code and pos < #str then
  168.             local changed = false
  169.             if colnames[str:sub(pos+1,pos+1)] then
  170.                 color = str:sub(pos+1,pos+1)
  171.                 changed = true
  172.             else
  173.                 if str:sub(pos+1,pos+1) == "r" then
  174.                     color = false
  175.                     obfuscated = false
  176.                     bold = false
  177.                     strikethrough = false
  178.                     underline = false
  179.                     italic = false
  180.                     changed = true
  181.                 end
  182.                 if str:sub(pos+1,pos+1) == "k" then
  183.                     obfuscated = true
  184.                     changed = true
  185.                 end
  186.                 if str:sub(pos+1,pos+1) == "l" then
  187.                     bold = true
  188.                     changed = true
  189.                 end
  190.                 if str:sub(pos+1,pos+1) == "m" then
  191.                     strikethrough = true
  192.                     changed = true
  193.                 end
  194.                 if str:sub(pos+1,pos+1) == "n" then
  195.                     underline = true
  196.                     changed = true
  197.                 end
  198.                 if str:sub(pos+1,pos+1) == "o" then
  199.                     italic = true
  200.                     changed = true
  201.                 end
  202.             end
  203.             if changed then
  204.                 output[opos].text = ""
  205.                 pos = pos + 2
  206.             else
  207.                 output[opos].text = str:sub(pos,pos)
  208.                 pos = pos + 1
  209.             end
  210.         else
  211.             output[opos].text = str:sub(pos,pos)
  212.             pos = pos + 1
  213.         end
  214.         output[opos].color = colnames[color or "f"]
  215.         output[opos].obfuscated = obfuscated
  216.         output[opos].bold = bold
  217.         output[opos].strikethrough = strikethrough
  218.         output[opos].underline = underline
  219.         output[opos].italic = italic
  220.         opos = opos + 1
  221.     end
  222.     if returnTable then
  223.         return output
  224.     else
  225.         return textutils.serialiseJSON(output)
  226.     end
  227. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement