From 9838a09a8cdc94baf950315133e24c8c18e3bb86 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 21 Mar 2013 21:29:26 +1100 Subject: [PATCH] Add slightly modified version of #220 - ChatColor.getByChar --- .../java/net/md_5/bungee/api/ChatColor.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/api/src/main/java/net/md_5/bungee/api/ChatColor.java b/api/src/main/java/net/md_5/bungee/api/ChatColor.java index 5a0246e5c..0df1cd5e2 100644 --- a/api/src/main/java/net/md_5/bungee/api/ChatColor.java +++ b/api/src/main/java/net/md_5/bungee/api/ChatColor.java @@ -1,5 +1,7 @@ package net.md_5.bungee.api; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Pattern; /** @@ -105,13 +107,30 @@ public enum ChatColor * Pattern to remove all colour codes. */ private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile( "(?i)" + String.valueOf( COLOR_CHAR ) + "[0-9A-FK-OR]" ); + /** + * Colour instances keyed by their active character. + */ + private static final Map BY_CHAR = new HashMap<>(); + /** + * The code appended to {@link #COLOR_CHAR} to make usable colour. + */ + private final char code; /** * This colour's colour char prefixed by the {@link #COLOR_CHAR}. */ private final String toString; + static + { + for ( ChatColor colour : values() ) + { + BY_CHAR.put( colour.code, colour ); + } + } + private ChatColor(char code) { + this.code = code; this.toString = new String( new char[] { COLOR_CHAR, code @@ -153,4 +172,15 @@ public enum ChatColor } return new String( b ); } + + /** + * Get the colour represented by the specified code. + * + * @param code the code to search for + * @return the mapped colour, or null if non exists + */ + public static ChatColor getByChar(char code) + { + return BY_CHAR.get( code ); + } }