Skip to content

Commit

Permalink
v2.0.1 - Hex color support
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHeijden committed Jul 5, 2020
1 parent 5db7fd2 commit f728c3e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.providers.ChatProvider;
import net.frankheijden.serverutils.common.utils.HexUtils;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;

Expand All @@ -27,7 +28,7 @@ public ServerCommandSender getConsoleSender() {
*/
@Override
public String color(String str) {
return ChatColor.translateAlternateColorCodes('&', str);
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.providers.ChatProvider;
import net.frankheijden.serverutils.common.utils.HexUtils;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;

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

@Override
public String color(String str) {
return ChatColor.translateAlternateColorCodes('&', str);
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.frankheijden.serverutils.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Provides basic conversion between the hex format <#FFFFFF> and bukkit hex format &x&F&F&F&F&F&F.
*/
public class HexUtils {

private static final Pattern hexPattern = Pattern.compile("<(#[0-9a-fA-F]{6})>");
private static final char COLOR_CHAR = '&';

/**
* Prefixes each character provided with the color character {@link #COLOR_CHAR}.
* @param color The color to prefix with the color character.
* @return The prefixed color.
*/
public static String convertHexColor(String color) {
StringBuilder sb = new StringBuilder(2 * (color.length() + 1)).append(COLOR_CHAR).append("x");
for (char c : color.toCharArray()) {
sb.append(COLOR_CHAR).append(c);
}
return sb.toString();
}

/**
* Converts the input string to a bukkit readable color string.
* The accepted hex format is `<#FFFFFF>`.
* @param str The input string.
* @return The output converted hex string.
*/
public static String convertHexString(String str) {
StringBuffer sb = new StringBuffer(str.length());
Matcher matcher = hexPattern.matcher(str);
while (matcher.find()) {
String hex = matcher.group();
matcher.appendReplacement(sb, convertHexColor(hex.substring(2, hex.length() - 1)));
}
matcher.appendTail(sb);
return sb.toString();
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

group = 'net.frankheijden.serverutils'
String dependencyDir = group + '.dependencies'
version = '2.0.0'
version = '2.0.1'

sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8

Expand Down

0 comments on commit f728c3e

Please sign in to comment.