mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-05 18:27:17 +01:00
Added ChatColor.MAGIC, changed to char based values and deprecated old methods. Added unit tests for ChatColor
By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
parent
b91d0d5a5f
commit
b59a919a08
2 changed files with 119 additions and 22 deletions
|
@ -11,87 +11,115 @@ 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<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.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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist
|
||||
*/
|
||||
public static ChatColor getByCode(final int code) {
|
||||
return colors.get(code);
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
58
paper-api/src/test/java/org/bukkit/ChatColorTest.java
Normal file
58
paper-api/src/test/java/org/bukkit/ChatColorTest.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue