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

Fix bedrock resetting colors on newline #5311

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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 @@ -192,7 +192,36 @@ private static String convertMessage(Component message, String locale, boolean a
lastFormatReset = next == 'r';
}

return finalLegacy.toString();
// This needs a better name
String finalLegacyString = finalLegacy.toString();

// If the message contains \n then go through and re-set the color after each by caching the last color
// Bedrock is dumb and resets the color after a newline
if (finalLegacyString.contains("\n")) {
StringBuilder output = new StringBuilder();

StringBuilder lastColors = new StringBuilder();
for (int i = 0; i < finalLegacyString.length(); i++) {
char c = finalLegacyString.charAt(i);

output.append(c);

if (c == ChatColor.ESCAPE) {
char newColor = finalLegacyString.charAt(i + 1);
if (newColor == 'r') {
lastColors = new StringBuilder();
} else {
lastColors.append(ChatColor.ESCAPE).append(newColor);
}
} else if (c == '\n' && !lastColors.isEmpty()) {
output.append(lastColors);
}
}

return output.toString();
} else {
return finalLegacyString;
}
} catch (Exception e) {
GeyserImpl.getInstance().getLogger().debug(GSON_SERIALIZER.serialize(message));
GeyserImpl.getInstance().getLogger().error("Failed to parse message", e);
Expand Down
Loading