more protocol helper methods
This commit is contained in:
parent
c1c3884041
commit
689d769baa
@ -1,5 +1,7 @@
|
|||||||
# rust_minecraft_server
|
# rust_minecraft_server
|
||||||
|
|
||||||
|
Простой майнкрафт сервер на расте. Поддерживаемая версия: 1.21.5 ()
|
||||||
|
|
||||||
## Как запустить
|
## Как запустить
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::{io::Read, sync::Arc};
|
use std::{io::Read, sync::Arc, time::{Duration, SystemTime}};
|
||||||
|
|
||||||
use rust_mc_proto::{DataReader, DataWriter, Packet};
|
use rust_mc_proto::{DataReader, DataWriter, Packet};
|
||||||
|
|
||||||
@ -27,6 +27,51 @@ impl ProtocolHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Leave from Configuration to Play state
|
||||||
|
pub fn leave_configuration(&self) -> Result<(), ServerError> {
|
||||||
|
match self.state {
|
||||||
|
ConnectionState::Configuration => {
|
||||||
|
self.client.conn().write_packet(&Packet::empty(0x03))?;
|
||||||
|
self.client.conn().read_packet()?;
|
||||||
|
self.client.set_state(ConnectionState::Play)?;
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
_ => Err(ServerError::UnexpectedState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enter to Configuration from Play state
|
||||||
|
pub fn enter_configuration(&self) -> Result<(), ServerError> {
|
||||||
|
match self.state {
|
||||||
|
ConnectionState::Play => {
|
||||||
|
self.client.conn().write_packet(&Packet::empty(0x6F))?;
|
||||||
|
self.client.conn().read_packet()?;
|
||||||
|
self.client.set_state(ConnectionState::Configuration)?;
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
_ => Err(ServerError::UnexpectedState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enter to Configuration from Play state
|
||||||
|
pub fn ping(&self) -> Result<Duration, ServerError> {
|
||||||
|
match self.state {
|
||||||
|
ConnectionState::Play => {
|
||||||
|
let time = SystemTime::now();
|
||||||
|
self.client.conn().write_packet(&Packet::empty(0x36))?;
|
||||||
|
self.client.conn().read_packet()?;
|
||||||
|
Ok(SystemTime::now().duration_since(time).unwrap())
|
||||||
|
},
|
||||||
|
ConnectionState::Configuration => {
|
||||||
|
let time = SystemTime::now();
|
||||||
|
self.client.conn().write_packet(&Packet::empty(0x05))?;
|
||||||
|
self.client.conn().read_packet()?;
|
||||||
|
Ok(SystemTime::now().duration_since(time).unwrap())
|
||||||
|
},
|
||||||
|
_ => Err(ServerError::UnexpectedState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn disconnect(&self, reason: TextComponent) -> Result<(), ServerError> {
|
pub fn disconnect(&self, reason: TextComponent) -> Result<(), ServerError> {
|
||||||
let packet = match self.state {
|
let packet = match self.state {
|
||||||
ConnectionState::Login => {
|
ConnectionState::Login => {
|
||||||
|
Loading…
Reference in New Issue
Block a user