Dealing with coloured console text.

Discussion in 'Plugin Development' started by avatarDr, Oct 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    avatarDr

    Does somebody know which classes from bukkit should i search for/extend/implement to make JTextPane to properly display coloured console text?
    All what I know (or think that know) is that I should add styles to StyledDocument and apply them in Handler when adding text to JTextPane. Am I right, or is there a simpler way which I don't know?
     
  2. I use this code to replace those colors to chat colors:
    long code (open)
    Code:java
    1. final static Map<String, String> replacements;
    2.  
    3. private static String replace(String input)
    4. {
    5. String output = input;
    6. for (Map.Entry<String, String> next : replacements.entrySet())
    7. {
    8. output = output.replace(next.getKey(), next.getValue());
    9. }
    10. return output;
    11. }
    12.  
    13. static
    14. {
    15. Map<String, String> replace = new HashMap<String, String>(39);
    16. replace.put("\033[30;22m", "§0");
    17. replace.put("\033[34;22m", "§1");
    18. replace.put("\033[32;22m", "§2");
    19. replace.put("\033[36;22m", "§3");
    20. replace.put("\033[31;22m", "§4");
    21. replace.put("\033[35;22m", "§5");
    22. replace.put("\033[33;22m", "§6");
    23. replace.put("\033[37;22m", "§7");
    24. replace.put("\033[30;1m", "§8");
    25. replace.put("\033[34;1m", "§9");
    26. replace.put("\033[32;1m", "§a");
    27. replace.put("\033[36;1m", "§b");
    28. replace.put("\033[31;1m", "§c");
    29. replace.put("\033[35;1m", "§d");
    30. replace.put("\033[33;1m", "§e");
    31. replace.put("\033[37;1m", "§f");
    32.  
    33. replace.put("\033[30m", "§0");
    34. replace.put("\033[32m", "§2");
    35. replace.put("\033[36m", "§3");
    36. replace.put("\033[31m", "§4");
    37. replace.put("\033[35m", "§5");
    38. replace.put("\033[33m", "§6");
    39. replace.put("\033[37m", "§7");
    40. replace.put("\033[30m", "§8");
    41. replace.put("\033[34m", "§9");
    42. replace.put("\033[32m", "§a");
    43. replace.put("\033[36m", "§b");
    44. replace.put("\033[31m", "§c");
    45. replace.put("\033[35m", "§d");
    46. replace.put("\033[33m", "§e");
    47. replace.put("\033[37m", "§f");
    48.  
    49. replace.put("\033[5m", "§k");
    50. replace.put("\033[21m", "§l");
    51. replace.put("\033[9m", "§m");
    52. replace.put("\033[4m", "§n");
    53. replace.put("\033[3m", "§o");
    54. replace.put("\033[0;39m", "§r");
    55.  
    56. replace.put("\033[0m", "§r");
    57.  
    58. replace.put("\033[m", "");
    59.  
    60. replacements = Collections.unmodifiableMap(replace);
    61. }

    and then this code to convert it to html:
    long class (open)
    Code:java
    1.  
    2. /*
    3.  * To change this template, choose Tools | Templates
    4.  * and open the template in the editor.
    5.  */
    6. package addesk.mc.console.client.connection.messages;
    7.  
    8. import java.io.CharArrayReader;
    9. import java.io.IOException;
    10. import java.io.Serializable;
    11. import java.util.ArrayDeque;
    12. import java.util.Iterator;
    13.  
    14. /**
    15.  *
    16.  * @author Fernando
    17.  */
    18. public abstract class BaseMessage implements Data, Serializable
    19. {
    20. protected static final String[] colors = new String[]
    21. {
    22. "#000000", "#0000aa", "#00aa00", "#00aaaa",
    23. "#aa0000", "#aa00aa", "#ffaa00", "#aaaaaa",
    24. "#555555", "#5555ff", "#55ff55", "#55ffff",
    25. "#ff5555", "#ff55ff", "#ffff55", "#ffffff",
    26. };
    27.  
    28. private static enum Token
    29. {
    30. MAGIC("<span style=\"background-color: black\">", "</span>"),
    31. BOLD("<b>", "</b>"),
    32. STRIKETHROUGH("<del>", "</del>"),
    33. UNDERLINE("<u>", "</u>"),
    34. ITALIC("<i>", "</i>");
    35. private final String start;
    36. private final String ending;
    37.  
    38. private Token(String start, String ending)
    39. {
    40. this.start = start;
    41. this.ending = ending;
    42. }
    43.  
    44. public String getStart()
    45. {
    46. return this.start;
    47. }
    48.  
    49. public String getEndingToken()
    50. {
    51. return this.ending;
    52. }
    53. }
    54.  
    55. public BaseMessage(String message, boolean mustBeWhite)
    56. {
    57. this.html = addHtmlColors(HtmlEscape.escapeBr(HtmlEscape.escapeSpecial(HtmlEscape.escapeTags(message.replace("&", "&amp;")))), mustBeWhite ? 15 : 0);
    58. this.message = message;
    59. }
    60.  
    61. public static String escapeHtml(String input)
    62. {
    63. return HtmlEscape.escape(input);
    64. }
    65.  
    66. protected static String addHtmlColors(String input, int defaultcolor)
    67. {
    68. try
    69. {
    70. CharArrayReader inputStream = new CharArrayReader(input.toCharArray());
    71. int ch = inputStream.read();
    72. boolean foundToken = false;
    73. int color = defaultcolor;
    74. ArrayDeque<Token> stack = new ArrayDeque<Token>();
    75. StringBuilder out = new StringBuilder().append("<font color=").append(colors[color]).append(">");
    76. while (ch != -1)
    77. {
    78. if (foundToken)
    79. {
    80. int tmp1 = Character.digit((char) ch, 16);
    81. if (tmp1 != -1)
    82. {
    83. Iterator<Token> desending = stack.descendingIterator();
    84. while (desending.hasNext())
    85. {
    86. out.append(desending.next().getEndingToken());
    87. }
    88. stack.clear();
    89. if (color != tmp1)
    90. {
    91. out.append("</font><font color=").append(colors[tmp1]).append(">");
    92. color = tmp1;
    93. }
    94. }
    95. else
    96. {
    97. if (((char) ch) == 'r')
    98. {
    99. Iterator<Token> desending = stack.descendingIterator();
    100. while (desending.hasNext())
    101. {
    102. out.append(desending.next().getEndingToken());
    103. }
    104. stack.clear();
    105. if (color != 15)
    106. {
    107. out.append("</font><font color=").append(colors[15]).append(">");
    108. color = 15;
    109. }
    110. }
    111. else if (((char) ch) == 'l')
    112. {
    113. if (!stack.contains(Token.BOLD))
    114. {
    115. stack.add(Token.BOLD);
    116. out.append(Token.BOLD.getStart());
    117. }
    118. }
    119. else if (((char) ch) == 'm')
    120. {
    121. if (!stack.contains(Token.STRIKETHROUGH))
    122. {
    123. stack.add(Token.STRIKETHROUGH);
    124. out.append(Token.STRIKETHROUGH.getStart());
    125. }
    126. }
    127. else if (((char) ch) == 'n')
    128. {
    129. if (!stack.contains(Token.UNDERLINE))
    130. {
    131. stack.add(Token.UNDERLINE);
    132. out.append(Token.UNDERLINE.getStart());
    133. }
    134. }
    135. else if (((char) ch) == 'o')
    136. {
    137. if (!stack.contains(Token.ITALIC))
    138. {
    139. stack.add(Token.ITALIC);
    140. out.append(Token.ITALIC.getStart());
    141. }
    142. }
    143. }
    144. foundToken = false;
    145. }
    146. else
    147. {
    148. if (((char) ch) == '\u00a7')
    149. {
    150. foundToken = true;
    151. }
    152. else
    153. {
    154. out.append((char) ch);
    155. }
    156. }
    157. ch = inputStream.read();
    158. }
    159. inputStream.close();
    160. if (!stack.isEmpty())
    161. {
    162. Iterator<Token> desending = stack.descendingIterator();
    163. while (desending.hasNext())
    164. {
    165. out.append(desending.next().getEndingToken());
    166. }
    167. }
    168. return out.append("</font>").toString();
    169. }
    170. catch (IOException ex)
    171. {
    172. throw new RuntimeException("The hell broke loose", ex); // stream closed before I wanted to close it is the only case of this, what coun't happend as it is single threaded
    173. }
    174. }
    175. protected final String html;
    176. protected final String message;
    177.  
    178. public String getHTML()
    179. {
    180. return this.html;
    181. }
    182.  
    183. public String getPlain()
    184. {
    185. return message;
    186. }
    187. public static void main(String[] arg)
    188. {
    189. System.out.println(new BaseMessage("§4colors!"););
    190. }
    191. }
    192.  

    and I use this class to excape html text to prevent security leaks:
    long class (open)
    Code:java
    1. /*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package addesk.mc.console.client.connection.messages;
    6.  
    7. /**
    8.  *
    9.  * @author Fernando
    10.  */
    11. public class HtmlEscape
    12. {
    13. private static char[] hex =
    14. {
    15. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    16. };
    17.  
    18. /**
    19. * Method for html escaping a String, for use in a textarea
    20. * @param original The String to escape
    21. * @return The escaped String
    22. */
    23. public static String escapeTextArea(String original)
    24. {
    25. return escapeSpecial(escapeTags(original));
    26. }
    27.  
    28. /**
    29. * Normal escape function, for Html escaping Strings
    30. * @param original The original String
    31. * @return The escape String
    32. */
    33. public static String escape(String original)
    34. {
    35. return escapeSpecial(escapeBr(escapeTags(original)));
    36. }
    37.  
    38. public static String escapeTags(String original)
    39. {
    40. if (original == null)
    41. {
    42. return "";
    43. }
    44. StringBuilder out = new StringBuilder("");
    45. char[] chars = original.toCharArray();
    46. for (int i = 0; i < chars.length; i++)
    47. {
    48. boolean found = true;
    49. switch (chars[i])
    50. {
    51. case 60:
    52. out.append("&lt;");
    53. break; //<
    54. case 62:
    55. out.append("&gt;");
    56. break; //>
    57. case 34:
    58. out.append("&quot;");
    59. break; //"
    60. default:
    61. found = false;
    62. break;
    63. }
    64. if (!found)
    65. {
    66. out.append(chars[i]);
    67. }
    68.  
    69. }
    70. return out.toString();
    71.  
    72. }
    73.  
    74. public static String escapeBr(String original)
    75. {
    76. if (original == null)
    77. {
    78. return "";
    79. }
    80. StringBuilder out = new StringBuilder("");
    81. char[] chars = original.toCharArray();
    82. for (int i = 0; i < chars.length; i++)
    83. {
    84. boolean found = true;
    85. switch (chars[i])
    86. {
    87. case '\n':
    88. out.append("<br/>");
    89. break; //newline
    90. case '\r':
    91. break;
    92. default:
    93. found = false;
    94. break;
    95. }
    96. if (!found)
    97. {
    98. out.append(chars[i]);
    99. }
    100.  
    101. }
    102. return out.toString();
    103. }
    104.  
    105. public static String escapeSpecial(String original)
    106. {
    107. if (original == null)
    108. {
    109. return "";
    110. }
    111. StringBuilder out = new StringBuilder("");
    112. char[] chars = original.toCharArray();
    113. for (int i = 0; i < chars.length; i++)
    114. {
    115. boolean found = true;
    116. switch (chars[i])
    117. {
    118. case 198:
    119. out.append("&AElig;");
    120. break; //Æ
    121. case 193:
    122. out.append("&Aacute;");
    123. break; //Á
    124. case 194:
    125. out.append("&Acirc;");
    126. break; //Â
    127. case 192:
    128. out.append("&Agrave;");
    129. break; //À
    130. case 197:
    131. out.append("&Aring;");
    132. break; //Å
    133. case 195:
    134. out.append("&Atilde;");
    135. break; //Ã
    136. case 196:
    137. out.append("&Auml;");
    138. break; //Ä
    139. case 199:
    140. out.append("&Ccedil;");
    141. break; //Ç
    142. case 208:
    143. out.append("&ETH;");
    144. break; //Ð
    145. case 201:
    146. out.append("&Eacute;");
    147. break; //É
    148. case 202:
    149. out.append("&Ecirc;");
    150. break; //Ê
    151. case 200:
    152. out.append("&Egrave;");
    153. break; //È
    154. case 203:
    155. out.append("&Euml;");
    156. break; //Ë
    157. case 205:
    158. out.append("&Iacute;");
    159. break; //Í
    160. case 206:
    161. out.append("&Icirc;");
    162. break; //Î
    163. case 204:
    164. out.append("&Igrave;");
    165. break; //Ì
    166. case 207:
    167. out.append("&Iuml;");
    168. break; //Ï
    169. case 209:
    170. out.append("&Ntilde;");
    171. break; //Ñ
    172. case 211:
    173. out.append("&Oacute;");
    174. break; //Ó
    175. case 212:
    176. out.append("&Ocirc;");
    177. break; //Ô
    178. case 210:
    179. out.append("&Ograve;");
    180. break; //Ò
    181. case 216:
    182. out.append("&Oslash;");
    183. break; //Ø
    184. case 213:
    185. out.append("&Otilde;");
    186. break; //Õ
    187. case 214:
    188. out.append("&Ouml;");
    189. break; //Ö
    190. case 222:
    191. out.append("&THORN;");
    192. break; //Þ
    193. case 218:
    194. out.append("&Uacute;");
    195. break; //Ú
    196. case 219:
    197. out.append("&Ucirc;");
    198. break; //Û
    199. case 217:
    200. out.append("&Ugrave;");
    201. break; //Ù
    202. case 220:
    203. out.append("&Uuml;");
    204. break; //Ü
    205. case 221:
    206. out.append("&Yacute;");
    207. break; //Ý
    208. case 225:
    209. out.append("&aacute;");
    210. break; //á
    211. case 226:
    212. out.append("&acirc;");
    213. break; //â
    214. case 230:
    215. out.append("&aelig;");
    216. break; //æ
    217. case 224:
    218. out.append("&agrave;");
    219. break; //à
    220. case 229:
    221. out.append("&aring;");
    222. break; //å
    223. case 227:
    224. out.append("&atilde;");
    225. break; //ã
    226. case 228:
    227. out.append("&auml;");
    228. break; //ä
    229. case 231:
    230. out.append("&ccedil;");
    231. break; //ç
    232. case 233:
    233. out.append("&eacute;");
    234. break; //é
    235. case 234:
    236. out.append("&ecirc;");
    237. break; //ê
    238. case 232:
    239. out.append("&egrave;");
    240. break; //è
    241. case 240:
    242. out.append("&eth;");
    243. break; //ð
    244. case 235:
    245. out.append("&euml;");
    246. break; //ë
    247. case 237:
    248. out.append("&iacute;");
    249. break; //í
    250. case 238:
    251. out.append("&icirc;");
    252. break; //î
    253. case 236:
    254. out.append("&igrave;");
    255. break; //ì
    256. case 239:
    257. out.append("&iuml;");
    258. break; //ï
    259. case 241:
    260. out.append("&ntilde;");
    261. break; //ñ
    262. case 243:
    263. out.append("&oacute;");
    264. break; //ó
    265. case 244:
    266. out.append("&ocirc;");
    267. break; //ô
    268. case 242:
    269. out.append("&ograve;");
    270. break; //ò
    271. case 248:
    272. out.append("&oslash;");
    273. break; //ø
    274. case 245:
    275. out.append("&otilde;");
    276. break; //õ
    277. case 246:
    278. out.append("&ouml;");
    279. break; //ö
    280. case 223:
    281. out.append("&szlig;");
    282. break; //ß
    283. case 254:
    284. out.append("&thorn;");
    285. break; //þ
    286. case 250:
    287. out.append("&uacute;");
    288. break; //ú
    289. case 251:
    290. out.append("&ucirc;");
    291. break; //û
    292. case 249:
    293. out.append("&ugrave;");
    294. break; //ù
    295. case 252:
    296. out.append("&uuml;");
    297. break; //ü
    298. case 253:
    299. out.append("&yacute;");
    300. break; //ý
    301. case 255:
    302. out.append("&yuml;");
    303. break; //ÿ
    304. case 162:
    305. out.append("&cent;");
    306. break; //¢
    307. default:
    308. found = false;
    309. break;
    310. }
    311. if (!found)
    312. {
    313. if (chars[i] == '§') // special character for colors
    314. {
    315. out.append(chars[i]);
    316. }
    317. else if (chars[i] > 127)
    318. {
    319. char c = chars[i];
    320. int a4 = c % 16;
    321. c = (char) (c / 16);
    322. int a3 = c % 16;
    323. c = (char) (c / 16);
    324. int a2 = c % 16;
    325. c = (char) (c / 16);
    326. int a1 = c % 16;
    327. out.append("&#x").append(hex[a1]).append(hex[a2]).append(hex[a3]).append(hex[a4]).append(";");
    328. }
    329. else
    330. {
    331. out.append(chars[i]);
    332. }
    333. }
    334. }
    335. return out.toString();
    336. }
    337. }
    338. [/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    after I ssee may code, it may not be as simple as I thought
     
  3. Offline

    skore87

    ferrybig Do you know what the difference between the two sets of color codes means by chance? You have listed "\033[30;22m" and "\033[30m" both being zero. Obviously they both have the same base, but what is that extra ";22m" on the first example?
     
  4. there was an bukkit update where the systax got chanced, but some server where I had this admin tool where running hte oldversion of bukkit, while others had the newest, so I left them both inside the file to make it work for both bukkit versions
     
  5. Offline

    avatarDr

    Well, the current problem of mine is that I need to make list of AttributeSet to handle colours in StyledDocument.
    Could someone explain how to do it?
    The only thing I found is from swing tutorial, and I hope it isn't the shortest way:
    Code:
    protected void addStylesToDocument(StyledDocument doc) {
            //Initialize some styles.
            Style def = StyleContext.getDefaultStyleContext().
                            getStyle(StyleContext.DEFAULT_STYLE);
     
            Style regular = doc.addStyle("regular", def);
            StyleConstants.setFontFamily(def, "SansSerif");
     
            Style s = doc.addStyle("italic", regular);
            StyleConstants.setItalic(s, true);
     
            s = doc.addStyle("bold", regular);
            StyleConstants.setBold(s, true);
     
            s = doc.addStyle("small", regular);
            StyleConstants.setFontSize(s, 10);
     
            s = doc.addStyle("large", regular);
            StyleConstants.setFontSize(s, 16);
     
            s = doc.addStyle("icon", regular);
     
  6. Offline

    zack6849

    You could also do:
    getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "This is green!");
    note: i may have misunderstood your request or something, sorry if this isnt what you were looking for.
    edit: derped a bit. fixed now.
     
  7. Offline

    avatarDr

    Yes, I could. So what will it give me?
     
Thread Status:
Not open for further replies.

Share This Page