aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/client_information.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-08-20 06:42:26 -1300
committermat <git@matdoes.dev>2025-08-20 22:42:40 +0300
commit63f15353e7c92c47b48df3aad7fa5c67012637c0 (patch)
tree0b7556cd3c80935fd8e323f0f11673857cb6ef46 /azalea-client/src/plugins/client_information.rs
parenta89cae5703abe0e103a17e9c931fb6132c448172 (diff)
downloadazalea-drasl-63f15353e7c92c47b48df3aad7fa5c67012637c0.tar.xz
split client information handling out of BrandPlugin and some other cleanup
Diffstat (limited to 'azalea-client/src/plugins/client_information.rs')
-rw-r--r--azalea-client/src/plugins/client_information.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/azalea-client/src/plugins/client_information.rs b/azalea-client/src/plugins/client_information.rs
new file mode 100644
index 00000000..d30b5329
--- /dev/null
+++ b/azalea-client/src/plugins/client_information.rs
@@ -0,0 +1,79 @@
+use azalea_protocol::{
+ common::client_information::ClientInformation,
+ packets::{config::s_client_information::ServerboundClientInformation, game},
+};
+use bevy_app::prelude::*;
+use bevy_ecs::prelude::*;
+use tracing::{debug, warn};
+
+use super::packet::config::SendConfigPacketEvent;
+use crate::{Client, brand::send_brand, packet::login::InLoginState};
+
+/// Send [`ServerboundClientInformation`] on join.
+pub struct ClientInformationPlugin;
+impl Plugin for ClientInformationPlugin {
+ fn build(&self, app: &mut App) {
+ app.add_systems(Update, send_client_information.after(send_brand));
+ }
+}
+
+pub fn send_client_information(
+ mut commands: Commands,
+ mut removed: RemovedComponents<InLoginState>,
+ query: Query<&ClientInformation>,
+) {
+ for entity in removed.read() {
+ let client_information = match query.get(entity).ok() {
+ Some(i) => i,
+ None => {
+ warn!(
+ "ClientInformation component was not set before leaving login state, using a default"
+ );
+ &ClientInformation::default()
+ }
+ };
+
+ debug!("Writing ClientInformation while in config state: {client_information:?}");
+ commands.trigger(SendConfigPacketEvent::new(
+ entity,
+ ServerboundClientInformation {
+ information: client_information.clone(),
+ },
+ ));
+ }
+}
+
+impl Client {
+ /// Tell the server we changed our game options (i.e. render distance, main
+ /// hand). If this is not set before the login packet, the default will
+ /// be sent.
+ ///
+ /// ```rust,no_run
+ /// # use azalea_client::{Client, ClientInformation};
+ /// # async fn example(bot: Client) -> Result<(), Box<dyn std::error::Error>> {
+ /// bot.set_client_information(ClientInformation {
+ /// view_distance: 2,
+ /// ..Default::default()
+ /// })
+ /// .await;
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub async fn set_client_information(&self, client_information: ClientInformation) {
+ {
+ let mut ecs = self.ecs.lock();
+ let mut client_information_mut = self.query::<&mut ClientInformation>(&mut ecs);
+ *client_information_mut = client_information.clone();
+ }
+
+ if self.logged_in() {
+ debug!(
+ "Sending client information (already logged in): {:?}",
+ client_information
+ );
+ self.write_packet(game::s_client_information::ServerboundClientInformation {
+ client_information,
+ });
+ }
+ }
+}