basalt_api/broadcast.rs
1//! Broadcast message types for cross-player communication.
2//!
3//! [`BroadcastMessage`] is the typed message sent through the server's
4//! broadcast channel. Each connected player's play loop receives every
5//! message and decides what to do with it (send packets, filter self).
6
7use basalt_types::Uuid;
8use basalt_types::nbt::NbtCompound;
9
10/// A profile property from the Mojang API (typically skin textures).
11///
12/// Sent in the `PlayerInfo` packet's add_player action as part of the
13/// game profile. The client uses the `textures` property to download
14/// and render the player's skin.
15#[derive(Debug, Clone)]
16pub struct ProfileProperty {
17 /// Property name (always "textures" for skins).
18 pub name: String,
19 /// Base64-encoded JSON containing the skin/cape URLs.
20 pub value: String,
21 /// Mojang signature for the property (base64-encoded).
22 pub signature: Option<String>,
23}
24
25/// A snapshot of a player's state at a point in time.
26///
27/// Used in broadcast messages and events to share a player's position
28/// and identity without holding locks on the player registry.
29#[derive(Debug, Clone)]
30pub struct PlayerSnapshot {
31 /// The player's display name.
32 pub username: String,
33 /// The player's UUID.
34 pub uuid: Uuid,
35 /// The player's unique entity ID.
36 pub entity_id: i32,
37 /// Current X coordinate.
38 pub x: f64,
39 /// Current Y coordinate.
40 pub y: f64,
41 /// Current Z coordinate.
42 pub z: f64,
43 /// Current yaw (horizontal look angle, degrees).
44 pub yaw: f32,
45 /// Current pitch (vertical look angle, degrees).
46 pub pitch: f32,
47 /// Mojang profile properties (skin textures).
48 pub skin_properties: Vec<ProfileProperty>,
49}
50
51/// A message broadcast from one player's task to all others.
52///
53/// Sent through the `broadcast::Sender` and received by each player's
54/// `broadcast::Receiver` in their play loop. Plugins use
55/// [`ChatContext::broadcast`](crate::context::ChatContext::broadcast)
56/// to send these.
57#[derive(Debug, Clone)]
58pub enum BroadcastMessage {
59 /// A chat message to display in all players' chat windows.
60 Chat {
61 /// The formatted text component as NBT.
62 content: NbtCompound,
63 },
64 /// A new player has joined the server.
65 PlayerJoined {
66 /// Snapshot of the joining player's state.
67 info: PlayerSnapshot,
68 },
69 /// A player has left the server.
70 PlayerLeft {
71 /// The leaving player's UUID (for PlayerRemove packet).
72 uuid: Uuid,
73 /// The leaving player's entity ID (for EntityDestroy packet).
74 entity_id: i32,
75 /// The leaving player's username (for chat message).
76 username: String,
77 },
78 /// A player moved or changed look direction.
79 EntityMoved {
80 /// The moving player's entity ID.
81 entity_id: i32,
82 /// New absolute X coordinate.
83 x: f64,
84 /// New absolute Y coordinate.
85 y: f64,
86 /// New absolute Z coordinate.
87 z: f64,
88 /// New yaw angle (degrees).
89 yaw: f32,
90 /// New pitch angle (degrees).
91 pitch: f32,
92 /// Whether the player is on the ground.
93 on_ground: bool,
94 },
95 /// A block was modified in the world.
96 BlockChanged {
97 /// Block X coordinate (absolute world coordinates).
98 x: i32,
99 /// Block Y coordinate (absolute world coordinates).
100 y: i32,
101 /// Block Z coordinate (absolute world coordinates).
102 z: i32,
103 /// The new block state ID.
104 block_state: i32,
105 },
106}