move play state to a packet handler
This commit is contained in:
parent
a7636c4028
commit
b83efa34cb
@ -9,7 +9,7 @@ use rust_mc_serv::{
|
|||||||
data::text_component::TextComponent,
|
data::text_component::TextComponent,
|
||||||
event::{Listener, PacketHandler},
|
event::{Listener, PacketHandler},
|
||||||
player::context::ClientContext,
|
player::context::ClientContext,
|
||||||
protocol::ConnectionState,
|
protocol::{ConnectionState, play::PlayHandler},
|
||||||
start_server,
|
start_server,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -153,6 +153,8 @@ fn main() {
|
|||||||
// Передается во все подключения
|
// Передается во все подключения
|
||||||
let mut server = ServerContext::new(config);
|
let mut server = ServerContext::new(config);
|
||||||
|
|
||||||
|
server.add_packet_handler(Box::new(PlayHandler)); // Добавляем дефолтную обработку режима Play
|
||||||
|
|
||||||
server.add_listener(Box::new(ExampleListener)); // Добавляем пример листенера
|
server.add_listener(Box::new(ExampleListener)); // Добавляем пример листенера
|
||||||
server.add_packet_handler(Box::new(ExamplePacketHandler)); // Добавляем пример пакет хандлера
|
server.add_packet_handler(Box::new(ExamplePacketHandler)); // Добавляем пример пакет хандлера
|
||||||
|
|
||||||
|
@ -8,12 +8,9 @@ use rust_mc_proto::{DataReader, DataWriter, Packet};
|
|||||||
|
|
||||||
use crate::trigger_event;
|
use crate::trigger_event;
|
||||||
|
|
||||||
use super::{
|
use super::{ConnectionState, packet_id::*};
|
||||||
ConnectionState,
|
|
||||||
packet_id::*,
|
|
||||||
play::{handle_configuration_state, handle_play_state},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// TODO: move brand to the config
|
||||||
pub const BRAND: &str = "rust_mc_serv";
|
pub const BRAND: &str = "rust_mc_serv";
|
||||||
|
|
||||||
pub fn handle_connection(
|
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.write_packet(&Packet::empty(clientbound::configuration::FINISH))?;
|
||||||
|
|
||||||
|
// На этом моменте пакет хандер ловит пакет и перед ним делает свое мракобесие
|
||||||
|
|
||||||
client.read_packet(&[serverbound::configuration::ACKNOWLEDGE_FINISH])?;
|
client.read_packet(&[serverbound::configuration::ACKNOWLEDGE_FINISH])?;
|
||||||
|
|
||||||
client.set_state(ConnectionState::Play)?; // Мы перешли в режим Play
|
client.set_state(ConnectionState::Play)?; // Мы перешли в режим Play
|
||||||
|
|
||||||
// Дальше работаем с режимом игры
|
// Тут работают уже приколы из пакет хандлера
|
||||||
handle_play_state(client)?;
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Тип подключения не рукопожатный
|
// Тип подключения не рукопожатный
|
||||||
|
@ -2,7 +2,7 @@ pub mod handler;
|
|||||||
pub mod packet_id;
|
pub mod packet_id;
|
||||||
pub mod play;
|
pub mod play;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ConnectionState {
|
pub enum ConnectionState {
|
||||||
Handshake,
|
Handshake,
|
||||||
Status,
|
Status,
|
||||||
|
@ -10,10 +10,46 @@ use rust_mc_proto::{DataReader, DataWriter, Packet, read_packet};
|
|||||||
use crate::{
|
use crate::{
|
||||||
ServerError,
|
ServerError,
|
||||||
data::{ReadWriteNBT, text_component::TextComponent},
|
data::{ReadWriteNBT, text_component::TextComponent},
|
||||||
|
event::PacketHandler,
|
||||||
player::context::ClientContext,
|
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<ClientContext>,
|
||||||
|
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<ClientContext>,
|
||||||
|
state: ConnectionState,
|
||||||
|
) -> Result<(), ServerError> {
|
||||||
|
if state == ConnectionState::Play {
|
||||||
|
// перешли в режим плей, отлично! делаем дела
|
||||||
|
|
||||||
|
handle_play_state(client)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn send_update_tags(client: Arc<ClientContext>) -> Result<(), ServerError> {
|
pub fn send_update_tags(client: Arc<ClientContext>) -> Result<(), ServerError> {
|
||||||
// TODO: rewrite this hardcode bullshit
|
// TODO: rewrite this hardcode bullshit
|
||||||
|
Loading…
x
Reference in New Issue
Block a user