aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/common
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2024-11-27 19:31:40 -0600
committerGitHub <noreply@github.com>2024-11-27 19:31:40 -0600
commit08958c2278b15ebeac8a964f392ebb792e479b61 (patch)
tree4ae3664cea38d7fd1a8f1e95ed06fac04ffe519e /azalea-protocol/src/common
parent139d77d3c2b0922fba5e9d4fa2bd9819d78bd773 (diff)
downloadazalea-drasl-08958c2278b15ebeac8a964f392ebb792e479b61.tar.xz
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
Diffstat (limited to 'azalea-protocol/src/common')
-rw-r--r--azalea-protocol/src/common/client_information.rs187
-rw-r--r--azalea-protocol/src/common/mod.rs4
-rw-r--r--azalea-protocol/src/common/server_links.rs28
3 files changed, 219 insertions, 0 deletions
diff --git a/azalea-protocol/src/common/client_information.rs b/azalea-protocol/src/common/client_information.rs
new file mode 100644
index 00000000..6f5e05e2
--- /dev/null
+++ b/azalea-protocol/src/common/client_information.rs
@@ -0,0 +1,187 @@
+use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite};
+use azalea_core::bitset::FixedBitSet;
+use bevy_ecs::component::Component;
+
+/// A component that contains some of the "settings" for this client that are
+/// sent to the server, such as render distance. This is only present on local
+/// players.
+#[derive(Clone, Debug, AzBuf, PartialEq, Eq, Component)]
+pub struct ClientInformation {
+ /// The locale of the client.
+ pub language: String,
+ /// The view distance of the client in chunks, same as the render distance
+ /// in-game.
+ pub view_distance: u8,
+ /// The types of chat messages the client wants to receive. Note that many
+ /// servers ignore this.
+ pub chat_visibility: ChatVisibility,
+ /// Whether the messages sent from the server should have colors. Note that
+ /// many servers ignore this and always send colored messages.
+ pub chat_colors: bool,
+ pub model_customization: ModelCustomization,
+ pub main_hand: HumanoidArm,
+ pub text_filtering_enabled: bool,
+ /// Whether the client should show up as "Anonymous Player" in the server
+ /// list.
+ pub allows_listing: bool,
+ pub particle_status: ParticleStatus,
+}
+
+impl Default for ClientInformation {
+ fn default() -> Self {
+ Self {
+ language: "en_us".to_string(),
+ view_distance: 8,
+ chat_visibility: ChatVisibility::default(),
+ chat_colors: true,
+ model_customization: ModelCustomization::default(),
+ main_hand: HumanoidArm::Right,
+ text_filtering_enabled: false,
+ allows_listing: false,
+ particle_status: ParticleStatus::default(),
+ }
+ }
+}
+
+#[derive(AzBuf, Clone, Copy, Debug, PartialEq, Eq, Default)]
+pub enum ChatVisibility {
+ /// All chat messages should be sent to the client.
+ #[default]
+ Full = 0,
+ /// Chat messages from other players should be not sent to the client, only
+ /// messages from the server like "Player joined the game" should be sent.
+ System = 1,
+ /// No chat messages should be sent to the client.
+ Hidden = 2,
+}
+
+#[derive(AzBuf, Clone, Copy, Debug, PartialEq, Eq, Default)]
+pub enum HumanoidArm {
+ Left = 0,
+ #[default]
+ Right = 1,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct ModelCustomization {
+ pub cape: bool,
+ pub jacket: bool,
+ pub left_sleeve: bool,
+ pub right_sleeve: bool,
+ pub left_pants: bool,
+ pub right_pants: bool,
+ pub hat: bool,
+}
+
+#[derive(AzBuf, Clone, Copy, Debug, PartialEq, Eq, Default)]
+pub enum ParticleStatus {
+ #[default]
+ All,
+ Decreased,
+ Minimal,
+}
+
+impl Default for ModelCustomization {
+ fn default() -> Self {
+ Self {
+ cape: true,
+ jacket: true,
+ left_sleeve: true,
+ right_sleeve: true,
+ left_pants: true,
+ right_pants: true,
+ hat: true,
+ }
+ }
+}
+
+impl AzaleaRead for ModelCustomization {
+ fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
+ let set = FixedBitSet::<7>::azalea_read(buf)?;
+ Ok(Self {
+ cape: set.index(0),
+ jacket: set.index(1),
+ left_sleeve: set.index(2),
+ right_sleeve: set.index(3),
+ left_pants: set.index(4),
+ right_pants: set.index(5),
+ hat: set.index(6),
+ })
+ }
+}
+
+impl AzaleaWrite for ModelCustomization {
+ fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
+ let mut set = FixedBitSet::<7>::new();
+ if self.cape {
+ set.set(0);
+ }
+ if self.jacket {
+ set.set(1);
+ }
+ if self.left_sleeve {
+ set.set(2);
+ }
+ if self.right_sleeve {
+ set.set(3);
+ }
+ if self.left_pants {
+ set.set(4);
+ }
+ if self.right_pants {
+ set.set(5);
+ }
+ if self.hat {
+ set.set(6);
+ }
+ set.azalea_write(buf)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::io::Cursor;
+
+ use azalea_buf::{AzaleaRead, AzaleaWrite};
+
+ use super::*;
+
+ #[test]
+ fn test_client_information() {
+ {
+ let data = ClientInformation::default();
+ let mut buf = Vec::new();
+ data.azalea_write(&mut buf).unwrap();
+ let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
+
+ let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
+ assert_eq!(read_data, data);
+ }
+
+ let data = ClientInformation {
+ language: "en_gb".to_string(),
+ view_distance: 24,
+ chat_visibility: ChatVisibility::Hidden,
+ chat_colors: false,
+ model_customization: ModelCustomization {
+ cape: false,
+ jacket: false,
+ left_sleeve: true,
+ right_sleeve: false,
+ left_pants: true,
+ right_pants: false,
+ hat: true,
+ },
+ main_hand: HumanoidArm::Left,
+ text_filtering_enabled: true,
+ allows_listing: true,
+ particle_status: ParticleStatus::Decreased,
+ };
+ let mut buf = Vec::new();
+ data.azalea_write(&mut buf).unwrap();
+ let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
+
+ let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
+ assert_eq!(read_data, data);
+ }
+}
diff --git a/azalea-protocol/src/common/mod.rs b/azalea-protocol/src/common/mod.rs
new file mode 100644
index 00000000..da535b48
--- /dev/null
+++ b/azalea-protocol/src/common/mod.rs
@@ -0,0 +1,4 @@
+//! Some serializable data types that are used by several packets.
+
+pub mod client_information;
+pub mod server_links;
diff --git a/azalea-protocol/src/common/server_links.rs b/azalea-protocol/src/common/server_links.rs
new file mode 100644
index 00000000..4aed98f3
--- /dev/null
+++ b/azalea-protocol/src/common/server_links.rs
@@ -0,0 +1,28 @@
+use azalea_buf::AzBuf;
+use azalea_chat::FormattedText;
+
+#[derive(Clone, Debug, AzBuf)]
+pub struct ServerLinkEntry {
+ pub kind: ServerLinkKind,
+ pub link: String,
+}
+
+#[derive(Clone, Debug, AzBuf)]
+pub enum ServerLinkKind {
+ Known(KnownLinkKind),
+ Component(FormattedText),
+}
+
+#[derive(Clone, Copy, Debug, AzBuf)]
+pub enum KnownLinkKind {
+ BugReport,
+ CommunityGuidelines,
+ Support,
+ Status,
+ Feedback,
+ Community,
+ Website,
+ Forums,
+ News,
+ Announcements,
+}