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

Normalize legacy text before deserializing #5710

Closed
wants to merge 4 commits into from
Closed
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 @@ -12,6 +12,7 @@
public final class AdventureUtil {
private static final LegacyComponentSerializer LEGACY_SERIALIZER;
private static final MiniMessage MINI_MESSAGE_NO_TAGS;
private static final char LEGACY_CHARACTER = '§';
private static final String LOOKUP = "0123456789abcdefklmnor";
private static final NamedTextColor[] COLORS = new NamedTextColor[]{NamedTextColor.BLACK, NamedTextColor.DARK_BLUE, NamedTextColor.DARK_GREEN, NamedTextColor.DARK_AQUA, NamedTextColor.DARK_RED, NamedTextColor.DARK_PURPLE, NamedTextColor.GOLD, NamedTextColor.GRAY, NamedTextColor.DARK_GRAY, NamedTextColor.BLUE, NamedTextColor.GREEN, NamedTextColor.AQUA, NamedTextColor.RED, NamedTextColor.LIGHT_PURPLE, NamedTextColor.YELLOW, NamedTextColor.WHITE};
private static IEssentials ess;
Expand Down Expand Up @@ -85,14 +86,36 @@ public static String legacyToMini(String text) {
* @param useCustomTags true if gold and red colors should use primary and secondary tags instead.
*/
public static String legacyToMini(String text, boolean useCustomTags) {
final Component deserializedText = LEGACY_SERIALIZER.deserialize(text);
final Component deserializedText = LEGACY_SERIALIZER.deserialize(normalizeLegacyText(text));
if (useCustomTags) {
return miniMessageInstance.serialize(deserializedText);
} else {
return MINI_MESSAGE_NO_TAGS.serialize(deserializedText);
}
}

/**
* Normalizes formatting codes within a section sign legacy string
*/
private static String normalizeLegacyText(String text) {
if (text == null || text.isEmpty()) {
return text;
}
final int length = text.length();
final char[] chars = text.toCharArray();
final StringBuilder normalized = new StringBuilder();
for (int i = 0; i < length; ++i) {
final char current = chars[i];
normalized.append(current);
if (current == LEGACY_CHARACTER && i < length - 1) {
final char next = chars[i + 1];
normalized.append(Character.toLowerCase(next));
++i;
}
}
return normalized.toString();
}

/**
* Get the {@link NamedTextColor} from its associated section sign char.
*/
Expand Down
Loading