diff --git a/azalea-chat/src/base_component.rs b/azalea-chat/src/base_component.rs index fa39a11c..c2f3513d 100755 --- a/azalea-chat/src/base_component.rs +++ b/azalea-chat/src/base_component.rs @@ -1,4 +1,4 @@ -use crate::{component::Component, style::Style}; +use crate::{style::Style, Component}; #[derive(Clone, Debug)] pub struct BaseComponent { diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 6dd11084..4df3796f 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -13,6 +13,7 @@ use crate::{ translatable_component::{StringOrComponent, TranslatableComponent}, }; +/// A chat component, basically anything you can see in chat. #[derive(Clone, Debug)] pub enum Component { Text(TextComponent), @@ -57,7 +58,22 @@ impl Component { Ok(None) } - /// Convert this component into an ansi string + /// Convert this component into an + /// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code), so you + /// can print it to your terminal and get styling. + /// + /// # Examples + /// + /// ```rust + /// use azalea_chat::Component; + /// + /// let component = Component::deserialize(&serde_json::json!({ + /// "text": "Hello, world!", + /// "color": "red", + /// })).unwrap(); + /// + /// println!("{}", component.to_ansi()); + /// ``` pub fn to_ansi(&self, default_style: Option<&Style>) -> String { // default the default_style to white if it's not set let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE); diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs index b7035e13..01f718eb 100755 --- a/azalea-chat/src/lib.rs +++ b/azalea-chat/src/lib.rs @@ -5,7 +5,9 @@ extern crate lazy_static; pub mod base_component; -pub mod component; +mod component; pub mod style; pub mod text_component; pub mod translatable_component; + +pub use component::Component; diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index 96eef08e..46cb0951 100644 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -1,7 +1,8 @@ use std::fmt::Display; -use crate::{base_component::BaseComponent, component::Component, style::ChatFormatting}; +use crate::{base_component::BaseComponent, style::ChatFormatting, Component}; +/// A component that contains text that's the same in all locales. #[derive(Clone, Debug)] pub struct TextComponent { pub base: BaseComponent, diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs index 7c01819b..d187adda 100644 --- a/azalea-chat/src/translatable_component.rs +++ b/azalea-chat/src/translatable_component.rs @@ -1,8 +1,7 @@ use std::fmt::{self, Display, Formatter}; use crate::{ - base_component::BaseComponent, component::Component, style::Style, - text_component::TextComponent, + base_component::BaseComponent, style::Style, text_component::TextComponent, Component, }; #[derive(Clone, Debug)] @@ -11,6 +10,7 @@ pub enum StringOrComponent { Component(Component), } +/// A message whose content depends on the client's language. #[derive(Clone, Debug)] pub struct TranslatableComponent { pub base: BaseComponent, diff --git a/azalea-chat/tests/integration_test.rs b/azalea-chat/tests/integration_test.rs index c2be960e..cc976888 100755 --- a/azalea-chat/tests/integration_test.rs +++ b/azalea-chat/tests/integration_test.rs @@ -1,6 +1,6 @@ use azalea_chat::{ - component::Component, style::{Ansi, ChatFormatting, TextColor}, + Component, }; use serde::Deserialize; use serde_json::Value; diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 715f2fba..2fedc4f5 100644 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -6,6 +6,18 @@ use uuid::Uuid; /// Something that can join Minecraft servers. /// /// To join a server using this account, use [`crate::Client::join`]. +/// +/// # Examples +/// +/// ```rust,no_run +/// use azalea_client::Account; +/// +/// # #[tokio::main] +/// # async fn main() { +/// let account = Account::microsoft("example@example.com").await; +/// // or Account::offline("example"); +/// # } +/// ``` #[derive(Clone, Debug)] pub struct Account { /// The Minecraft username of the account. diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index ab52b65e..cb87c250 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -1,6 +1,6 @@ use crate::{movement::MoveDirection, Account, Player}; use azalea_auth::game_profile::GameProfile; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_core::{ChunkPos, ResourceLocation, Vec3}; use azalea_protocol::{ connect::{Connection, ConnectionError, ReadConnection, WriteConnection}, @@ -62,6 +62,7 @@ pub enum Event { Packet(Box), } +/// A chat packet, either a system message or a chat message. #[derive(Debug, Clone)] pub enum ChatPacket { System(ClientboundSystemChatPacket), @@ -69,6 +70,7 @@ pub enum ChatPacket { } impl ChatPacket { + /// Get the message shown in chat for this packet. pub fn message(&self) -> Component { match self { ChatPacket::System(p) => p.content.clone(), diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 265fbf8b..c32bd212 100755 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -14,7 +14,7 @@ pub mod ping; mod player; pub use account::Account; -pub use client::{Client, ClientInformation, Event, JoinError}; +pub use client::{ChatPacket, Client, ClientInformation, Event, JoinError}; pub use movement::MoveDirection; pub use player::Player; diff --git a/azalea-protocol/src/packets/game/clientbound_boss_event_packet.rs b/azalea-protocol/src/packets/game/clientbound_boss_event_packet.rs index 3bcf10d2..59378d3e 100644 --- a/azalea-protocol/src/packets/game/clientbound_boss_event_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_boss_event_packet.rs @@ -1,7 +1,7 @@ use azalea_buf::{ BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, }; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; use std::io::Cursor; use std::io::Write; diff --git a/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs b/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs index f6343f73..5a72ae33 100644 --- a/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs index bc4b83cb..ecff0278 100644 --- a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_map_item_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_map_item_data_packet.rs index 4ce71a12..07e49687 100644 --- a/azalea-protocol/src/packets/game/clientbound_map_item_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_map_item_data_packet.rs @@ -1,6 +1,6 @@ use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; use std::io::{Cursor, Write}; diff --git a/azalea-protocol/src/packets/game/clientbound_open_screen_packet.rs b/azalea-protocol/src/packets/game/clientbound_open_screen_packet.rs index 521975af..f127f587 100644 --- a/azalea-protocol/src/packets/game/clientbound_open_screen_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_open_screen_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs index 3f75e74b..fedc81df 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs @@ -1,7 +1,7 @@ use azalea_buf::McBuf; use azalea_chat::{ - component::Component, translatable_component::{StringOrComponent, TranslatableComponent}, + Component, }; use azalea_core::BitSet; use azalea_crypto::{MessageSignature, SignedMessageHeader}; diff --git a/azalea-protocol/src/packets/game/clientbound_player_combat_kill_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_combat_kill_packet.rs index f1294f2e..fa547af1 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_combat_kill_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_combat_kill_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; /// Used to send a respawn screen. diff --git a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs index 885148a5..84218842 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,7 +1,7 @@ use crate::packets::login::serverbound_hello_packet::ProfilePublicKeyData; use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable}; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; use std::io::{Cursor, Write}; use uuid::Uuid; diff --git a/azalea-protocol/src/packets/game/clientbound_resource_pack_packet.rs b/azalea-protocol/src/packets/game/clientbound_resource_pack_packet.rs index e31ebaa4..73ade728 100644 --- a/azalea-protocol/src/packets/game/clientbound_resource_pack_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_resource_pack_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs index b1147305..2305cf32 100644 --- a/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_set_action_bar_text_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_action_bar_text_packet.rs index aefe072f..e279e33c 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_action_bar_text_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_action_bar_text_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_set_objective_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_objective_packet.rs index 0e7e334d..ad75a1c3 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_objective_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_objective_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; use std::io::{Cursor, Write}; diff --git a/azalea-protocol/src/packets/game/clientbound_set_player_team_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_player_team_packet.rs index a0754ddd..2550a98c 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_player_team_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_player_team_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; -use azalea_chat::{component::Component, style::ChatFormatting}; +use azalea_chat::{style::ChatFormatting, Component}; use azalea_protocol_macros::ClientboundGamePacket; use std::io::{Cursor, Write}; diff --git a/azalea-protocol/src/packets/game/clientbound_set_subtitle_text_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_subtitle_text_packet.rs index d1a3b281..b7d1e7a4 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_subtitle_text_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_subtitle_text_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_set_title_text_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_title_text_packet.rs index c6e5f0e8..a9185e1a 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_title_text_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_title_text_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs index 4e5222b7..a3319721 100644 --- a/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_tab_list_packet.rs b/azalea-protocol/src/packets/game/clientbound_tab_list_packet.rs index 49f357b2..94f23241 100644 --- a/azalea-protocol/src/packets/game/clientbound_tab_list_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_tab_list_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundGamePacket; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs index eb52e133..5f63dd1a 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_core::{ResourceLocation, Slot}; use azalea_protocol_macros::ClientboundGamePacket; use std::collections::HashMap; diff --git a/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs index 2b8144ef..983dc31b 100644 --- a/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundLoginPacket; #[derive(Clone, Debug, McBuf, ClientboundLoginPacket)] diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index f8b46b8d..64ea14f9 100755 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_protocol_macros::ClientboundStatusPacket; use serde::Deserialize; use serde_json::Value; diff --git a/azalea-world/src/entity/data.rs b/azalea-world/src/entity/data.rs index 0f90c790..c73a84d6 100644 --- a/azalea-world/src/entity/data.rs +++ b/azalea-world/src/entity/data.rs @@ -1,6 +1,6 @@ use azalea_buf::{BufReadError, McBufVarReadable}; use azalea_buf::{McBuf, McBufReadable, McBufWritable}; -use azalea_chat::component::Component; +use azalea_chat::Component; use azalea_core::{BlockPos, Direction, GlobalPos, Particle, Slot}; use std::io::{Cursor, Write}; use uuid::Uuid; diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 2d0344fc..d9465062 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -94,8 +94,10 @@ where /// The address of the server that we're connecting to. This can be a /// `&str`, [`ServerAddress`], or anything that implements /// `TryInto`. + /// + /// [`ServerAddress`]: azalea_protocol::ServerAddress pub address: A, - /// The account that's going to join the server, + /// The account that's going to join the server. pub account: Account, /// A list of plugins that are going to be used. Plugins are external /// crates that add extra functionality to Azalea. @@ -116,6 +118,17 @@ where /// ``` pub state: S, /// The function that's called whenever we get an event. + /// + /// # Examples + /// + /// ```rust + /// use anyhow::Result; + /// use azalea::prelude::*; + /// + /// async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { + /// Ok(()) + /// } + /// ``` pub handle: HandleFn, } diff --git a/codegen/lib/code/utils.py b/codegen/lib/code/utils.py index 635dff0b..66b18eed 100644 --- a/codegen/lib/code/utils.py +++ b/codegen/lib/code/utils.py @@ -45,7 +45,7 @@ def burger_type_to_rust_type(burger_type, field_name: Optional[str] = None, inst elif burger_type == 'chatcomponent': field_type_rs = 'Component' - uses.add('azalea_chat::component::Component') + uses.add('azalea_chat::Component') elif burger_type == 'identifier': field_type_rs = 'ResourceLocation' uses.add('azalea_core::ResourceLocation')