Skip to content

Commit

Permalink
Merge pull request #35 from Matt-MX/dev
Browse files Browse the repository at this point in the history
Add smart mode
Matt-MX authored Dec 1, 2024
2 parents cd3e5cd + 4607de8 commit 1681401
Showing 8 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ dependencies {
compileOnly(libs.tab.api)
compileOnly(libs.packet.events)
implementation(libs.entity.lib)
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
}

tasks {
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ paperweight = "1.7.1"
kotlin = "2.0.0"
updateVersions = "0.51.0"
shadow = "8.1.8"
paperApi = "1.20.4-R0.1-SNAPSHOT"
paperApi = "1.21.1-R0.1-SNAPSHOT"
placeholderapi = "2.11.6"
ktgui = "2.4.2-alpha"
runPaper = "2.2.4"
Empty file added logs/latest.log
Empty file.
1 change: 1 addition & 0 deletions src/main/java/com/mattmx/nametags/NameTags.java
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@

public class NameTags extends JavaPlugin {
public static final int TRANSPARENT = Color.fromARGB(0).asARGB();
public static final char LEGACY_CHAR = (char)167;
private static @Nullable NameTags instance;

private final HashMap<String, ConfigurationSection> groups = new HashMap<>();
1 change: 0 additions & 1 deletion src/main/java/com/mattmx/nametags/config/ConfigHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mattmx.nametags.config;

import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
83 changes: 74 additions & 9 deletions src/main/java/com/mattmx/nametags/config/TextFormatter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mattmx.nametags.config;

import com.mattmx.nametags.NameTags;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -18,25 +19,58 @@ public enum TextFormatter {
),
LEGACY(
"legacy",
(line) -> getLegacySerializer().deserialize(convertLegacyHex(line))
(line) -> getLegacySerializer().deserialize(convertLegacyHex(line.replace(NameTags.LEGACY_CHAR, '&')))
),
SMART(
"smart",
(line) -> {
// First replace any legacy chars with &
String mutableLine = convertLegacyHex(line.replace(NameTags.LEGACY_CHAR, '&'));

// Convert legacy to modern formatting
mutableLine = convertLegacyHexToMiniMessage(mutableLine);
mutableLine = mutableLine
.replace("&0", "<black>")
.replace("&1", "<dark_blue>")
.replace("&2", "<dark_green>")
.replace("&3", "<dark_aqua>")
.replace("&4", "<dark_red>")
.replace("&5", "<dark_purple>")
.replace("&6", "<gold>")
.replace("&7", "<gray>")
.replace("&8", "<dark_gray>")
.replace("&9", "<blue>")
.replace("&a", "<green>")
.replace("&b", "<aqua>")
.replace("&c", "<red>")
.replace("&d", "<light_purple>")
.replace("&e", "<yellow>")
.replace("&f", "<white>")
.replace("&k", "<obf>")
.replace("&l", "<b>")
.replace("&m", "<st>")
.replace("&n", "<u>")
.replace("&o", "<i>")
.replace("&r", "<reset>");

return MINI_MESSAGE.format(mutableLine);
}
)
;

// Converts legacy hex format &x&9&0&0&c&3&f to modern hex format &#900c3f
// Converts legacy hex format &x&9&0&0&c&3&f -> &#900c3f modern hex format
// https://github.com/Matt-MX/DisplayNameTags/issues/32#issuecomment-2509403581
private static String convertLegacyHex(String input) {
//regex to match the legacy hex format
String legacyHexPattern = "&x(&[0-9a-fA-F]){6}";
Pattern pattern = Pattern.compile(legacyHexPattern);
Matcher matcher = pattern.matcher(input);
private static final Pattern LEGACY_HEX_PATTERN = Pattern.compile("&x(&[0-9a-fA-F]){6}");
public static String convertLegacyHex(String input) {
Matcher matcher = LEGACY_HEX_PATTERN.matcher(input);

StringBuilder result = new StringBuilder();
while (matcher.find()) {
String legacyHex = matcher.group();
//extract hex digits from the legacy format
// Extract hex digits from the legacy format
String hexColor = legacyHex.replace("&x", "")
.replace("&", "");
//replace with modern format
// Replace with modern format
String modernHex = "&#" + hexColor;
matcher.appendReplacement(result, modernHex);
}
@@ -45,6 +79,37 @@ private static String convertLegacyHex(String input) {
return result.toString();
}

/**
* Converts Minecraft legacy hex color codes (&#RRGGBB) to MiniMessage format (<#RRGGBB>).
*
* @param legacyText The input string with legacy color codes.
* @return The converted string in MiniMessage format.
*/
public static String convertLegacyHexToMiniMessage(@NotNull String legacyText) {
if (legacyText.isEmpty()) {
return legacyText;
}

// Regex to match legacy hex color codes (&# followed by 6 hexadecimal characters)
Pattern legacyHexPattern = Pattern.compile("&#([0-9a-fA-F]{6})");
Matcher matcher = legacyHexPattern.matcher(legacyText);

StringBuilder convertedText = new StringBuilder();

while (matcher.find()) {
// Extract the hex color code (RRGGBB)
String hexColor = matcher.group(1);

// Replace with MiniMessage format
matcher.appendReplacement(convertedText, "<#" + hexColor + ">");
}

// Append the rest of the text
matcher.appendTail(convertedText);

return convertedText.toString();
}

private static final LegacyComponentSerializer legacy = LegacyComponentSerializer.builder()
.character('&')
.hexCharacter('#')
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ groups:
# Options:
# - minimessage
# - legacy
# - smart (Attempts to format legacy -> minimessage)
formatter: minimessage

# Extra features
22 changes: 22 additions & 0 deletions src/test/java/com/mattmx/nametags/config/TextFormatterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mattmx.nametags.config;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class TextFormatterTest {

@ParameterizedTest
@CsvSource({
"&x&c&7&0&0&3&9[Admin],&#c70039[Admin]",
"&x&9&0&0&c&3&fCoyotea&r,&#900c3fCoyotea&r",
"&x&9&0&0&c&3&fCoyotea,&#900c3fCoyotea",
"&x&c&7&0&0&3&9[Admin] &x&9&0&0&c&3&fCoyotea&r,&#c70039[Admin] &#900c3fCoyotea&r"
})
public void test_convertLegacyHex(String input, String expectedOut) {
final String out = TextFormatter.convertLegacyHex(input);

Assertions.assertEquals(expectedOut, out);
}

}

0 comments on commit 1681401

Please sign in to comment.