Skip to content

Commit

Permalink
Convert hex legacy chars to MiniMessage too
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 16, 2024
1 parent 74b6c20 commit 5de4f21
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("[§&]#(?<hex>[0-9a-fA-F]{6})");
private static final Pattern LEGACY_UGLY_HEX_PATTERN = Pattern.compile("([§&])x\\1(?<h1>[0-9a-fA-F])\\1(?<h2>[0-9a-fA-F])\\1(?<h3>[0-9a-fA-F])\\1(?<h4>[0-9a-fA-F])\\1(?<h5>[0-9a-fA-F])\\1(?<h6>[0-9a-fA-F])");
private static final Map<Character, String> MINIMESSAGE_REPLACEMENTS;
private static final List<PlaceholderParser> preParsePlaceholders = new CopyOnWriteArrayList<>();
private static final List<PlaceholderParser> 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");
Expand Down Expand Up @@ -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);
Expand All @@ -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<Character, String> entry : MINIMESSAGE_REPLACEMENTS.entrySet()) {
str = str.replace(prefix + entry.getKey(), "<" + entry.getValue() + ">");
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<red>Hello World", PlaceholderUtil.convertMiniMessageString("§cHello World"));
}

@Test
public void testBasicConversionAmpersand() {
Assertions.assertEquals("<red>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"));
}
}
14 changes: 14 additions & 0 deletions buildSrc/src/main/kotlin/pm.java-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +77,7 @@ protected void load(AxiomConfiguration config) {

AxiomConfigurationSection perDomainStatusSection = config.getSection("advanced.perDomainStatus.domains");
List<String> domains = perDomainStatusSection.getKeys();
Map<String, PerDomainStatusDomain> domainMap = new LinkedHashMap<>();
Map<String, PerDomainStatusDomain> domainMap = new HashMap<>();
for (String domainId : domains) {
AxiomConfigurationSection domainSection = perDomainStatusSection.getSection(domainId);

Expand Down

0 comments on commit 5de4f21

Please sign in to comment.