1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

more docs

This commit is contained in:
mat 2022-10-30 15:28:19 -05:00
parent c65e1fc660
commit 329f8b1784
32 changed files with 78 additions and 32 deletions

View file

@ -1,4 +1,4 @@
use crate::{component::Component, style::Style}; use crate::{style::Style, Component};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct BaseComponent { pub struct BaseComponent {

View file

@ -13,6 +13,7 @@ use crate::{
translatable_component::{StringOrComponent, TranslatableComponent}, translatable_component::{StringOrComponent, TranslatableComponent},
}; };
/// A chat component, basically anything you can see in chat.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Component { pub enum Component {
Text(TextComponent), Text(TextComponent),
@ -57,7 +58,22 @@ impl Component {
Ok(None) 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 { pub fn to_ansi(&self, default_style: Option<&Style>) -> String {
// default the default_style to white if it's not set // default the default_style to white if it's not set
let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE); let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE);

View file

@ -5,7 +5,9 @@
extern crate lazy_static; extern crate lazy_static;
pub mod base_component; pub mod base_component;
pub mod component; mod component;
pub mod style; pub mod style;
pub mod text_component; pub mod text_component;
pub mod translatable_component; pub mod translatable_component;
pub use component::Component;

View file

@ -1,7 +1,8 @@
use std::fmt::Display; 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)] #[derive(Clone, Debug)]
pub struct TextComponent { pub struct TextComponent {
pub base: BaseComponent, pub base: BaseComponent,

View file

@ -1,8 +1,7 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use crate::{ use crate::{
base_component::BaseComponent, component::Component, style::Style, base_component::BaseComponent, style::Style, text_component::TextComponent, Component,
text_component::TextComponent,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -11,6 +10,7 @@ pub enum StringOrComponent {
Component(Component), Component(Component),
} }
/// A message whose content depends on the client's language.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TranslatableComponent { pub struct TranslatableComponent {
pub base: BaseComponent, pub base: BaseComponent,

View file

@ -1,6 +1,6 @@
use azalea_chat::{ use azalea_chat::{
component::Component,
style::{Ansi, ChatFormatting, TextColor}, style::{Ansi, ChatFormatting, TextColor},
Component,
}; };
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;

View file

@ -6,6 +6,18 @@ use uuid::Uuid;
/// Something that can join Minecraft servers. /// Something that can join Minecraft servers.
/// ///
/// To join a server using this account, use [`crate::Client::join`]. /// 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)] #[derive(Clone, Debug)]
pub struct Account { pub struct Account {
/// The Minecraft username of the account. /// The Minecraft username of the account.

View file

@ -1,6 +1,6 @@
use crate::{movement::MoveDirection, Account, Player}; use crate::{movement::MoveDirection, Account, Player};
use azalea_auth::game_profile::GameProfile; use azalea_auth::game_profile::GameProfile;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_core::{ChunkPos, ResourceLocation, Vec3}; use azalea_core::{ChunkPos, ResourceLocation, Vec3};
use azalea_protocol::{ use azalea_protocol::{
connect::{Connection, ConnectionError, ReadConnection, WriteConnection}, connect::{Connection, ConnectionError, ReadConnection, WriteConnection},
@ -62,6 +62,7 @@ pub enum Event {
Packet(Box<ClientboundGamePacket>), Packet(Box<ClientboundGamePacket>),
} }
/// A chat packet, either a system message or a chat message.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum ChatPacket { pub enum ChatPacket {
System(ClientboundSystemChatPacket), System(ClientboundSystemChatPacket),
@ -69,6 +70,7 @@ pub enum ChatPacket {
} }
impl ChatPacket { impl ChatPacket {
/// Get the message shown in chat for this packet.
pub fn message(&self) -> Component { pub fn message(&self) -> Component {
match self { match self {
ChatPacket::System(p) => p.content.clone(), ChatPacket::System(p) => p.content.clone(),

View file

@ -14,7 +14,7 @@ pub mod ping;
mod player; mod player;
pub use account::Account; 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 movement::MoveDirection;
pub use player::Player; pub use player::Player;

View file

@ -1,7 +1,7 @@
use azalea_buf::{ use azalea_buf::{
BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable,
}; };
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::io::Cursor; use std::io::Cursor;
use std::io::Write; use std::io::Write;

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,6 +1,6 @@
use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,7 +1,7 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::{ use azalea_chat::{
component::Component,
translatable_component::{StringOrComponent, TranslatableComponent}, translatable_component::{StringOrComponent, TranslatableComponent},
Component,
}; };
use azalea_core::BitSet; use azalea_core::BitSet;
use azalea_crypto::{MessageSignature, SignedMessageHeader}; use azalea_crypto::{MessageSignature, SignedMessageHeader};

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
/// Used to send a respawn screen. /// Used to send a respawn screen.

View file

@ -1,7 +1,7 @@
use crate::packets::login::serverbound_hello_packet::ProfilePublicKeyData; use crate::packets::login::serverbound_hello_packet::ProfilePublicKeyData;
use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufWritable}; use azalea_buf::{McBufReadable, McBufWritable};
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use uuid::Uuid; use uuid::Uuid;

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};

View file

@ -1,5 +1,5 @@
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; 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 azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, McBuf, ClientboundGamePacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_core::{ResourceLocation, Slot}; use azalea_core::{ResourceLocation, Slot};
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::collections::HashMap; use std::collections::HashMap;

View file

@ -1,5 +1,5 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundLoginPacket; use azalea_protocol_macros::ClientboundLoginPacket;
#[derive(Clone, Debug, McBuf, ClientboundLoginPacket)] #[derive(Clone, Debug, McBuf, ClientboundLoginPacket)]

View file

@ -1,5 +1,5 @@
use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_protocol_macros::ClientboundStatusPacket; use azalea_protocol_macros::ClientboundStatusPacket;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;

View file

@ -1,6 +1,6 @@
use azalea_buf::{BufReadError, McBufVarReadable}; use azalea_buf::{BufReadError, McBufVarReadable};
use azalea_buf::{McBuf, McBufReadable, McBufWritable}; use azalea_buf::{McBuf, McBufReadable, McBufWritable};
use azalea_chat::component::Component; use azalea_chat::Component;
use azalea_core::{BlockPos, Direction, GlobalPos, Particle, Slot}; use azalea_core::{BlockPos, Direction, GlobalPos, Particle, Slot};
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use uuid::Uuid; use uuid::Uuid;

View file

@ -94,8 +94,10 @@ where
/// The address of the server that we're connecting to. This can be a /// The address of the server that we're connecting to. This can be a
/// `&str`, [`ServerAddress`], or anything that implements /// `&str`, [`ServerAddress`], or anything that implements
/// `TryInto<ServerAddress>`. /// `TryInto<ServerAddress>`.
///
/// [`ServerAddress`]: azalea_protocol::ServerAddress
pub address: A, pub address: A,
/// The account that's going to join the server, /// The account that's going to join the server.
pub account: Account, pub account: Account,
/// A list of plugins that are going to be used. Plugins are external /// A list of plugins that are going to be used. Plugins are external
/// crates that add extra functionality to Azalea. /// crates that add extra functionality to Azalea.
@ -116,6 +118,17 @@ where
/// ``` /// ```
pub state: S, pub state: S,
/// The function that's called whenever we get an event. /// 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<Fut, S>, pub handle: HandleFn<Fut, S>,
} }

View file

@ -45,7 +45,7 @@ def burger_type_to_rust_type(burger_type, field_name: Optional[str] = None, inst
elif burger_type == 'chatcomponent': elif burger_type == 'chatcomponent':
field_type_rs = 'Component' field_type_rs = 'Component'
uses.add('azalea_chat::component::Component') uses.add('azalea_chat::Component')
elif burger_type == 'identifier': elif burger_type == 'identifier':
field_type_rs = 'ResourceLocation' field_type_rs = 'ResourceLocation'
uses.add('azalea_core::ResourceLocation') uses.add('azalea_core::ResourceLocation')