Skip to content

Commit

Permalink
feat(MinecraftUtils): HEX Colors
Browse files Browse the repository at this point in the history
Added HEX color support
  • Loading branch information
GeorgeV220 committed Oct 14, 2022
1 parent 2db85c3 commit ff59b30
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/georgev22/api/minecraft/MinecraftUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.lang.reflect.Field;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class MinecraftUtils {
Expand Down Expand Up @@ -134,8 +136,24 @@ public static void msg(final CommandSender target, final List<String> message) {
* @return A translated message
*/
public static @NotNull String colorize(final String msg) {
Validate.notNull(msg, "The string can't be null!");
return ChatColor.translateAlternateColorCodes('&', msg);
String unEditedMessage = msg;
Validate.notNull(unEditedMessage, "The string can't be null!");
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
Matcher matcher = pattern.matcher(unEditedMessage);
while (matcher.find()) {
String hexCode = unEditedMessage.substring(matcher.start(), matcher.end());
String replaceSharp = hexCode.replace('#', 'x');

char[] ch = replaceSharp.toCharArray();
StringBuilder builder = new StringBuilder();
for (char c : ch) {
builder.append("&").append(c);
}

unEditedMessage = unEditedMessage.replace(hexCode, builder.toString());
matcher = pattern.matcher(unEditedMessage);
}
return ChatColor.translateAlternateColorCodes('&', unEditedMessage);
}

public static String stripColor(final String msg) {
Expand Down

0 comments on commit ff59b30

Please sign in to comment.