diff options
author | Nathan Adams <dinnerbone@dinnerbone.com> | 2012-03-22 22:56:36 +0000 |
---|---|---|
committer | Nathan Adams <dinnerbone@dinnerbone.com> | 2012-03-22 23:01:44 +0000 |
commit | 97ba2ff9385ec58128fd3b1e22c8ece57f89020d (patch) | |
tree | acb1fe04dc957f4bc1bcdb33b1a92467a324bf1a /src | |
parent | 13040dfd62a5c39d072dc15eb3728077c02042c1 (diff) | |
download | bukkit-97ba2ff9385ec58128fd3b1e22c8ece57f89020d.tar bukkit-97ba2ff9385ec58128fd3b1e22c8ece57f89020d.tar.gz bukkit-97ba2ff9385ec58128fd3b1e22c8ece57f89020d.tar.lz bukkit-97ba2ff9385ec58128fd3b1e22c8ece57f89020d.tar.xz bukkit-97ba2ff9385ec58128fd3b1e22c8ece57f89020d.zip |
Added isFormat, isColor and getLastColors methods to ChatColor
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/ChatColor.java | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java index 4d8ec832..e1a6d937 100644 --- a/src/main/java/org/bukkit/ChatColor.java +++ b/src/main/java/org/bukkit/ChatColor.java @@ -78,23 +78,23 @@ public enum ChatColor { /** * Represents magical characters that change around randomly */ - MAGIC('k', 0x10), + MAGIC('k', 0x10, true), /** * Makes the text bold. */ - BOLD('l', 0x11), + BOLD('l', 0x11, true), /** * Makes a line appear through the text. */ - STRIKETHROUGH('m', 0x12), + STRIKETHROUGH('m', 0x12, true), /** * Makes the text appear underlined. */ - UNDERLINE('n', 0x13), + UNDERLINE('n', 0x13, true), /** * Makes the text italic. */ - ITALIC('o', 0x14), + ITALIC('o', 0x14, true), /** * Resets all previous chat colors or formats. */ @@ -109,13 +109,19 @@ public enum ChatColor { private final int intCode; private final char code; + private final boolean isFormat; private final String toString; private final static Map<Integer, ChatColor> BY_ID = Maps.newHashMap(); private final static Map<Character, ChatColor> BY_CHAR = Maps.newHashMap(); private ChatColor(char code, int intCode) { + this(code, intCode, false); + } + + private ChatColor(char code, int intCode, boolean isFormat) { this.code = code; this.intCode = intCode; + this.isFormat = isFormat; this.toString = new String(new char[] {COLOR_CHAR, code}); } @@ -134,6 +140,20 @@ public enum ChatColor { } /** + * Checks if this code is a format code as opposed to a color code. + */ + public boolean isFormat() { + return isFormat; + } + + /** + * Checks if this code is a color code as opposed to a format code. + */ + public boolean isColor() { + return !isFormat && this != RESET; + } + + /** * Gets the color represented by the specified color code * * @param code Code to check @@ -190,6 +210,35 @@ public enum ChatColor { return new String(b); } + /** + * Gets the ChatColors used at the end of the given input string. + * + * @param input Input string to retrieve the colors from. + * @return Any remaining ChatColors to pass onto the next line. + */ + public static String getLastColors(String input) { + String result = ""; + int lastIndex = -1; + int length = input.length(); + + while ((lastIndex = input.indexOf(COLOR_CHAR, lastIndex + 1)) != -1) { + if (lastIndex != length) { + char c = input.charAt(lastIndex + 1); + ChatColor col = getByChar(c); + + if (col != null) { + if (col.isColor()) { + result = col.toString(); + } else if (col.isFormat()) { + result += col.toString(); + } + } + } + } + + return result; + } + static { for (ChatColor color : values()) { BY_ID.put(color.intCode, color); |