Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-111 Fix message formatting #111

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public ChatRenderedMessage process(ChatMessage chatMessage) {
format = this.templateService.applyTemplates(format);
format = this.placeholderRegistry.format(format, sender);

format = Legacy.clearSection(format);
format = Legacy.legacyToAdventure(format);

Component renderedMessage = this.miniMessage.deserialize(format, this.createTags(chatMessage));

return new ChatRenderedMessage(sender, GSON.serialize(renderedMessage));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.eternalcode.formatter.legacy;

import com.google.common.collect.ImmutableMap;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -13,8 +16,34 @@ public final class Legacy {
public static final String SHADOW = "<ampersand>";

public static final Pattern ALL_PATTERN = Pattern.compile(".*");
public static final Pattern AMPERSAND_PATTERN = Pattern.compile("(?i)" + AMPERSAND + "[0-9A-FK-ORX#]");
public static final Pattern AMPERSAND_PATTERN = Pattern.compile("(?i)" + AMPERSAND + "([0-9A-FK-ORX#])");
public static final Pattern SHADOW_PATTERN = Pattern.compile("(?i)" + SHADOW + "[0-9A-FK-ORX#]");
public static final Pattern HEX_PATTERN = Pattern.compile("(?i)" + AMPERSAND + "#([0-9A-F]{6})");

public static final Map<String, String> codeTranslations = new ImmutableMap.Builder<String, String>()
.put("0", "<black>")
.put("1", "<dark_blue>")
.put("2", "<dark_green>")
.put("3", "<dark_aqua>")
.put("4", "<dark_red>")
.put("5", "<dark_purple>")
.put("6", "<gold>")
.put("7", "<gray>")
.put("8", "<dark_gray>")
.put("9", "<blue>")
.put("a", "<green>")
.put("b", "<aqua>")
.put("c", "<red>")
.put("d", "<light_purple>")
.put("e", "<yellow>")
.put("f", "<white>")
.put("k", "<obfuscated>")
.put("l", "<bold>")
.put("m", "<strikethrough>")
.put("n", "<underlined>")
.put("o", "<italic>")
.put("r", "<reset>")
.build();

private Legacy() {
}
Expand Down Expand Up @@ -66,4 +95,23 @@ static String placeholderToAmpersand(String text) {
return builder.toString();
}

public static String legacyToAdventure(String input) {
String result = HEX_PATTERN.matcher(input).replaceAll(matchResult -> {
String hex = matchResult.group(1);
return "<#" + hex + ">";
});

result = AMPERSAND_PATTERN.matcher(result).replaceAll(matchResult -> {
String color = matchResult.group(1);
String adventure = codeTranslations.get(color.toLowerCase());

if (adventure == null) {
return matchResult.group();
}

return adventure;
});

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.formatter.legacy;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -39,4 +40,14 @@ private void assertShadow(String input, String expectedShadowed) {
assertEquals(input, deshadowed);
}

@Test
@DisplayName("Should convert legacy color codes to adventure")
void testLegacyToAdventure() {
String input = "&c SIEMA &#8376d3 &rtest&a!";

String result = Legacy.legacyToAdventure(input);

assertEquals("<red> SIEMA <#8376d3> <reset>test<green>!", result);
}

}