Skip to content

Commit f728c3e

Browse files
committed
v2.0.1 - Hex color support
1 parent 5db7fd2 commit f728c3e

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

Bukkit/src/main/java/net/frankheijden/serverutils/bukkit/entities/BukkitChatProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
44
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
55
import net.frankheijden.serverutils.common.providers.ChatProvider;
6+
import net.frankheijden.serverutils.common.utils.HexUtils;
67
import net.md_5.bungee.api.ChatColor;
78
import org.bukkit.Bukkit;
89

@@ -27,7 +28,7 @@ public ServerCommandSender getConsoleSender() {
2728
*/
2829
@Override
2930
public String color(String str) {
30-
return ChatColor.translateAlternateColorCodes('&', str);
31+
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
3132
}
3233

3334
/**

Bungee/src/main/java/net/frankheijden/serverutils/bungee/entities/BungeeChatProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
44
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
55
import net.frankheijden.serverutils.common.providers.ChatProvider;
6+
import net.frankheijden.serverutils.common.utils.HexUtils;
67
import net.md_5.bungee.api.ChatColor;
78
import net.md_5.bungee.api.ProxyServer;
89

@@ -15,7 +16,7 @@ public ServerCommandSender getConsoleSender() {
1516

1617
@Override
1718
public String color(String str) {
18-
return ChatColor.translateAlternateColorCodes('&', str);
19+
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
1920
}
2021

2122
@Override
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.frankheijden.serverutils.common.utils;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Provides basic conversion between the hex format <#FFFFFF> and bukkit hex format &x&F&F&F&F&F&F.
8+
*/
9+
public class HexUtils {
10+
11+
private static final Pattern hexPattern = Pattern.compile("<(#[0-9a-fA-F]{6})>");
12+
private static final char COLOR_CHAR = '&';
13+
14+
/**
15+
* Prefixes each character provided with the color character {@link #COLOR_CHAR}.
16+
* @param color The color to prefix with the color character.
17+
* @return The prefixed color.
18+
*/
19+
public static String convertHexColor(String color) {
20+
StringBuilder sb = new StringBuilder(2 * (color.length() + 1)).append(COLOR_CHAR).append("x");
21+
for (char c : color.toCharArray()) {
22+
sb.append(COLOR_CHAR).append(c);
23+
}
24+
return sb.toString();
25+
}
26+
27+
/**
28+
* Converts the input string to a bukkit readable color string.
29+
* The accepted hex format is `<#FFFFFF>`.
30+
* @param str The input string.
31+
* @return The output converted hex string.
32+
*/
33+
public static String convertHexString(String str) {
34+
StringBuffer sb = new StringBuffer(str.length());
35+
Matcher matcher = hexPattern.matcher(str);
36+
while (matcher.find()) {
37+
String hex = matcher.group();
38+
matcher.appendReplacement(sb, convertHexColor(hex.substring(2, hex.length() - 1)));
39+
}
40+
matcher.appendTail(sb);
41+
return sb.toString();
42+
}
43+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55

66
group = 'net.frankheijden.serverutils'
77
String dependencyDir = group + '.dependencies'
8-
version = '2.0.0'
8+
version = '2.0.1'
99

1010
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
1111

0 commit comments

Comments
 (0)