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

account.rs and client.rs

This commit is contained in:
mat 2022-06-18 16:54:49 -05:00
parent e32b8fabb7
commit fc3151f89d
5 changed files with 49 additions and 38 deletions

View file

@ -0,0 +1,27 @@
use crate::Client;
use azalea_protocol::{
packets::game::{
clientbound_player_chat_packet::ClientboundPlayerChatPacket,
clientbound_system_chat_packet::ClientboundSystemChatPacket,
},
ServerAddress,
};
use std::fmt::Debug;
///! Connect to Minecraft servers.
/// Something that can join Minecraft servers.
pub struct Account {
pub username: String,
}
impl Account {
pub fn offline(username: &str) -> Self {
Self {
username: username.to_string(),
}
}
pub async fn join(&self, address: &ServerAddress) -> Result<Client, String> {
Client::join(self, address).await
}
}

View file

@ -1,5 +1,5 @@
use crate::Player; use crate::{Account, Player};
use azalea_core::{resource_location::ResourceLocation, ChunkPos, EntityPos}; use azalea_core::{resource_location::ResourceLocation, ChunkPos};
use azalea_entity::Entity; use azalea_entity::Entity;
use azalea_protocol::{ use azalea_protocol::{
connect::{GameConnection, HandshakeConnection}, connect::{GameConnection, HandshakeConnection},
@ -25,25 +25,16 @@ use std::{fmt::Debug, sync::Arc};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex; use tokio::sync::Mutex;
///! Connect to Minecraft servers.
/// Something that can join Minecraft servers.
pub struct Account {
username: String,
}
#[derive(Default)] #[derive(Default)]
pub struct ClientState { pub struct ClientState {
pub player: Player, pub player: Player,
pub world: Option<World>, pub world: Option<World>,
} }
/// A player that you can control that is currently in a Minecraft server. #[derive(Debug, Clone)]
pub struct Client { pub enum Event {
event_receiver: UnboundedReceiver<Event>, Login,
pub conn: Arc<Mutex<GameConnection>>, Chat(ChatPacket),
pub state: Arc<Mutex<ClientState>>,
// game_loop
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -61,17 +52,20 @@ pub enum ChatPacket {
// } // }
// } // }
#[derive(Debug, Clone)] /// A player that you can control that is currently in a Minecraft server.
pub enum Event { pub struct Client {
Login, event_receiver: UnboundedReceiver<Event>,
Chat(ChatPacket), pub conn: Arc<Mutex<GameConnection>>,
pub state: Arc<Mutex<ClientState>>,
// game_loop
} }
/// Whether we should ignore errors when decoding packets. /// Whether we should ignore errors when decoding packets.
const IGNORE_ERRORS: bool = false; const IGNORE_ERRORS: bool = !cfg!(debug_assertions);
impl Client { impl Client {
async fn join(account: &Account, address: &ServerAddress) -> Result<Self, String> { /// Connect to a Minecraft server with an account.
pub async fn join(account: &Account, address: &ServerAddress) -> Result<Self, String> {
let resolved_address = resolver::resolve_address(address).await?; let resolved_address = resolver::resolve_address(address).await?;
let mut conn = HandshakeConnection::new(&resolved_address).await?; let mut conn = HandshakeConnection::new(&resolved_address).await?;
@ -469,15 +463,3 @@ impl Client {
self.event_receiver.recv().await self.event_receiver.recv().await
} }
} }
impl Account {
pub fn offline(username: &str) -> Self {
Self {
username: username.to_string(),
}
}
pub async fn join(&self, address: &ServerAddress) -> Result<Client, String> {
Client::join(self, address).await
}
}

View file

@ -1,10 +1,12 @@
//! Significantly abstract azalea-protocol so it's actually useable for bots. //! Significantly abstract azalea-protocol so it's actually useable for bots.
mod connect; mod account;
mod client;
pub mod ping; pub mod ping;
mod player; mod player;
pub use connect::{Account, Client, Event}; pub use account::Account;
pub use client::{Client, Event};
pub use player::Player; pub use player::Player;
#[cfg(test)] #[cfg(test)]

View file

@ -4,6 +4,7 @@ use azalea_core::ChunkPos;
use azalea_entity::Entity; use azalea_entity::Entity;
use nohash_hasher::IntMap; use nohash_hasher::IntMap;
#[derive(Debug)]
pub struct EntityStorage { pub struct EntityStorage {
by_id: IntMap<u32, Entity>, by_id: IntMap<u32, Entity>,
// TODO: this doesn't work yet (should be updated in the set_pos method in azalea-entity) // TODO: this doesn't work yet (should be updated in the set_pos method in azalea-entity)

View file

@ -1,5 +1,4 @@
use azalea_client::{Account, Event}; use azalea_client::{Account, Event};
use azalea_core::BlockPos;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -20,10 +19,10 @@ async fn main() {
match e { match e {
// TODO: have a "loaded" or "ready" event that fires when all chunks are loaded // TODO: have a "loaded" or "ready" event that fires when all chunks are loaded
Event::Login => {} Event::Login => {}
Event::Chat(p) => { Event::Chat(_p) => {
let state = client.state.lock().await; let state = client.state.lock().await;
let world = state.world.as_ref().unwrap(); let world = state.world.as_ref().unwrap();
println!("{:?}", state.world.entities.get_player(player)); println!("{:?}", world.entities);
// world.get_block_state(state.player.entity.pos); // world.get_block_state(state.player.entity.pos);
// println!("{}", p.message.to_ansi(None)); // println!("{}", p.message.to_ansi(None));
// if p.message.to_ansi(None) == "<py5> ok" { // if p.message.to_ansi(None) == "<py5> ok" {