From 5de4f21a77e5fe04e94ce5e883eee75a98679e27 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Mon, 16 Dec 2024 10:30:27 +0100 Subject: [PATCH] Convert hex legacy chars to MiniMessage too --- .../pistonmotd/api/PlaceholderUtil.java | 25 +++++++++---- .../pistonmotd/api/ConversionTest.java | 36 +++++++++++++++++++ .../kotlin/pm.java-conventions.gradle.kts | 14 ++++++++ .../shared/PistonMOTDServerConfig.java | 4 +-- 4 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 api/src/test/java/net/pistonmaster/pistonmotd/api/ConversionTest.java diff --git a/api/src/main/java/net/pistonmaster/pistonmotd/api/PlaceholderUtil.java b/api/src/main/java/net/pistonmaster/pistonmotd/api/PlaceholderUtil.java index 7dd4d96..d4d8fb6 100644 --- a/api/src/main/java/net/pistonmaster/pistonmotd/api/PlaceholderUtil.java +++ b/api/src/main/java/net/pistonmaster/pistonmotd/api/PlaceholderUtil.java @@ -4,23 +4,27 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.pistonmaster.pistonmotd.kyori.PistonSerializersRelocated; import org.apiguardian.api.API; +import org.jetbrains.annotations.VisibleForTesting; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.regex.Pattern; /** * Class for managing parsers and parse text. */ @SuppressWarnings({"unused"}) public class PlaceholderUtil { + private static final Pattern LEGACY_HEX_PATTERN = Pattern.compile("[§&]#(?[0-9a-fA-F]{6})"); + private static final Pattern LEGACY_UGLY_HEX_PATTERN = Pattern.compile("([§&])x\\1(?

[0-9a-fA-F])\\1(?

[0-9a-fA-F])\\1(?

[0-9a-fA-F])\\1(?

[0-9a-fA-F])\\1(?

[0-9a-fA-F])\\1(?
[0-9a-fA-F])"); private static final Map MINIMESSAGE_REPLACEMENTS; private static final List preParsePlaceholders = new CopyOnWriteArrayList<>(); private static final List postParsePlaceholders = new CopyOnWriteArrayList<>(); static { - MINIMESSAGE_REPLACEMENTS = new LinkedHashMap<>(); + MINIMESSAGE_REPLACEMENTS = new HashMap<>(); MINIMESSAGE_REPLACEMENTS.put('0', "black"); MINIMESSAGE_REPLACEMENTS.put('1', "dark_blue"); MINIMESSAGE_REPLACEMENTS.put('2', "dark_green"); @@ -79,10 +83,7 @@ public static String parseTextToLegacy(final String text) { } private static Component parseTextToComponent(final String text) { - String parsedText = text; - - parsedText = replaceLegacyWithMiniMessage("&", parsedText); - parsedText = replaceLegacyWithMiniMessage("§", parsedText); + String parsedText = convertMiniMessageString(text); for (PlaceholderParser parser : preParsePlaceholders) { parsedText = parser.parseString(parsedText); @@ -102,6 +103,18 @@ private static Component parseTextToComponent(final String text) { return PistonSerializersRelocated.ampersandRGB.deserialize(ampersandRGB); } + @VisibleForTesting + @API(status = API.Status.INTERNAL) + public static String convertMiniMessageString(String str) { + str = LEGACY_HEX_PATTERN.matcher(str).replaceAll("<#${hex}>"); + str = LEGACY_UGLY_HEX_PATTERN.matcher(str).replaceAll("<#${h1}${h2}${h3}${h4}${h5}${h6}>"); + + str = replaceLegacyWithMiniMessage("&", str); + str = replaceLegacyWithMiniMessage("§", str); + + return str; + } + private static String replaceLegacyWithMiniMessage(String prefix, String str) { for (Map.Entry entry : MINIMESSAGE_REPLACEMENTS.entrySet()) { str = str.replace(prefix + entry.getKey(), "<" + entry.getValue() + ">"); diff --git a/api/src/test/java/net/pistonmaster/pistonmotd/api/ConversionTest.java b/api/src/test/java/net/pistonmaster/pistonmotd/api/ConversionTest.java new file mode 100644 index 0000000..ed3cd52 --- /dev/null +++ b/api/src/test/java/net/pistonmaster/pistonmotd/api/ConversionTest.java @@ -0,0 +1,36 @@ +package net.pistonmaster.pistonmotd.api; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ConversionTest { + @Test + public void testBasicConversionSection() { + Assertions.assertEquals("Hello World", PlaceholderUtil.convertMiniMessageString("§cHello World")); + } + + @Test + public void testBasicConversionAmpersand() { + Assertions.assertEquals("Hello World", PlaceholderUtil.convertMiniMessageString("&cHello World")); + } + + @Test + public void testBasicConversionHexSection() { + Assertions.assertEquals("<#ff0000>Hello World", PlaceholderUtil.convertMiniMessageString("§#ff0000Hello World")); + } + + @Test + public void testBasicConversionHexAmpersand() { + Assertions.assertEquals("<#ff0000>Hello World", PlaceholderUtil.convertMiniMessageString("&#ff0000Hello World")); + } + + @Test + public void testBasicConversionHexSectionUgly() { + Assertions.assertEquals("<#ff0000>Hello World", PlaceholderUtil.convertMiniMessageString("§x§f§f§0§0§0§0Hello World")); + } + + @Test + public void testBasicConversionHexAmpersandUgly() { + Assertions.assertEquals("<#ff0000>Hello World", PlaceholderUtil.convertMiniMessageString("&x&f&f&0&0&0&0Hello World")); + } +} diff --git a/buildSrc/src/main/kotlin/pm.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/pm.java-conventions.gradle.kts index 832efe4..564600c 100644 --- a/buildSrc/src/main/kotlin/pm.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/pm.java-conventions.gradle.kts @@ -11,12 +11,26 @@ dependencies { compileOnly("org.projectlombok:lombok:1.18.36") annotationProcessor("org.projectlombok:lombok:1.18.36") + + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.3") + testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.3") + testImplementation("org.mockito:mockito-core:5.14.2") + testImplementation("org.mockito:mockito-junit-jupiter:5.14.2") } tasks { processResources { expand("version" to version, "description" to description, "url" to "https://pistonmaster.net/PistonMOTD") } + test { + reports.junitXml.required = true + reports.html.required = true + useJUnitPlatform() + maxParallelForks = Runtime.getRuntime().availableProcessors().div(2).coerceAtLeast(1) + } + jar { + from(rootProject.file("LICENSE")) + } } java { diff --git a/shared/src/main/java/net/pistonmaster/pistonmotd/shared/PistonMOTDServerConfig.java b/shared/src/main/java/net/pistonmaster/pistonmotd/shared/PistonMOTDServerConfig.java index f119f45..fe04add 100644 --- a/shared/src/main/java/net/pistonmaster/pistonmotd/shared/PistonMOTDServerConfig.java +++ b/shared/src/main/java/net/pistonmaster/pistonmotd/shared/PistonMOTDServerConfig.java @@ -5,7 +5,7 @@ import net.skinsrestorer.axiom.AxiomConfiguration; import net.skinsrestorer.axiom.AxiomConfigurationSection; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -77,7 +77,7 @@ protected void load(AxiomConfiguration config) { AxiomConfigurationSection perDomainStatusSection = config.getSection("advanced.perDomainStatus.domains"); List domains = perDomainStatusSection.getKeys(); - Map domainMap = new LinkedHashMap<>(); + Map domainMap = new HashMap<>(); for (String domainId : domains) { AxiomConfigurationSection domainSection = perDomainStatusSection.getSection(domainId);