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

View file

@ -4,6 +4,7 @@ use azalea_core::ChunkPos;
use azalea_entity::Entity;
use nohash_hasher::IntMap;
#[derive(Debug)]
pub struct EntityStorage {
by_id: IntMap<u32, 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_core::BlockPos;
#[tokio::main]
async fn main() {
@ -20,10 +19,10 @@ async fn main() {
match e {
// TODO: have a "loaded" or "ready" event that fires when all chunks are loaded
Event::Login => {}
Event::Chat(p) => {
Event::Chat(_p) => {
let state = client.state.lock().await;
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);
// println!("{}", p.message.to_ansi(None));
// if p.message.to_ansi(None) == "<py5> ok" {