azalea-rs / azalea

A collection of Rust crates for making Minecraft bots, clients, and tools.
https://azalea.matdoes.dev
MIT License
361 stars 48 forks source link

Reading chat packet failure on all secure servers #129

Closed BenMcAvoy closed 6 months ago

BenMcAvoy commented 6 months ago

When trying to read chat messages from a typical secure server this error is presented:

2024-02-20T21:16:05.420008Z ERROR azalea_client::packet_handling::game: failed to read packet: Parse { packet_id: 105, packet_name: "ClientboundSystemChatPacket", backtrace: <disabled>, source: Custom("couldn't read nbt") }

Here is my code:

use common::{Message, Source};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use azalea::prelude::*;

use std::env;

lazy_static! {
    pub static ref SOURCE: Mutex<Source> = Mutex::new(Source::default());
}

const SERVER_IP: &str = "localhost";

#[tokio::main]
pub async fn main() {
    dotenvy::dotenv().unwrap();

    let email = env::var("MS_EMAIL").unwrap();

    // let account = Account::offline("chattr");
    let account = Account::microsoft(&email).await.unwrap();

    ClientBuilder::new()
        .set_handler(handle)
        .start(account.clone(), SERVER_IP)
        .await
        .unwrap();
}

#[derive(Default, Clone, Component)]
pub struct State {}

async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
    // match event {
    //     Event::Chat(m) => {
    if let Event::Chat(m) = event {
        if let Some(username) = m.username() {
            if username == bot.username() {
                return Ok(());
            }

            let message = Message {
                content: m.content(),
                identifier: username,
            };

            SOURCE.lock().add_message(message);
        }

        println!("{}", m.message().to_ansi());
    }
    // _ => {}
    // }

    Ok(())
}
BenMcAvoy commented 6 months ago

Fixed by updated to the unstable version of the lib (git)