1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

support legacy hex colors

This commit is contained in:
mat 2025-05-07 14:24:07 +09:00
parent 4a1fdf0121
commit 8dff973d26
2 changed files with 36 additions and 4 deletions

View file

@ -19,6 +19,7 @@ write down most non-trivial breaking changes.
- `FormattedText::to_html` and `FormattedText::to_custom_format`.
- Add auto-reconnecting which is enabled by default.
- The pathfinder no longer avoids slabs, stairs, and dirt path blocks.
- Non-standard legacy hex colors like `§#ff0000` are now supported in azalea-chat.
### Changed
@ -37,3 +38,4 @@ write down most non-trivial breaking changes.
- Block shapes and some properties were using data from `1.20.3-pre4` due to using an old data generator (Pixlyzer), which has now been replaced with the data generator from [Pumpkin](https://github.com/Pumpkin-MC/Extractor).
- No more chunk errors when the client joins another world with the same name but different height.
- Mining now cancels correctly and doesn't flag Grim.
- azalea-chat now handles legacy color codes correctly when parsing from NBT.

View file

@ -2,7 +2,11 @@ use std::fmt::Display;
use serde::{__private::ser::FlatMapSerializer, Serialize, Serializer, ser::SerializeMap};
use crate::{FormattedText, base_component::BaseComponent, style::ChatFormatting};
use crate::{
FormattedText,
base_component::BaseComponent,
style::{ChatFormatting, TextColor},
};
/// A component that contains text that's the same in all locales.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
@ -69,13 +73,26 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo
i += 1;
continue;
};
if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
if formatting_code == '#' {
let color = legacy_color_code
.chars()
.skip(i + 1)
// 7 to include the #
.take(7)
.collect::<String>();
if components.is_empty() || !components.last().unwrap().text.is_empty() {
components.push(TextComponent::new("".to_string()));
}
let style = &mut components.last_mut().unwrap().base.style;
style.color = TextColor::parse(color);
i += 6;
} else if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
if components.is_empty() || !components.last().unwrap().text.is_empty() {
components.push(TextComponent::new("".to_string()));
}
let style = &mut components.last_mut().unwrap().base.style;
// if the formatter is a reset, then we need to reset the style to the default
style.apply_formatting(&formatter);
}
i += 1;
@ -213,4 +230,17 @@ mod tests {
)
);
}
#[test]
fn test_legacy_color_code_with_rgb() {
let component = TextComponent::new("§#Ff0000This is a test message".to_string()).get();
assert_eq!(
component.to_ansi(),
format!(
"{RED}This is a test message{RESET}",
RED = Ansi::rgb(0xff0000),
RESET = Ansi::RESET
)
);
}
}