Skip to content

Commit

Permalink
feat: Add more Legacy API parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Jul 17, 2023
1 parent c7845ce commit 5481872
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/build.gradle.kts
Expand Up @@ -14,6 +14,7 @@ dependencies {
testImplementation(libs.adventure.api)
testImplementation(libs.adventure.minimesssage)
testImplementation(libs.adventure.serializer.plain)
testImplementation(libs.adventure.serializer.legacy)
}

tasks {
Expand Down Expand Up @@ -46,4 +47,7 @@ tasks {
"https://jd.advntr.dev/text-minimessage/${libs.versions.adventure.get()}/"
)
}
compileTestJava {
options.encoding = Charsets.UTF_8.name()
}
}
@@ -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;
Expand All @@ -26,6 +28,12 @@ private LegacyUtils() {

/**
* Parse a string with possible legacy Ampersand/Section symbols
* <br>
* <p><b>
* 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
* </b></p>
*
* @param string the string
* @return a parsed string
Expand All @@ -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
* <p><b>
* 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
* </b></p>
*
* @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
* <p><b>
* 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
* </b></p>
*
* @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
);
}
}
20 changes: 20 additions & 0 deletions 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 = "<rainbow>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);
}
}

0 comments on commit 5481872

Please sign in to comment.