From 8dff973d2602d065193003815eed39f0c8964ef2 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 7 May 2025 14:24:07 +0900 Subject: [PATCH] support legacy hex colors --- CHANGELOG.md | 2 ++ azalea-chat/src/text_component.rs | 38 +++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 151621df..cc1ba076 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index 7332adfa..62547d0d 100644 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -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::(); + + 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 + ) + ); + } }