Skip to content

Commit

Permalink
Add support for color codes in format: #123123
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed May 14, 2024
1 parent 0dedf37 commit dd56c28
Showing 1 changed file with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class LegacyToMiniMessageConverter implements MessageConverter {
private static final Pattern BUKKIT_COLOR_PATTERN = Pattern.compile("(?i)[§&][A-FK-OR\\d]");
private static final Pattern SPIGOT_HEX_COLOR_PATTERN = Pattern.compile("(?i)[§&]X([§&][A-F\\d]){6}");
private static final Pattern PAPER_HEX_COLOR_PATTERN = Pattern.compile("(?i)[§&]#([A-F\\d]){6}");
private static final Pattern SIMPLIFIED_PAPER_HEX_COLOR_PATTERN = Pattern.compile("(?i)#([A-F\\d]){6}");

private static final Pattern CHATTY_HEX_COLOR_PATTERN = Pattern.compile("(?i)\\{#([A-F\\d]{6})}");
private static final Pattern CHATTY_HEX_GRADIENT_PATTERN = Pattern.compile("(?i)\\{#([A-F\\d]{6})(:#([A-F\\d]{6}))+( )([^{}])*(})");
Expand Down Expand Up @@ -56,76 +57,90 @@ public final class LegacyToMiniMessageConverter implements MessageConverter {
convertedMessage = convertChattyHexCodes(convertedMessage);
convertedMessage = convertPaperHexCodes(convertedMessage);
convertedMessage = convertSpigotHexCodes(convertedMessage);
convertedMessage = convertSimplifiedPaperHexCodes(convertedMessage);
convertedMessage = convertBukkitCodes(convertedMessage);
return convertedMessage;
}

private @NotNull String convertPaperHexCodes(@NotNull String message) {
Matcher matcher = PAPER_HEX_COLOR_PATTERN.matcher(message);

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
String hex = matcher.group().substring(2);
matcher.appendReplacement(buffer, RESET_TAGS + "<color:#" + hex + ">");
matcher.appendReplacement(builder, RESET_TAGS + "<color:#" + hex + ">");
}
matcher.appendTail(buffer);
matcher.appendTail(builder);

return buffer.toString();
return builder.toString();
}

private @NotNull String convertSimplifiedPaperHexCodes(@NotNull String message) {
Matcher matcher = SIMPLIFIED_PAPER_HEX_COLOR_PATTERN.matcher(message);

StringBuilder builder = new StringBuilder();
while (matcher.find()) {
String hex = matcher.group().substring(1);
matcher.appendReplacement(builder, RESET_TAGS + "<color:#" + hex + ">");
}
matcher.appendTail(builder);

return builder.toString();
}

private @NotNull String convertChattyCodes(@NotNull String message) {
Matcher matcher = CHATTY_HEX_GRADIENT_PATTERN.matcher(message);

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
String group = matcher.group();
String codes = group.substring(1, group.indexOf(' '));
String text = group.substring(group.indexOf(' ') + 1, group.length() - 1);

matcher.appendReplacement(buffer, RESET_TAGS + "<gradient:" + codes + ">" + text + "</gradient>");
matcher.appendReplacement(builder, RESET_TAGS + "<gradient:" + codes + ">" + text + "</gradient>");
}
matcher.appendTail(buffer);
matcher.appendTail(builder);

return buffer.toString();
return builder.toString();
}

private @NotNull String convertChattyHexCodes(@NotNull String message) {
Matcher matcher = CHATTY_HEX_COLOR_PATTERN.matcher(message);

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(buffer, RESET_TAGS + "<color:#" + matcher.group().substring(2, 8) + ">");
matcher.appendReplacement(builder, RESET_TAGS + "<color:#" + matcher.group().substring(2, 8) + ">");
}
matcher.appendTail(buffer);
matcher.appendTail(builder);

return buffer.toString();
return builder.toString();
}

private @NotNull String convertSpigotHexCodes(@NotNull String message) {
Matcher matcher = SPIGOT_HEX_COLOR_PATTERN.matcher(message);

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
String hex = COLOR_SYMBOLS_PATTERN.matcher(matcher.group()).replaceAll("").substring(1);
matcher.appendReplacement(buffer, RESET_TAGS + "<color:#" + hex + ">");
matcher.appendReplacement(builder, RESET_TAGS + "<color:#" + hex + ">");
}
matcher.appendTail(buffer);
matcher.appendTail(builder);

return buffer.toString();
return builder.toString();
}

private @NotNull String convertBukkitCodes(@NotNull String message) {
Matcher matcher = BUKKIT_COLOR_PATTERN.matcher(message);

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
String group = matcher.group();
String replacement = legacyCodeToMiniMessageMap.get(group.charAt(1));
matcher.appendReplacement(buffer, replacement);
matcher.appendReplacement(builder, replacement);
}
matcher.appendTail(buffer);
matcher.appendTail(builder);

return buffer.toString();
return builder.toString();
}

}

0 comments on commit dd56c28

Please sign in to comment.