Strip legacy color codes if message cannot be parsed by MiniMessage (#4077)

This commit is contained in:
Hannes Greule 2023-06-15 10:23:09 +02:00 committed by GitHub
parent cbb284b0fd
commit a54276d3b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Class<? extends PlotFlag<?, ?>>> 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);
}