13
0
geforkt von Mirrors/Paper

Added ChatColor.MAGIC, changed to char based values and deprecated old methods. Added unit tests for ChatColor

By: Nathan Adams <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-01-16 19:38:08 +00:00
Ursprung b91d0d5a5f
Commit b59a919a08
2 geänderte Dateien mit 119 neuen und 22 gelöschten Zeilen

Datei anzeigen

@ -11,87 +11,115 @@ public enum ChatColor {
/** /**
* Represents black * Represents black
*/ */
BLACK(0x0), BLACK('0', 0x01),
/** /**
* Represents dark blue * Represents dark blue
*/ */
DARK_BLUE(0x1), DARK_BLUE('1', 0x1),
/** /**
* Represents dark green * Represents dark green
*/ */
DARK_GREEN(0x2), DARK_GREEN('2', 0x2),
/** /**
* Represents dark blue (aqua) * Represents dark blue (aqua)
*/ */
DARK_AQUA(0x3), DARK_AQUA('3', 0x3),
/** /**
* Represents dark red * Represents dark red
*/ */
DARK_RED(0x4), DARK_RED('4', 0x4),
/** /**
* Represents dark purple * Represents dark purple
*/ */
DARK_PURPLE(0x5), DARK_PURPLE('5', 0x5),
/** /**
* Represents gold * Represents gold
*/ */
GOLD(0x6), GOLD('6', 0x6),
/** /**
* Represents gray * Represents gray
*/ */
GRAY(0x7), GRAY('7', 0x7),
/** /**
* Represents dark gray * Represents dark gray
*/ */
DARK_GRAY(0x8), DARK_GRAY('8', 0x8),
/** /**
* Represents blue * Represents blue
*/ */
BLUE(0x9), BLUE('9', 0x9),
/** /**
* Represents green * Represents green
*/ */
GREEN(0xA), GREEN('a', 0xA),
/** /**
* Represents aqua * Represents aqua
*/ */
AQUA(0xB), AQUA('b', 0xB),
/** /**
* Represents red * Represents red
*/ */
RED(0xC), RED('c', 0xC),
/** /**
* Represents light purple * Represents light purple
*/ */
LIGHT_PURPLE(0xD), LIGHT_PURPLE('d', 0xD),
/** /**
* Represents yellow * Represents yellow
*/ */
YELLOW(0xE), YELLOW('e', 0xE),
/** /**
* Represents white * 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<Integer, ChatColor> colors = new HashMap<Integer, ChatColor>(); private final static Map<Integer, ChatColor> colors = new HashMap<Integer, ChatColor>();
private final static Map<Character, ChatColor> lookup = new HashMap<Character, ChatColor>();
private ChatColor(final int code) { private ChatColor(char code, int intCode) {
this.code = code; this.code = code;
this.intCode = intCode;
} }
/** /**
* Gets the data value associated with this color * Gets the data value associated with this color
* *
* @return An integer value of this color code * @return An integer value of this color code
* @deprecated Use {@link #getChar()}
*/ */
public int getCode() { 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; return code;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("\u00A7%x", code); return String.format("\u00A7%c", 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
* @deprecated Use {@link #getByChar(char)}
*/
public static ChatColor getByCode(final int code) {
return colors.get(code);
} }
/** /**
@ -100,8 +128,18 @@ public enum ChatColor {
* @param code Code to check * @param code Code to check
* @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist
*/ */
public static ChatColor getByCode(final int code) { public static ChatColor getByChar(char code) {
return colors.get(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));
} }
/** /**
@ -115,12 +153,13 @@ public enum ChatColor {
return null; return null;
} }
return input.replaceAll("(?i)\u00A7[0-9A-F]", ""); return input.replaceAll("(?i)\u00A7[0-9A-FK]", "");
} }
static { static {
for (ChatColor color : ChatColor.values()) { for (ChatColor color : ChatColor.values()) {
colors.put(color.getCode(), color); colors.put(color.getCode(), color);
lookup.put(color.getChar(), color);
} }
} }
} }

Datei anzeigen

@ -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));
}
}