Skip to content

Instantly share code, notes, and snippets.

@TehBrian
Last active September 23, 2022 19:59
Show Gist options
  • Save TehBrian/8c7df397c37081e8ceb6d279275a4aba to your computer and use it in GitHub Desktop.
Save TehBrian/8c7df397c37081e8ceb6d279275a4aba to your computer and use it in GitHub Desktop.
/* Prefer using adventure or something, but if you need something tiny.. */
public static final Pattern HEX_PATTERN = Pattern.compile("&(#[A-Fa-f0-9]{6})");
public static String color(String string) {
return string == null ? null : replaceHex(ChatColor.translateAlternateColorCodes('&', string));
}
public static String replaceHex(String str) {
Matcher matcher = HEX_PATTERN.matcher(str);
while (matcher.find()) {
str = str.replace(matcher.group(0), ChatColor.of(matcher.group(1)).toString());
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment