From d9b6f6306a0519539c963dbc06a058f4f3be6f83 Mon Sep 17 00:00:00 2001 From: zml Date: Tue, 13 Apr 2021 16:02:37 -0700 Subject: [PATCH] text-serializer-legacy: Properly passthrough invalid RGB codes --- .../legacy/LegacyComponentSerializerImpl.java | 22 +++++++++++++++---- .../legacy/LegacyComponentSerializerTest.java | 8 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/text-serializer-legacy/src/main/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerImpl.java b/text-serializer-legacy/src/main/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerImpl.java index 83543474c..4017faf70 100644 --- a/text-serializer-legacy/src/main/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerImpl.java +++ b/text-serializer-legacy/src/main/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerImpl.java @@ -155,20 +155,34 @@ final class LegacyComponentSerializerImpl implements LegacyComponentSerializer { return null; } if(foundFormat == FormatCodeType.KYORI_HEX) { - return new DecodedFormat(foundFormat, TextColor.fromHexString('#' + input.substring(pos, pos + 6))); + final @Nullable TextColor parsed = tryParseHexColor(input.substring(pos, pos + 6)); + if(parsed != null) { + return new DecodedFormat(foundFormat, parsed); + } } else if(foundFormat == FormatCodeType.MOJANG_LEGACY) { return new DecodedFormat(foundFormat, FORMATS.get(LEGACY_CHARS.indexOf(legacy))); } else if(foundFormat == FormatCodeType.BUNGEECORD_UNUSUAL_HEX) { - final StringBuilder foundHex = new StringBuilder(); + final StringBuilder foundHex = new StringBuilder(6); for(int i = pos - 1; i >= pos - 11; i -= 2) { foundHex.append(input.charAt(i)); } - foundHex.append('#'); - return new DecodedFormat(foundFormat, TextColor.fromHexString(foundHex.reverse().toString())); + final @Nullable TextColor parsed = tryParseHexColor(foundHex.reverse().toString()); + if(parsed != null) { + return new DecodedFormat(foundFormat, parsed); + } } return null; } + private static @Nullable TextColor tryParseHexColor(final String hexDigits) { + try { + final int color = Integer.parseInt(hexDigits, 16); + return TextColor.color(color); + } catch(final NumberFormatException ex) { + return null; + } + } + private static boolean isHexTextColor(final TextFormat format) { return format instanceof TextColor && !(format instanceof NamedTextColor); } diff --git a/text-serializer-legacy/src/test/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerTest.java b/text-serializer-legacy/src/test/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerTest.java index 36c635187..c4eff83c1 100644 --- a/text-serializer-legacy/src/test/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerTest.java +++ b/text-serializer-legacy/src/test/java/net/kyori/adventure/text/serializer/legacy/LegacyComponentSerializerTest.java @@ -310,4 +310,12 @@ void testNoRedundantReset() { final String roundtripped = LegacyComponentSerializer.legacyAmpersand().serialize(deserialized); assertEquals(text, roundtripped); } + + @Test + void testInvalidHexStringsPassedThrough() { + final String text = "Hello&#hellos world"; + final Component deserialized = LegacyComponentSerializer.legacyAmpersand().deserialize(text); + + assertEquals(Component.text(text), deserialized); + } }