From b83efa34cbb67e93f236a9f6abce596acd817ef5 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Wed, 7 May 2025 12:20:48 +0300 Subject: [PATCH] move play state to a packet handler --- src/main.rs | 4 +++- src/protocol/handler.rs | 15 ++++++--------- src/protocol/mod.rs | 2 +- src/protocol/play.rs | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1701297..0ab490b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use rust_mc_serv::{ data::text_component::TextComponent, event::{Listener, PacketHandler}, player::context::ClientContext, - protocol::ConnectionState, + protocol::{ConnectionState, play::PlayHandler}, start_server, }; @@ -153,6 +153,8 @@ fn main() { // Передается во все подключения let mut server = ServerContext::new(config); + server.add_packet_handler(Box::new(PlayHandler)); // Добавляем дефолтную обработку режима Play + server.add_listener(Box::new(ExampleListener)); // Добавляем пример листенера server.add_packet_handler(Box::new(ExamplePacketHandler)); // Добавляем пример пакет хандлера diff --git a/src/protocol/handler.rs b/src/protocol/handler.rs index c5cdd97..70301d5 100644 --- a/src/protocol/handler.rs +++ b/src/protocol/handler.rs @@ -8,12 +8,9 @@ use rust_mc_proto::{DataReader, DataWriter, Packet}; use crate::trigger_event; -use super::{ - ConnectionState, - packet_id::*, - play::{handle_configuration_state, handle_play_state}, -}; +use super::{ConnectionState, packet_id::*}; +// TODO: move brand to the config pub const BRAND: &str = "rust_mc_serv"; pub fn handle_connection( @@ -171,15 +168,15 @@ pub fn handle_connection( }, )?)?; - handle_configuration_state(client.clone())?; - client.write_packet(&Packet::empty(clientbound::configuration::FINISH))?; + + // На этом моменте пакет хандер ловит пакет и перед ним делает свое мракобесие + client.read_packet(&[serverbound::configuration::ACKNOWLEDGE_FINISH])?; client.set_state(ConnectionState::Play)?; // Мы перешли в режим Play - // Дальше работаем с режимом игры - handle_play_state(client)?; + // Тут работают уже приколы из пакет хандлера } _ => { // Тип подключения не рукопожатный diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 32f677a..1de2e3c 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -2,7 +2,7 @@ pub mod handler; pub mod packet_id; pub mod play; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum ConnectionState { Handshake, Status, diff --git a/src/protocol/play.rs b/src/protocol/play.rs index d3d00e9..d7c5610 100644 --- a/src/protocol/play.rs +++ b/src/protocol/play.rs @@ -10,10 +10,46 @@ use rust_mc_proto::{DataReader, DataWriter, Packet, read_packet}; use crate::{ ServerError, data::{ReadWriteNBT, text_component::TextComponent}, + event::PacketHandler, player::context::ClientContext, }; -use super::packet_id::*; +use super::{ConnectionState, packet_id::*}; + +pub struct PlayHandler; + +impl PacketHandler for PlayHandler { + fn on_outcoming_packet( + &self, + client: Arc, + packet: &mut Packet, + cancel: &mut bool, + state: ConnectionState, + ) -> Result<(), ServerError> { + if !*cancel // проверяем что пакет не отмененный, облегчаем себе задачу, ведь проверять айди наверняка сложней + && state == ConnectionState::Configuration // проверяем стейт, т.к айди могут быть одинаковыми между стейтами + && packet.id() == clientbound::configuration::FINISH + { + handle_configuration_state(client)?; // делаем наши грязные дела + } + + Ok(()) + } + + fn on_state( + &self, + client: Arc, + state: ConnectionState, + ) -> Result<(), ServerError> { + if state == ConnectionState::Play { + // перешли в режим плей, отлично! делаем дела + + handle_play_state(client)?; + } + + Ok(()) + } +} pub fn send_update_tags(client: Arc) -> Result<(), ServerError> { // TODO: rewrite this hardcode bullshit