From dd11b6fafeeb668d04f1f077e31cf7ed4d3e2f18 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Tue, 19 Aug 2025 19:08:34 +0700 Subject: [PATCH] fix: hopefully fix denis crash + make the section sign problem a bit better (not really) --- build-number.txt | 2 +- .../chomens_bot/util/ComponentUtilities.java | 43 +++++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/build-number.txt b/build-number.txt index b6873d79..d5b91a9a 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -3586 \ No newline at end of file +3592 \ 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 a79b260a..37745c57 100644 --- a/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java +++ b/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java @@ -41,11 +41,11 @@ public class ComponentUtilities { private static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)"); private static final Pattern DISCORD_ANSI_PATTERN = Pattern.compile("(\\u001b\\[\\d+m)"); - // we are using our own total depth counter instead of adventure's - // because we don't want to throw an exception when exceeded the depth limit - private static final ThreadLocal TOTAL_DEPTH = ThreadLocal.withInitial(() -> 0); + private static final ThreadLocal TOTAL_PLACEHOLDERS = ThreadLocal.withInitial(() -> 0); + private static final int PLACEHOLDER_THRESHOLD = 4; + private static final long MAX_PARSE_TIME = 200; - private static final int MAX_DEPTH = 512; + private static final ThreadLocal START_PARSE_TIME = ThreadLocal.withInitial(() -> 0L); private static Map loadJsonStringMap (final String name) { final Map map = new Object2ObjectOpenHashMap<>(); @@ -102,6 +102,7 @@ public class ComponentUtilities { private static String guardedStringify (final ComponentEncoder serializer, final Component message) { try { + START_PARSE_TIME.set(System.currentTimeMillis()); return serializer.serialize(message); } catch (final Throwable throwable) { return guardedStringify( @@ -113,7 +114,7 @@ public class ComponentUtilities { ) ); } finally { - TOTAL_DEPTH.set(0); + TOTAL_PLACEHOLDERS.set(0); } } @@ -125,16 +126,14 @@ public class ComponentUtilities { return guardedStringify(LEGACY_COMPONENT_SERIALIZER, message); } - public static String stringifyAnsi (final Component message) { return stringifyAnsi(message, true); } - public static String stringifyAnsi (final Component message, final boolean resetEnd) { - return guardedStringify(TRUE_COLOR_ANSI_SERIALIZER, message) + (resetEnd ? "\u001b[0m" : ""); + public static String stringifyAnsi (final Component message) { + return guardedStringify(TRUE_COLOR_ANSI_SERIALIZER, message); } - public static String stringifyDiscordAnsi (final Component message) { return stringifyDiscordAnsi(message, true); } - public static String stringifyDiscordAnsi (final Component message, final boolean resetEnd) { + public static String stringifyDiscordAnsi (final Component message) { return guardedStringify(DISCORD_ANSI_SERIALIZER, message) - .replace("\u001b[9", "\u001b[3") // we have to downscale because discord's ANSI doesn't have bright colors - + (resetEnd ? "\u001b[0m" : ""); + // we have to downscale because discord's ANSI doesn't support bright colors as of now + .replace("\u001b[9", "\u001b[3"); } public static String deserializeFromDiscordAnsi (final String original) { @@ -197,14 +196,8 @@ public class ComponentUtilities { .replaceFirst("ยง+$", "") ); - // goofy code - return StringUtilities.replaceLast( - isDiscord - ? stringifyDiscordAnsi(deserialized, false) - : stringifyAnsi(deserialized, false), - "\u001b[0m", - "" - ) + mapText(Component.text(formatting), true, isDiscord); + return (isDiscord ? stringifyDiscordAnsi(deserialized) : stringifyAnsi(deserialized)) + + mapText(Component.text(formatting), true, isDiscord); } } @@ -251,9 +244,13 @@ public class ComponentUtilities { if (idx < 0 || idx > component.arguments().size()) throw new IllegalArgumentException(); - final int currentTotalDepth = TOTAL_DEPTH.get(); - if (currentTotalDepth > MAX_DEPTH) return; - TOTAL_DEPTH.set(currentTotalDepth + 1); + final int currentTotalPlaceholders = TOTAL_PLACEHOLDERS.get(); + if (currentTotalPlaceholders > PLACEHOLDER_THRESHOLD + && System.currentTimeMillis() - START_PARSE_TIME.get() > MAX_PARSE_TIME) { + return; + } else { + TOTAL_PLACEHOLDERS.set(currentTotalPlaceholders + 1); + } result.add( component.arguments()