use std::{net::{SocketAddr, TcpStream}, sync::{Arc, RwLock, RwLockWriteGuard}}; use itertools::Itertools; use rust_mc_proto::{MinecraftConnection, Packet}; use crate::{config::Config, data::ServerError, player::{ClientInfo, Handshake, PlayerInfo}}; pub struct ServerContext { pub config: Arc, listeners: Vec>, handlers: Vec> } impl ServerContext { pub fn new(config: Arc) -> ServerContext { ServerContext { config, listeners: Vec::new(), handlers: Vec::new() } } pub fn add_packet_handler(&mut self, handler: Box) { self.handlers.push(handler); } pub fn add_listener(&mut self, listener: Box) { self.listeners.push(listener); } pub fn packet_handlers( self: &Arc, sort_by: F ) -> Vec<&Box> where K: Ord, F: FnMut(&&Box) -> K { self.handlers.iter().sorted_by_key(sort_by).collect_vec() } pub fn listeners( self: &Arc, sort_by: F ) -> Vec<&Box> where K: Ord, F: FnMut(&&Box) -> K { self.listeners.iter().sorted_by_key(sort_by).collect_vec() } } pub struct ClientContext { pub server: Arc, pub conn: RwLock>, pub addr: SocketAddr, pub handshake: RwLock>, pub client_info: RwLock>, pub player_info: RwLock> } impl ClientContext { pub fn new( server: Arc, conn: MinecraftConnection ) -> ClientContext { ClientContext { server, addr: conn.get_ref().peer_addr().unwrap(), conn: RwLock::new(conn), handshake: RwLock::new(None), client_info: RwLock::new(None), player_info: RwLock::new(None) } } pub fn set_handshake(self: &Arc, handshake: Handshake) { *self.handshake.write().unwrap() = Some(handshake); } pub fn set_client_info(self: &Arc, client_info: ClientInfo) { *self.client_info.write().unwrap() = Some(client_info); } pub fn set_player_info(self: &Arc, player_info: PlayerInfo) { *self.player_info.write().unwrap() = Some(player_info); } pub fn handshake(self: &Arc) -> Option { self.handshake.read().unwrap().clone() } pub fn client_info(self: &Arc) -> Option { self.client_info.read().unwrap().clone() } pub fn player_info(self: &Arc) -> Option { self.player_info.read().unwrap().clone() } pub fn conn(self: &Arc) -> RwLockWriteGuard<'_, MinecraftConnection> { self.conn.write().unwrap() } } pub trait Listener: Sync + Send { fn on_status_priority(&self) -> i8 { 0 } fn on_status(&self, _: Arc, _: &mut String) -> Result<(), ServerError> { Ok(()) } } pub trait PacketHandler: Sync + Send { fn on_incoming_packet_priority(&self) -> i8 { 0 } fn on_incoming_packet(&self, _: Arc, _: &mut Packet) -> Result<(), ServerError> { Ok(()) } fn on_outcoming_packet_priority(&self) -> i8 { 0 } fn on_outcoming_packet(&self, _: Arc, _: &mut Packet) -> Result<(), ServerError> { Ok(()) } } pub struct Player { }