Skip to content

Commit

Permalink
text formatter: strip redundant &r's
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 13, 2022
1 parent dde4a58 commit 32dcc80
Showing 1 changed file with 14 additions and 8 deletions.
Expand Up @@ -75,10 +75,14 @@ public static String stringify(BaseComponent[] components, ChatColor baseColor)
for (BaseComponent component : components) {
builder.append(stringify(component));
}
while (builder.toString().endsWith(RESET)) {
builder.setLength(builder.length() - RESET.length());
String output = builder.toString();
while (output.endsWith(RESET)) {
output = output.substring(0, output.length() - RESET.length());
}
while (output.startsWith(POSSIBLE_RESET_PREFIX) && output.length() > 4 && colorCodeInvalidator.isMatch(output.charAt(3))) {
output = output.substring(2);
}
return builder.toString();
return output;
}

public static boolean boolNotNull(Boolean bool) {
Expand Down Expand Up @@ -185,7 +189,7 @@ else if (component instanceof ScoreComponent) {
return cleanRedundantCodes(output);
}

public static final String RESET = ChatColor.RESET.toString();
public static final String RESET = ChatColor.RESET.toString(), POSSIBLE_RESET_PREFIX = RESET + ChatColor.COLOR_CHAR;

public static TextComponent copyFormatToNewText(TextComponent last) {
TextComponent toRet = new TextComponent();
Expand Down Expand Up @@ -232,13 +236,15 @@ public static int findEndIndexFor(String base, String type, int startAt) {
return findEndIndexFor(base, "[" + type + "=", "[/" + type + "]", startAt);
}

public static AsciiMatcher allowedCharCodes = new AsciiMatcher("0123456789abcdefABCDEFklmnorxKLMNORX[");
public static String HEX = "0123456789abcdefABCDEF";

public static AsciiMatcher allowedCharCodes = new AsciiMatcher(HEX + "klmnorxKLMNORX[");

public static AsciiMatcher hexMatcher = new AsciiMatcher("0123456789abcdefABCDEF");
public static AsciiMatcher hexMatcher = new AsciiMatcher(HEX);

public static AsciiMatcher colorCodesOrReset = new AsciiMatcher("0123456789abcdefABCDEFrR"); // Any color code that can be invalidated
public static AsciiMatcher colorCodesOrReset = new AsciiMatcher(HEX + "rR"); // Any color code that can be invalidated

public static AsciiMatcher colorCodeInvalidator = new AsciiMatcher("0123456789abcdefABCDEFrRxX"); // Any code that can invalidate the colors above
public static AsciiMatcher colorCodeInvalidator = new AsciiMatcher(HEX + "rRxX"); // Any code that can invalidate the colors above

public static String cleanRedundantCodes(String str) {
int index = str.indexOf(ChatColor.COLOR_CHAR);
Expand Down

0 comments on commit 32dcc80

Please sign in to comment.