Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ChatColor.java
1 package org.bukkit;
2 
3 import java.util.Map;
4 import java.util.regex.Pattern;
5 
6 import org.apache.commons.lang.Validate;
7 
8 import com.google.common.collect.Maps;
9 
10 /**
11  * All supported color values for chat
12  */
13 public enum ChatColor {
14  /**
15  * Represents black
16  */
17  BLACK('0', 0x00),
18  /**
19  * Represents dark blue
20  */
21  DARK_BLUE('1', 0x1),
22  /**
23  * Represents dark green
24  */
25  DARK_GREEN('2', 0x2),
26  /**
27  * Represents dark blue (aqua)
28  */
29  DARK_AQUA('3', 0x3),
30  /**
31  * Represents dark red
32  */
33  DARK_RED('4', 0x4),
34  /**
35  * Represents dark purple
36  */
37  DARK_PURPLE('5', 0x5),
38  /**
39  * Represents gold
40  */
41  GOLD('6', 0x6),
42  /**
43  * Represents gray
44  */
45  GRAY('7', 0x7),
46  /**
47  * Represents dark gray
48  */
49  DARK_GRAY('8', 0x8),
50  /**
51  * Represents blue
52  */
53  BLUE('9', 0x9),
54  /**
55  * Represents green
56  */
57  GREEN('a', 0xA),
58  /**
59  * Represents aqua
60  */
61  AQUA('b', 0xB),
62  /**
63  * Represents red
64  */
65  RED('c', 0xC),
66  /**
67  * Represents light purple
68  */
69  LIGHT_PURPLE('d', 0xD),
70  /**
71  * Represents yellow
72  */
73  YELLOW('e', 0xE),
74  /**
75  * Represents white
76  */
77  WHITE('f', 0xF),
78  /**
79  * Represents magical characters that change around randomly
80  */
81  MAGIC('k', 0x10, true),
82  /**
83  * Makes the text bold.
84  */
85  BOLD('l', 0x11, true),
86  /**
87  * Makes a line appear through the text.
88  */
89  STRIKETHROUGH('m', 0x12, true),
90  /**
91  * Makes the text appear underlined.
92  */
93  UNDERLINE('n', 0x13, true),
94  /**
95  * Makes the text italic.
96  */
97  ITALIC('o', 0x14, true),
98  /**
99  * Resets all previous chat colors or formats.
100  */
101  RESET('r', 0x15);
102 
103  /**
104  * The special character which prefixes all chat colour codes. Use this if
105  * you need to dynamically convert colour codes from your custom format.
106  */
107  public static final char COLOR_CHAR = '\u00A7';
108  private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK-OR]");
109 
110  private final int intCode;
111  private final char code;
112  private final boolean isFormat;
113  private final String toString;
114  private final static Map<Integer, ChatColor> BY_ID = Maps.newHashMap();
115  private final static Map<Character, ChatColor> BY_CHAR = Maps.newHashMap();
116 
117  private ChatColor(char code, int intCode) {
118  this(code, intCode, false);
119  }
120 
121  private ChatColor(char code, int intCode, boolean isFormat) {
122  this.code = code;
123  this.intCode = intCode;
124  this.isFormat = isFormat;
125  this.toString = new String(new char[] {COLOR_CHAR, code});
126  }
127 
128  /**
129  * Gets the char value associated with this color
130  *
131  * @return A char value of this color code
132  */
133  public char getChar() {
134  return code;
135  }
136 
137  @Override
138  public String toString() {
139  return toString;
140  }
141 
142  /**
143  * Checks if this code is a format code as opposed to a color code.
144  */
145  public boolean isFormat() {
146  return isFormat;
147  }
148 
149  /**
150  * Checks if this code is a color code as opposed to a format code.
151  */
152  public boolean isColor() {
153  return !isFormat && this != RESET;
154  }
155 
156  /**
157  * Gets the color represented by the specified color code
158  *
159  * @param code Code to check
160  * @return Associative {@link org.bukkit.ChatColor} with the given code,
161  * or null if it doesn't exist
162  */
163  public static ChatColor getByChar(char code) {
164  return BY_CHAR.get(code);
165  }
166 
167  /**
168  * Gets the color represented by the specified color code
169  *
170  * @param code Code to check
171  * @return Associative {@link org.bukkit.ChatColor} with the given code,
172  * or null if it doesn't exist
173  */
174  public static ChatColor getByChar(String code) {
175  Validate.notNull(code, "Code cannot be null");
176  Validate.isTrue(code.length() > 0, "Code must have at least one char");
177 
178  return BY_CHAR.get(code.charAt(0));
179  }
180 
181  /**
182  * Strips the given message of all color codes
183  *
184  * @param input String to strip of color
185  * @return A copy of the input string, without any coloring
186  */
187  public static String stripColor(final String input) {
188  if (input == null) {
189  return null;
190  }
191 
192  return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
193  }
194 
195  /**
196  * Translates a string using an alternate color code character into a
197  * string that uses the internal ChatColor.COLOR_CODE color code
198  * character. The alternate color code character will only be replaced if
199  * it is immediately followed by 0-9, A-F, a-f, K-O, k-o, R or r.
200  *
201  * @param altColorChar The alternate color code character to replace. Ex: &
202  * @param textToTranslate Text containing the alternate color code character.
203  * @return Text containing the ChatColor.COLOR_CODE color code character.
204  */
205  public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
206  char[] b = textToTranslate.toCharArray();
207  for (int i = 0; i < b.length - 1; i++) {
208  if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i+1]) > -1) {
209  b[i] = ChatColor.COLOR_CHAR;
210  b[i+1] = Character.toLowerCase(b[i+1]);
211  }
212  }
213  return new String(b);
214  }
215 
216  /**
217  * Gets the ChatColors used at the end of the given input string.
218  *
219  * @param input Input string to retrieve the colors from.
220  * @return Any remaining ChatColors to pass onto the next line.
221  */
222  public static String getLastColors(String input) {
223  String result = "";
224  int length = input.length();
225 
226  // Search backwards from the end as it is faster
227  for (int index = length - 1; index > -1; index--) {
228  char section = input.charAt(index);
229  if (section == COLOR_CHAR && index < length - 1) {
230  char c = input.charAt(index + 1);
231  ChatColor color = getByChar(c);
232 
233  if (color != null) {
234  result = color.toString() + result;
235 
236  // Once we find a color or reset we can stop searching
237  if (color.isColor() || color.equals(RESET)) {
238  break;
239  }
240  }
241  }
242  }
243 
244  return result;
245  }
246 
247  static {
248  for (ChatColor color : values()) {
249  BY_ID.put(color.intCode, color);
250  BY_CHAR.put(color.code, color);
251  }
252  }
253 }
static ChatColor getByChar(char code)
Definition: ChatColor.java:163
static String getLastColors(String input)
Definition: ChatColor.java:222
static ChatColor getByChar(String code)
Definition: ChatColor.java:174
static String stripColor(final String input)
Definition: ChatColor.java:187
static String translateAlternateColorCodes(char altColorChar, String textToTranslate)
Definition: ChatColor.java:205
staticfinal char COLOR_CHAR
Definition: ChatColor.java:107