From f73dcc8afb3e2372933e5ae1c2171fb209491024 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sat, 15 Jan 2022 14:23:47 +0100 Subject: [PATCH] properly handle json in TextParser Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../src/main/java/tc/oc/pgm/util/text/TextParser.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/util/src/main/java/tc/oc/pgm/util/text/TextParser.java b/util/src/main/java/tc/oc/pgm/util/text/TextParser.java index e5fdfa014f..7adb68b0ca 100644 --- a/util/src/main/java/tc/oc/pgm/util/text/TextParser.java +++ b/util/src/main/java/tc/oc/pgm/util/text/TextParser.java @@ -434,6 +434,8 @@ public static Component parseComponent(String text) throws TextException { * *
Accepts legacy formatting with "ยง" as the color character. * + *
Accepts full qualified json strings as components. + * *
This method is mainly for backwards compatability for {@link * XMLUtils#parseFormattedText(Node, Component)}. Previously using {@link #parseComponent(String)} * with the result from {@code parseFormattedText} would bug out when sent to older clients, since @@ -442,10 +444,19 @@ public static Component parseComponent(String text) throws TextException { * * @param text The text. * @return a Component. + * @throws TextException If there is json present and it is invalid. */ public static Component parseComponentSection(String text) { checkNotNull(text, "cannot parse component from null"); + if (text.startsWith("{\"") && text.endsWith("\"}")) { + try { + return GsonComponentSerializer.gson().deserialize(text); + } catch (JsonSyntaxException e) { + throw invalidFormat(text, Component.class, e); + } + } + return LegacyComponentSerializer.legacySection().deserialize(text); }