From 693cfe0f4291cfec216540f596ca1c7803dc3fa8 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:27:28 +0700 Subject: [PATCH] fix: reset on end parse (ComponentUtilities.ComponentParser) --- build-number.txt | 2 +- .../chomens_bot/util/ComponentUtilities.java | 23 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/build-number.txt b/build-number.txt index 6efa8688..c650dc11 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1420 \ No newline at end of file +1421 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java b/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java index 8dd5ec3d..75a50d62 100644 --- a/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java +++ b/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java @@ -57,7 +57,7 @@ public class ComponentUtilities { private static class ComponentParser { public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])"); - public static final int MAX_DEPTH = 16; + public static final int MAX_DEPTH = 12; public static final Map ansiMap = new HashMap<>(); static { @@ -87,14 +87,16 @@ public class ComponentUtilities { } private ParseType type; - private int formatsPlaceholdersCount = 0; + private int depth = 0; private String lastStyle = ""; + private boolean isSubParsing = false; + private String stringify (Component message, ParseType type) { this.type = type; - if (formatsPlaceholdersCount > MAX_DEPTH) return ""; + if (depth > MAX_DEPTH) return ""; try { final StringBuilder builder = new StringBuilder(); @@ -109,10 +111,18 @@ public class ComponentUtilities { for (Component child : message.children()) { final ComponentParser parser = new ComponentParser(); parser.lastStyle = lastStyle + color + style; - parser.formatsPlaceholdersCount = formatsPlaceholdersCount; + parser.depth = depth; + parser.isSubParsing = true; builder.append(parser.stringify(child, type)); } + if ( + !isSubParsing && + (type == ParseType.ANSI || type == ParseType.DISCORD_ANSI) + ) { + builder.append(ansiMap.get("r")); + } + if (type == ParseType.DISCORD_ANSI) { // as of the time writing this (2024-12-28) discord doesn't support the bright colors yet return builder.toString().replace("\u001b[9", "\u001b[3"); @@ -285,7 +295,7 @@ public class ComponentUtilities { // is INTENTIONAL and is a FEATURE int i = 0; while (matcher.find()) { - formatsPlaceholdersCount++; + depth++; if (matcher.group().equals("%%")) { matcher.appendReplacement(sb, "%"); } else { @@ -297,7 +307,8 @@ public class ComponentUtilities { final ComponentParser parser = new ComponentParser(); parser.lastStyle = lastStyle + color + style; - parser.formatsPlaceholdersCount = formatsPlaceholdersCount; + parser.depth = depth; + parser.isSubParsing = true; matcher.appendReplacement( sb,