From a54276d3b22268982b8dae0ddb7278088e71231c Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 15 Jun 2023 10:23:09 +0200 Subject: [PATCH] Strip legacy color codes if message cannot be parsed by MiniMessage (#4077) --- .../core/configuration/caption/CaptionUtility.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionUtility.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionUtility.java index 005370a40..160e3277a 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionUtility.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionUtility.java @@ -28,16 +28,20 @@ import com.plotsquared.core.plot.flag.implementations.PlotTitleFlag; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.ParsingException; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Set; +import java.util.regex.Pattern; import static com.plotsquared.core.configuration.caption.ComponentTransform.nested; import static com.plotsquared.core.configuration.caption.ComponentTransform.stripClicks; public class CaptionUtility { + private static final Pattern LEGACY_FORMATTING = Pattern.compile("ยง[a-gklmnor0-9]"); + // flags which values are parsed by minimessage private static final Set>> MINI_MESSAGE_FLAGS = Set.of( GreetingFlag.class, @@ -100,7 +104,14 @@ public class CaptionUtility { */ public static String stripClickEvents(final @NonNull String miniMessageString) { // parse, transform and serialize again - Component component = MiniMessage.miniMessage().deserialize(miniMessageString); + Component component; + try { + component = MiniMessage.miniMessage().deserialize(miniMessageString); + } catch (ParsingException e) { + // if the String cannot be parsed, we try stripping legacy colors + String legacyStripped = LEGACY_FORMATTING.matcher(miniMessageString).replaceAll(""); + component = MiniMessage.miniMessage().deserialize(legacyStripped); + } component = CLICK_STRIP_TRANSFORM.transform(component); return MiniMessage.miniMessage().serialize(component); }