From 08958c2278b15ebeac8a964f392ebb792e479b61 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:31:40 -0600 Subject: Refactor azalea-protocol (#190) * start updating to 1.21.4 * fix block codegen and stop using block data from burger * rename packet related modules and structs to be simpler * ItemSlot -> ItemStack for more consistency with mojmap * .get() -> .into_packet() * simplify declare_state_packets by removing packet ids * rename read_from and write_into to azalea_read and azalea_write * rename McBufReadable and McBufWritable to AzaleaRead and AzaleaWrite * McBuf -> AzBuf * remove most uses of into_variant * update codegen and use resourcelocation names for packets * implement #[limit(i)] attribute for AzBuf derive macro * fixes for 1.21.4 * fix examples * update some physics code and fix ChatType * remove unused imports in codegen * re-add some things to migrate.py and update +mc version numbers automatically * downgrade to 1.21.3 lol --- azalea-registry/src/extra.rs | 34 +++++++++++++++++++++++ azalea-registry/src/lib.rs | 66 ++++++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 33 deletions(-) (limited to 'azalea-registry/src') diff --git a/azalea-registry/src/extra.rs b/azalea-registry/src/extra.rs index d5816e3b..4b7e89a4 100644 --- a/azalea-registry/src/extra.rs +++ b/azalea-registry/src/extra.rs @@ -97,3 +97,37 @@ enum JukeboxSong { CreatorMusicBox => "creator_music_box", } } + +registry! { +enum ChatType { + Chat => "chat", + SayCommand => "say_command", + MsgCommandIncoming => "msg_command_incoming", + MsgCommandOutgoing => "msg_command_outgoing", + TeamMsgCommandIncoming => "team_msg_command_incoming", + TeamMsgCommandOutgoing => "team_msg_command_outgoing", + EmoteCommand => "emote_command", +} +} +impl ChatType { + #[must_use] + pub fn chat_translation_key(self) -> &'static str { + match self { + ChatType::Chat => "chat.type.text", + ChatType::SayCommand => "chat.type.announcement", + ChatType::MsgCommandIncoming => "commands.message.display.incoming", + ChatType::MsgCommandOutgoing => "commands.message.display.outgoing", + ChatType::TeamMsgCommandIncoming => "chat.type.team.text", + ChatType::TeamMsgCommandOutgoing => "chat.type.team.sent", + ChatType::EmoteCommand => "chat.type.emote", + } + } + + #[must_use] + pub fn narrator_translation_key(self) -> &'static str { + match self { + ChatType::EmoteCommand => "chat.type.emote", + _ => "chat.type.text.narrate", + } + } +} diff --git a/azalea-registry/src/lib.rs b/azalea-registry/src/lib.rs index e4287b1c..e1fb2be5 100755 --- a/azalea-registry/src/lib.rs +++ b/azalea-registry/src/lib.rs @@ -11,11 +11,11 @@ pub mod tags; use std::fmt::{self, Debug}; use std::io::{Cursor, Write}; -use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; +use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_registry_macros::registry; pub use extra::*; -pub trait Registry: McBufReadable + McBufWritable +pub trait Registry: AzaleaRead + AzaleaWrite where Self: Sized, { @@ -26,11 +26,11 @@ where /// A registry that might not be present. This is transmitted as a single /// varint in the protocol. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct OptionalRegistry(Option); +pub struct OptionalRegistry(pub Option); -impl McBufReadable for OptionalRegistry { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result { - Ok(OptionalRegistry(match u32::var_read_from(buf)? { +impl AzaleaRead for OptionalRegistry { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { + Ok(OptionalRegistry(match u32::azalea_read_var(buf)? { 0 => None, value => Some( T::from_u32(value - 1) @@ -39,49 +39,49 @@ impl McBufReadable for OptionalRegistry { })) } } -impl McBufWritable for OptionalRegistry { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for OptionalRegistry { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match &self.0 { - None => 0u32.var_write_into(buf), - Some(value) => (value.to_u32() + 1).var_write_into(buf), + None => 0u32.azalea_write_var(buf), + Some(value) => (value.to_u32() + 1).azalea_write_var(buf), } } } /// A registry that will either take an ID or a resource location. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum CustomRegistry { +pub enum CustomRegistry { Direct(D), Custom(C), } -impl McBufReadable for CustomRegistry { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result { - let direct_registry = OptionalRegistry::::read_from(buf)?; +impl AzaleaRead for CustomRegistry { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { + let direct_registry = OptionalRegistry::::azalea_read(buf)?; if let Some(direct_registry) = direct_registry.0 { return Ok(CustomRegistry::Direct(direct_registry)); } - Ok(CustomRegistry::Custom(C::read_from(buf)?)) + Ok(CustomRegistry::Custom(C::azalea_read(buf)?)) } } -impl McBufWritable for CustomRegistry { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for CustomRegistry { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match self { CustomRegistry::Direct(direct_registry) => { // write the id + 1 - (direct_registry.to_u32() + 1).var_write_into(buf) + (direct_registry.to_u32() + 1).azalea_write_var(buf) } CustomRegistry::Custom(custom_registry) => { // write 0, then the custom registry - 0u32.var_write_into(buf)?; - custom_registry.write_into(buf) + 0u32.azalea_write_var(buf)?; + custom_registry.azalea_write(buf) } } } } #[derive(Clone, PartialEq)] -pub enum HolderSet { +pub enum HolderSet { Direct { contents: Vec, }, @@ -91,13 +91,13 @@ pub enum HolderSet }, } -impl McBufReadable +impl AzaleaRead for HolderSet { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result { - let size = i32::var_read_from(buf)? - 1; + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { + let size = i32::azalea_read_var(buf)? - 1; if size == -1 { - let key = ResourceLocation::read_from(buf)?; + let key = ResourceLocation::azalea_read(buf)?; Ok(Self::Named { key, contents: Vec::new(), @@ -105,32 +105,32 @@ impl McBufReadable } else { let mut contents = Vec::new(); for _ in 0..size { - contents.push(D::read_from(buf)?); + contents.push(D::azalea_read(buf)?); } Ok(Self::Direct { contents }) } } } -impl McBufWritable +impl AzaleaWrite for HolderSet { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match self { Self::Direct { contents } => { - (contents.len() as i32 + 1).var_write_into(buf)?; + (contents.len() as i32 + 1).azalea_write_var(buf)?; for item in contents { - item.write_into(buf)?; + item.azalea_write(buf)?; } } Self::Named { key, .. } => { - 0i32.var_write_into(buf)?; - key.write_into(buf)?; + 0i32.azalea_write_var(buf)?; + key.azalea_write(buf)?; } } Ok(()) } } -impl Debug +impl Debug for HolderSet { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -- cgit v1.2.3