mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-22 05:06:44 +01:00
Strip legacy color codes if message cannot be parsed by MiniMessage (#4077)
This commit is contained in:
parent
cbb284b0fd
commit
a54276d3b2
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user