v2.0.1 - Hex color support
This commit is contained in:
parent
5db7fd2ba3
commit
f728c3e6bb
4 changed files with 48 additions and 3 deletions
|
|
@ -3,6 +3,7 @@ package net.frankheijden.serverutils.bukkit.entities;
|
|||
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;
|
||||
|
||||
|
|
@ -27,7 +28,7 @@ public class BukkitChatProvider extends ChatProvider {
|
|||
*/
|
||||
@Override
|
||||
public String color(String str) {
|
||||
return ChatColor.translateAlternateColorCodes('&', str);
|
||||
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package net.frankheijden.serverutils.bungee.entities;
|
|||
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;
|
||||
|
||||
|
|
@ -15,7 +16,7 @@ public class BungeeChatProvider extends ChatProvider {
|
|||
|
||||
@Override
|
||||
public String color(String str) {
|
||||
return ChatColor.translateAlternateColorCodes('&', str);
|
||||
return ChatColor.translateAlternateColorCodes('&', HexUtils.convertHexString(str));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue