diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-20 15:17:07 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-20 15:17:07 -0500 |
| commit | dbb2092ac002790c07ad21cf7d12aabb477a2e74 (patch) | |
| tree | 5d5bb1e6dbca8250292a9e0b1edc7325699bbbaf /azalea-chat/src | |
| parent | ac4d675d44a93a6625f508263c650206a7ff1f98 (diff) | |
| download | azalea-drasl-dbb2092ac002790c07ad21cf7d12aabb477a2e74.tar.xz | |
Implement ALL packets (#16)
* add a couple more packets and improve codegen
* enums in packet codegen
* fix enums and MORE PACKETS
* make unsigned numbers the default
* codegen can make hashmaps
* UnsizedByteArray in codegen
* Vec and Option
* enum codgen works in more situations
* ServerboundInteractPacket
* Fix error with new error system
* More packets
* more packets
* more packets
* guess what was added
* yeah it's more packets
* add more packets
* packets
* start adding ClientboundBossEventPacket
* finish boss event packet
* improve codegen for linux
* start on command suggestions packet
* rename declare_commands to commands
* más paquetes
* fix generating custom payload packet
* more packets
* mehr Pakete
* improve codegen for movement packets
* rename move packets to have "packet" at the end
* fix some unused variable warns
* addere plus facis
* pli da pakoj
* plus de paquets
* più pacchetti
* make ChatFormatting a macro in azalea-chat
* change a match to matches! macro
* update SetPlayerTeam to use ChatFormatting
* ClientboundSetScorePacket & fix clippy warnings
* finish game state :tada:
* add remaining packets for other states
* fix error in ping.rs
Diffstat (limited to 'azalea-chat/src')
| -rwxr-xr-x | azalea-chat/src/component.rs | 2 | ||||
| -rwxr-xr-x | azalea-chat/src/style.rs | 297 | ||||
| -rwxr-xr-x | azalea-chat/src/text_component.rs | 18 |
3 files changed, 192 insertions, 125 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 8a038bf0..291baa16 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -18,7 +18,7 @@ pub enum Component { lazy_static! { pub static ref DEFAULT_STYLE: Style = Style { - color: Some(ChatFormatting::WHITE.try_into().unwrap()), + color: Some(ChatFormatting::White.try_into().unwrap()), ..Style::default() }; } diff --git a/azalea-chat/src/style.rs b/azalea-chat/src/style.rs index 3a667776..6edcdc2d 100755 --- a/azalea-chat/src/style.rs +++ b/azalea-chat/src/style.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, fmt}; +use azalea_buf::McBuf; use serde_json::Value; #[derive(Clone, PartialEq, Eq, Debug)] @@ -28,15 +29,15 @@ impl TextColor { } lazy_static! { - static ref LEGACY_FORMAT_TO_COLOR: HashMap<&'static ChatFormatting<'static>, TextColor> = { + static ref LEGACY_FORMAT_TO_COLOR: HashMap<&'static ChatFormatting, TextColor> = { let mut legacy_format_to_color = HashMap::new(); for formatter in &ChatFormatting::FORMATTERS { - if !formatter.is_format && *formatter != ChatFormatting::RESET { + if !formatter.is_format() && *formatter != ChatFormatting::Reset { legacy_format_to_color.insert( formatter, TextColor { - value: formatter.color.unwrap(), - name: Some(formatter.name.to_string()), + value: formatter.color().unwrap(), + name: Some(formatter.name().to_string()), }, ); } @@ -52,15 +53,6 @@ lazy_static! { }; } -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct ChatFormatting<'a> { - pub name: &'a str, - pub code: char, - pub is_format: bool, - pub id: i32, - pub color: Option<u32>, -} - pub struct Ansi {} impl Ansi { pub const BOLD: &'static str = "\u{1b}[1m"; @@ -80,91 +72,172 @@ impl Ansi { } } -impl<'a> ChatFormatting<'a> { - pub const BLACK: ChatFormatting<'a> = ChatFormatting::new("BLACK", '0', false, 0, Some(0)); - pub const DARK_BLUE: ChatFormatting<'a> = - ChatFormatting::new("DARK_BLUE", '1', false, 1, Some(170)); - pub const DARK_GREEN: ChatFormatting<'a> = - ChatFormatting::new("DARK_GREEN", '2', false, 2, Some(43520)); - pub const DARK_AQUA: ChatFormatting<'a> = - ChatFormatting::new("DARK_AQUA", '3', false, 3, Some(43690)); - pub const DARK_RED: ChatFormatting<'a> = - ChatFormatting::new("DARK_RED", '4', false, 4, Some(1114112)); - pub const DARK_PURPLE: ChatFormatting<'a> = - ChatFormatting::new("DARK_PURPLE", '5', false, 5, Some(11141290)); - pub const GOLD: ChatFormatting<'a> = ChatFormatting::new("GOLD", '6', false, 6, Some(16755200)); - pub const GRAY: ChatFormatting<'a> = ChatFormatting::new("GRAY", '7', false, 7, Some(11184810)); - pub const DARK_GRAY: ChatFormatting<'a> = - ChatFormatting::new("DARK_GRAY", '8', false, 8, Some(5592405)); - pub const BLUE: ChatFormatting<'a> = ChatFormatting::new("BLUE", '9', false, 9, Some(5592575)); - pub const GREEN: ChatFormatting<'a> = - ChatFormatting::new("GREEN", 'a', false, 10, Some(5635925)); - pub const AQUA: ChatFormatting<'a> = ChatFormatting::new("AQUA", 'b', false, 11, Some(5636095)); - pub const RED: ChatFormatting<'a> = ChatFormatting::new("RED", 'c', false, 12, Some(16733525)); - pub const LIGHT_PURPLE: ChatFormatting<'a> = - ChatFormatting::new("LIGHT_PURPLE", 'd', false, 13, Some(16733695)); - pub const YELLOW: ChatFormatting<'a> = - ChatFormatting::new("YELLOW", 'e', false, 14, Some(16777045)); - pub const WHITE: ChatFormatting<'a> = - ChatFormatting::new("WHITE", 'f', false, 15, Some(16777215)); - pub const OBFUSCATED: ChatFormatting<'a> = - ChatFormatting::new("OBFUSCATED", 'k', true, -1, None); - pub const STRIKETHROUGH: ChatFormatting<'a> = - ChatFormatting::new("STRIKETHROUGH", 'm', true, -1, None); - pub const BOLD: ChatFormatting<'a> = ChatFormatting::new("BOLD", 'l', true, -1, None); - pub const UNDERLINE: ChatFormatting<'a> = ChatFormatting::new("UNDERLINE", 'n', true, -1, None); - pub const ITALIC: ChatFormatting<'a> = ChatFormatting::new("ITALIC", 'o', true, -1, None); - pub const RESET: ChatFormatting<'a> = ChatFormatting::new("RESET", 'r', true, -1, None); - - pub const FORMATTERS: [ChatFormatting<'a>; 22] = [ - ChatFormatting::BLACK, - ChatFormatting::DARK_BLUE, - ChatFormatting::DARK_GREEN, - ChatFormatting::DARK_AQUA, - ChatFormatting::DARK_RED, - ChatFormatting::DARK_PURPLE, - ChatFormatting::GOLD, - ChatFormatting::GRAY, - ChatFormatting::DARK_GRAY, - ChatFormatting::BLUE, - ChatFormatting::GREEN, - ChatFormatting::AQUA, - ChatFormatting::RED, - ChatFormatting::LIGHT_PURPLE, - ChatFormatting::YELLOW, - ChatFormatting::WHITE, - ChatFormatting::OBFUSCATED, - ChatFormatting::STRIKETHROUGH, - ChatFormatting::BOLD, - ChatFormatting::UNDERLINE, - ChatFormatting::ITALIC, - ChatFormatting::RESET, +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, McBuf)] +pub enum ChatFormatting { + Black, + DarkBlue, + DarkGreen, + DarkAqua, + DarkRed, + DarkPurple, + Gold, + Gray, + DarkGray, + Blue, + Green, + Aqua, + Red, + LightPurple, + Yellow, + White, + Obfuscated, + Strikethrough, + Bold, + Underline, + Italic, + Reset, +} + +impl ChatFormatting { + pub const FORMATTERS: [ChatFormatting; 22] = [ + ChatFormatting::Black, + ChatFormatting::DarkBlue, + ChatFormatting::DarkGreen, + ChatFormatting::DarkAqua, + ChatFormatting::DarkRed, + ChatFormatting::DarkPurple, + ChatFormatting::Gold, + ChatFormatting::Gray, + ChatFormatting::DarkGray, + ChatFormatting::Blue, + ChatFormatting::Green, + ChatFormatting::Aqua, + ChatFormatting::Red, + ChatFormatting::LightPurple, + ChatFormatting::Yellow, + ChatFormatting::White, + ChatFormatting::Obfuscated, + ChatFormatting::Strikethrough, + ChatFormatting::Bold, + ChatFormatting::Underline, + ChatFormatting::Italic, + ChatFormatting::Reset, ]; - const fn new( - name: &str, - code: char, - is_format: bool, - id: i32, - color: Option<u32>, - ) -> ChatFormatting { - ChatFormatting { - name, - code, - is_format, - id, - color, + pub fn name(&self) -> &'static str { + match self { + ChatFormatting::Black => "BLACK", + ChatFormatting::DarkBlue => "DARK_BLUE", + ChatFormatting::DarkGreen => "DARK_GREEN", + ChatFormatting::DarkAqua => "DARK_AQUA", + ChatFormatting::DarkRed => "DARK_RED", + ChatFormatting::DarkPurple => "DARK_PURPLE", + ChatFormatting::Gold => "GOLD", + ChatFormatting::Gray => "GRAY", + ChatFormatting::DarkGray => "DARK_GRAY", + ChatFormatting::Blue => "BLUE", + ChatFormatting::Green => "GREEN", + ChatFormatting::Aqua => "AQUA", + ChatFormatting::Red => "RED", + ChatFormatting::LightPurple => "LIGHT_PURPLE", + ChatFormatting::Yellow => "YELLOW", + ChatFormatting::White => "WHITE", + ChatFormatting::Obfuscated => "OBFUSCATED", + ChatFormatting::Strikethrough => "STRIKETHROUGH", + ChatFormatting::Bold => "BOLD", + ChatFormatting::Underline => "UNDERLINE", + ChatFormatting::Italic => "ITALIC", + ChatFormatting::Reset => "RESET", } } - pub fn from_code(code: char) -> Option<&'static ChatFormatting<'static>> { - for formatter in &ChatFormatting::FORMATTERS { - if formatter.code == code { - return Some(formatter); - } + pub fn code(&self) -> char { + match self { + ChatFormatting::Black => '0', + ChatFormatting::DarkBlue => '1', + ChatFormatting::DarkGreen => '2', + ChatFormatting::DarkAqua => '3', + ChatFormatting::DarkRed => '4', + ChatFormatting::DarkPurple => '5', + ChatFormatting::Gold => '6', + ChatFormatting::Gray => '7', + ChatFormatting::DarkGray => '8', + ChatFormatting::Blue => '9', + ChatFormatting::Green => 'a', + ChatFormatting::Aqua => 'b', + ChatFormatting::Red => 'c', + ChatFormatting::LightPurple => 'd', + ChatFormatting::Yellow => 'e', + ChatFormatting::White => 'f', + ChatFormatting::Obfuscated => 'k', + ChatFormatting::Strikethrough => 'm', + ChatFormatting::Bold => 'l', + ChatFormatting::Underline => 'n', + ChatFormatting::Italic => 'o', + ChatFormatting::Reset => 'r', } + } - None + pub fn from_code(code: char) -> Option<ChatFormatting> { + match code { + '0' => Some(ChatFormatting::Black), + '1' => Some(ChatFormatting::DarkBlue), + '2' => Some(ChatFormatting::DarkGreen), + '3' => Some(ChatFormatting::DarkAqua), + '4' => Some(ChatFormatting::DarkRed), + '5' => Some(ChatFormatting::DarkPurple), + '6' => Some(ChatFormatting::Gold), + '7' => Some(ChatFormatting::Gray), + '8' => Some(ChatFormatting::DarkGray), + '9' => Some(ChatFormatting::Blue), + 'a' => Some(ChatFormatting::Green), + 'b' => Some(ChatFormatting::Aqua), + 'c' => Some(ChatFormatting::Red), + 'd' => Some(ChatFormatting::LightPurple), + 'e' => Some(ChatFormatting::Yellow), + 'f' => Some(ChatFormatting::White), + 'k' => Some(ChatFormatting::Obfuscated), + 'm' => Some(ChatFormatting::Strikethrough), + 'l' => Some(ChatFormatting::Bold), + 'n' => Some(ChatFormatting::Underline), + 'o' => Some(ChatFormatting::Italic), + 'r' => Some(ChatFormatting::Reset), + _ => None, + } + } + + pub fn is_format(&self) -> bool { + matches!( + self, + ChatFormatting::Obfuscated + | ChatFormatting::Strikethrough + | ChatFormatting::Bold + | ChatFormatting::Underline + | ChatFormatting::Italic + | ChatFormatting::Reset + ) + } + + pub fn color(&self) -> Option<u32> { + match self { + ChatFormatting::Black => Some(0), + ChatFormatting::DarkBlue => Some(170), + ChatFormatting::DarkGreen => Some(43520), + ChatFormatting::DarkAqua => Some(43690), + ChatFormatting::DarkRed => Some(1114112), + ChatFormatting::DarkPurple => Some(11141290), + ChatFormatting::Gold => Some(16755200), + ChatFormatting::Gray => Some(11184810), + ChatFormatting::DarkGray => Some(5592405), + ChatFormatting::Blue => Some(5592575), + ChatFormatting::Green => Some(5635925), + ChatFormatting::Aqua => Some(5636095), + ChatFormatting::Red => Some(16733525), + ChatFormatting::LightPurple => Some(16733695), + ChatFormatting::Yellow => Some(16777045), + ChatFormatting::White => Some(16777215), + _ => None, + } } } @@ -189,15 +262,15 @@ impl fmt::Display for TextColor { } // from ChatFormatting to TextColor -impl TryFrom<ChatFormatting<'_>> for TextColor { +impl TryFrom<ChatFormatting> for TextColor { type Error = String; - fn try_from(formatter: ChatFormatting<'_>) -> Result<Self, Self::Error> { - if formatter.is_format { - return Err(format!("{} is not a color", formatter.name)); + fn try_from(formatter: ChatFormatting) -> Result<Self, Self::Error> { + if formatter.is_format() { + return Err(format!("{} is not a color", formatter.name())); } - let color = formatter.color.unwrap_or(0); - Ok(Self::new(color, Some(formatter.name.to_string()))) + let color = formatter.color().unwrap_or(0); + Ok(Self::new(color, Some(formatter.name().to_string()))) } } @@ -363,21 +436,15 @@ impl Style { /// Apply a ChatFormatting to this style pub fn apply_formatting(&mut self, formatting: &ChatFormatting) { match *formatting { - ChatFormatting::BOLD => self.bold = Some(true), - ChatFormatting::ITALIC => self.italic = Some(true), - ChatFormatting::UNDERLINE => self.underlined = Some(true), - ChatFormatting::STRIKETHROUGH => self.strikethrough = Some(true), - ChatFormatting::OBFUSCATED => self.obfuscated = Some(true), - ChatFormatting::RESET => self.reset = true, - ChatFormatting { - name: _, - code: _, - is_format: _, - id: _, - color, - } => { + ChatFormatting::Bold => self.bold = Some(true), + ChatFormatting::Italic => self.italic = Some(true), + ChatFormatting::Underline => self.underlined = Some(true), + ChatFormatting::Strikethrough => self.strikethrough = Some(true), + ChatFormatting::Obfuscated => self.obfuscated = Some(true), + ChatFormatting::Reset => self.reset = true, + formatter => { // if it's a color, set it - if let Some(color) = color { + if let Some(color) = formatter.color() { self.color = Some(TextColor::from_rgb(color)); } } @@ -455,7 +522,7 @@ mod tests { format!( "{reset}{italic}{white}", reset = Ansi::RESET, - white = Ansi::rgb(ChatFormatting::WHITE.color.unwrap()), + white = Ansi::rgb(ChatFormatting::White.color().unwrap()), italic = Ansi::ITALIC ) ) @@ -465,15 +532,15 @@ mod tests { fn test_from_code() { assert_eq!( ChatFormatting::from_code('a').unwrap(), - &ChatFormatting::GREEN + ChatFormatting::Green ); } #[test] fn test_apply_formatting() { let mut style = Style::default(); - style.apply_formatting(&ChatFormatting::BOLD); - style.apply_formatting(&ChatFormatting::RED); + style.apply_formatting(&ChatFormatting::Bold); + style.apply_formatting(&ChatFormatting::Red); assert_eq!(style.color, Some(TextColor::from_rgb(16733525))); } } diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index a3eb0e4b..564511fc 100755 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -29,7 +29,7 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo 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); + style.apply_formatting(&formatter); } i += 1; } else { @@ -93,9 +93,9 @@ mod tests { component.to_ansi(None), format!( "{GREEN}Hypixel Network {RED}[1.8-1.18]\n{BOLD}{AQUA}HAPPY HOLIDAYS{RESET}", - GREEN = Ansi::rgb(ChatFormatting::GREEN.color.unwrap()), - RED = Ansi::rgb(ChatFormatting::RED.color.unwrap()), - AQUA = Ansi::rgb(ChatFormatting::AQUA.color.unwrap()), + GREEN = Ansi::rgb(ChatFormatting::Green.color().unwrap()), + RED = Ansi::rgb(ChatFormatting::Red.color().unwrap()), + AQUA = Ansi::rgb(ChatFormatting::Aqua.color().unwrap()), BOLD = Ansi::BOLD, RESET = Ansi::RESET ) @@ -111,11 +111,11 @@ mod tests { "{BOLD}Hello {RESET}{DARK_BLUE}w{DARK_GREEN}o{DARK_AQUA}r{DARK_RED}l{DARK_PURPLE}d{RESET}", BOLD = Ansi::BOLD, RESET = Ansi::RESET, - DARK_BLUE = Ansi::rgb(ChatFormatting::DARK_BLUE.color.unwrap()), - DARK_GREEN = Ansi::rgb(ChatFormatting::DARK_GREEN.color.unwrap()), - DARK_AQUA = Ansi::rgb(ChatFormatting::DARK_AQUA.color.unwrap()), - DARK_RED = Ansi::rgb(ChatFormatting::DARK_RED.color.unwrap()), - DARK_PURPLE = Ansi::rgb(ChatFormatting::DARK_PURPLE.color.unwrap()) + DARK_BLUE = Ansi::rgb(ChatFormatting::DarkBlue.color().unwrap()), + DARK_GREEN = Ansi::rgb(ChatFormatting::DarkGreen.color().unwrap()), + DARK_AQUA = Ansi::rgb(ChatFormatting::DarkAqua.color().unwrap()), + DARK_RED = Ansi::rgb(ChatFormatting::DarkRed.color().unwrap()), + DARK_PURPLE = Ansi::rgb(ChatFormatting::DarkPurple.color().unwrap()) ) ); } |
