From ed34f17ddf0b539d33dd0bec8b055c4e154f7cfc Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 16 Jan 2012 19:38:08 +0000 Subject: Added ChatColor.MAGIC, changed to char based values and deprecated old methods. Added unit tests for ChatColor --- src/main/java/org/bukkit/ChatColor.java | 79 +++++++++++++++++++++-------- src/test/java/org/bukkit/ChatColorTest.java | 58 +++++++++++++++++++++ 2 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 src/test/java/org/bukkit/ChatColorTest.java (limited to 'src') diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java index 06540b2c..91392332 100644 --- a/src/main/java/org/bukkit/ChatColor.java +++ b/src/main/java/org/bukkit/ChatColor.java @@ -11,87 +11,104 @@ public enum ChatColor { /** * Represents black */ - BLACK(0x0), + BLACK('0', 0x01), /** * Represents dark blue */ - DARK_BLUE(0x1), + DARK_BLUE('1', 0x1), /** * Represents dark green */ - DARK_GREEN(0x2), + DARK_GREEN('2', 0x2), /** * Represents dark blue (aqua) */ - DARK_AQUA(0x3), + DARK_AQUA('3', 0x3), /** * Represents dark red */ - DARK_RED(0x4), + DARK_RED('4', 0x4), /** * Represents dark purple */ - DARK_PURPLE(0x5), + DARK_PURPLE('5', 0x5), /** * Represents gold */ - GOLD(0x6), + GOLD('6', 0x6), /** * Represents gray */ - GRAY(0x7), + GRAY('7', 0x7), /** * Represents dark gray */ - DARK_GRAY(0x8), + DARK_GRAY('8', 0x8), /** * Represents blue */ - BLUE(0x9), + BLUE('9', 0x9), /** * Represents green */ - GREEN(0xA), + GREEN('a', 0xA), /** * Represents aqua */ - AQUA(0xB), + AQUA('b', 0xB), /** * Represents red */ - RED(0xC), + RED('c', 0xC), /** * Represents light purple */ - LIGHT_PURPLE(0xD), + LIGHT_PURPLE('d', 0xD), /** * Represents yellow */ - YELLOW(0xE), + YELLOW('e', 0xE), /** * Represents white */ - WHITE(0xF); + WHITE('f', 0xF), + /** + * Represents magical characters that change around randomly + */ + MAGIC('k', 0x10); - private final int code; + private final int intCode; + private final char code; private final static Map colors = new HashMap(); + private final static Map lookup = new HashMap(); - private ChatColor(final int code) { + private ChatColor(char code, int intCode) { this.code = code; + this.intCode = intCode; } /** * Gets the data value associated with this color * * @return An integer value of this color code + * @deprecated Use {@link #getChar()} */ public int getCode() { + return intCode; + } + + /** + * Gets the char value associated with this color + * + * @return A char value of this color code + */ + public char getChar() { return code; } @Override public String toString() { - return String.format("\u00A7%x", code); + return String.format("\u00A7%c", code); } /** @@ -99,11 +116,32 @@ public enum ChatColor { * * @param code Code to check * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist + * @deprecated Use {@link #getByChar(char)} */ public static ChatColor getByCode(final int code) { return colors.get(code); } + /** + * Gets the color represented by the specified color code + * + * @param code Code to check + * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist + */ + public static ChatColor getByChar(char code) { + return lookup.get(code); + } + + /** + * Gets the color represented by the specified color code + * + * @param code Code to check + * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist + */ + public static ChatColor getByChar(String code) { + return lookup.get(code.charAt(0)); + } + /** * Strips the given message of all color codes * @@ -115,12 +153,13 @@ public enum ChatColor { return null; } - return input.replaceAll("(?i)\u00A7[0-9A-F]", ""); + return input.replaceAll("(?i)\u00A7[0-9A-FK]", ""); } static { for (ChatColor color : ChatColor.values()) { colors.put(color.getCode(), color); + lookup.put(color.getChar(), color); } } } diff --git a/src/test/java/org/bukkit/ChatColorTest.java b/src/test/java/org/bukkit/ChatColorTest.java new file mode 100644 index 00000000..acf7c5e9 --- /dev/null +++ b/src/test/java/org/bukkit/ChatColorTest.java @@ -0,0 +1,58 @@ +package org.bukkit; + +import org.junit.AfterClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.junit.BeforeClass; +import static org.hamcrest.CoreMatchers.*; + +public class ChatColorTest { + @Test + public void testGetCode() { + ChatColor color = ChatColor.DARK_RED; + assertThat(color.getCode(), equalTo(4)); + } + + @Test + public void testGetChar() { + ChatColor color = ChatColor.MAGIC; + assertThat(color.getChar(), equalTo('k')); + } + + @Test + public void testToString() { + ChatColor color = ChatColor.LIGHT_PURPLE; + assertThat(color.toString(), equalTo("\u00A7d")); + } + + @Test + public void testGetByCode() { + ChatColor color = ChatColor.AQUA; + assertThat(ChatColor.getByCode(color.getCode()), equalTo(color)); + } + + @Test + public void testGetByChar_char() { + ChatColor color = ChatColor.GOLD; + assertThat(ChatColor.getByChar(color.getChar()), equalTo(color)); + } + + @Test + public void testGetByChar_String() { + ChatColor color = ChatColor.BLUE; + assertThat(ChatColor.getByChar(((Character)color.getChar()).toString()), equalTo(color)); + } + + @Test + public void testStripColor() { + String string = ""; + String expected = ""; + + for (ChatColor color : ChatColor.values()) { + string += color + "test"; + expected += "test"; + } + + assertThat(ChatColor.stripColor(string), equalTo(expected)); + } +} -- cgit v1.2.3