From 548187238ee2973695f02cc96705f9c206e1ebad Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 17 Jul 2023 17:02:31 -0500 Subject: [PATCH] feat: Add more Legacy API parsing --- api/build.gradle.kts | 4 ++ .../api/utils/LegacyUtils.java | 69 +++++++++++++++++++ .../miniplaceholders/test/LegacyTest.java | 20 ++++++ 3 files changed, 93 insertions(+) create mode 100644 api/src/test/java/io/github/miniplaceholders/test/LegacyTest.java diff --git a/api/build.gradle.kts b/api/build.gradle.kts index c53c642..ee72884 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { testImplementation(libs.adventure.api) testImplementation(libs.adventure.minimesssage) testImplementation(libs.adventure.serializer.plain) + testImplementation(libs.adventure.serializer.legacy) } tasks { @@ -46,4 +47,7 @@ tasks { "https://jd.advntr.dev/text-minimessage/${libs.versions.adventure.get()}/" ) } + compileTestJava { + options.encoding = Charsets.UTF_8.name() + } } diff --git a/api/src/main/java/io/github/miniplaceholders/api/utils/LegacyUtils.java b/api/src/main/java/io/github/miniplaceholders/api/utils/LegacyUtils.java index 49fc48c..8c871fe 100644 --- a/api/src/main/java/io/github/miniplaceholders/api/utils/LegacyUtils.java +++ b/api/src/main/java/io/github/miniplaceholders/api/utils/LegacyUtils.java @@ -1,6 +1,8 @@ package io.github.miniplaceholders.api.utils; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.Context; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -26,6 +28,12 @@ private LegacyUtils() { /** * Parse a string with possible legacy Ampersand/Section symbols + *
+ *

+ * This method should only be used in case a String is provided from a legacy plugin/mod + * or legacy source, where it is very likely to get a legacy string. + * It is not recommended to use this method regularly in any sense + *

* * @param string the string * @return a parsed string @@ -41,6 +49,67 @@ private LegacyUtils() { } return miniMessage().deserialize( miniMessage().serialize(LEGACY_HEX_SERIALIZER.deserialize(string)) + .replace("\\<", "<") + .replace("\\>", ">") + ); + } + + /** + * Parse a string with possible legacy Ampersand/Section symbols + * using a parsing Context + *

+ * This method should only be used in case a String is provided from a legacy plugin/mod + * or legacy source, where it is very likely to get a legacy string. + * It is not recommended to use this method regularly in any sense + *

+ * + * @param string the string + * @param context the provided parsing context + * @return a parsed string + * @since 2.2.1 + */ + public static @NotNull Component parsePossibleLegacy(@Nullable String string, @NotNull final Context context) { + if (string == null || string.isEmpty()) return Component.empty(); + if (string.indexOf(LegacyComponentSerializer.SECTION_CHAR) != -1) { + string = string.replace(LegacyComponentSerializer.SECTION_CHAR, LegacyComponentSerializer.AMPERSAND_CHAR); + } + if (string.indexOf(LegacyComponentSerializer.AMPERSAND_CHAR) == -1) { + return context.deserialize(string); + } + return context.deserialize( + miniMessage().serialize(LEGACY_HEX_SERIALIZER.deserialize(string)) + .replace("\\<", "<") + .replace("\\>", ">") + ); + } + + /** + * Parse a string with possible legacy Ampersand/Section symbols + * using the provided resolver + *

+ * This method should only be used in case a String is provided from a legacy plugin/mod + * or legacy source, where it is very likely to get a legacy string. + * It is not recommended to use this method regularly in any sense + *

+ * + * @param string the string + * @param resolver a resolver + * @return a parsed string + * @since 2.2.1 + */ + public static @NotNull Component parsePossibleLegacy(@Nullable String string, @NotNull final TagResolver resolver) { + if (string == null || string.isEmpty()) return Component.empty(); + if (string.indexOf(LegacyComponentSerializer.SECTION_CHAR) != -1) { + string = string.replace(LegacyComponentSerializer.SECTION_CHAR, LegacyComponentSerializer.AMPERSAND_CHAR); + } + if (string.indexOf(LegacyComponentSerializer.AMPERSAND_CHAR) == -1) { + return miniMessage().deserialize(string, resolver); + } + return miniMessage().deserialize( + miniMessage().serialize(LEGACY_HEX_SERIALIZER.deserialize(string)) + .replace("\\<", "<") + .replace("\\>", ">"), + resolver ); } } diff --git a/api/src/test/java/io/github/miniplaceholders/test/LegacyTest.java b/api/src/test/java/io/github/miniplaceholders/test/LegacyTest.java new file mode 100644 index 0000000..66a1c38 --- /dev/null +++ b/api/src/test/java/io/github/miniplaceholders/test/LegacyTest.java @@ -0,0 +1,20 @@ +package io.github.miniplaceholders.test; + +import io.github.miniplaceholders.api.utils.LegacyUtils; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class LegacyTest { + @Test + void testLegacyParsing() { + final String string = "hello... i &8hate legacy format §bso much"; + final Component parsed = LegacyUtils.parsePossibleLegacy(string); + final String result = PlainTextComponentSerializer.plainText().serialize(parsed); + final String expected = "hello... i hate legacy format so much"; + + assertEquals(expected, result); + } +}