more helper functions

This commit is contained in:
MeexReay 2025-05-03 20:42:23 +03:00
parent 50262ff1d7
commit 4d04729809
2 changed files with 44 additions and 1 deletions

View File

@ -25,4 +25,3 @@ impl ReadWritePosition for Packet {
Ok(self.write_long(((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF))?)
}
}

View File

@ -37,6 +37,32 @@ impl ProtocolHelper {
}
}
pub fn reset_chat(&self) -> Result<(), ServerError> {
match self.state {
ConnectionState::Configuration => {
self.client
.write_packet(&Packet::empty(clientbound::configuration::RESET_CHAT))?;
Ok(())
}
_ => Err(ServerError::UnexpectedState),
}
}
pub fn store_cookie(&self, id: &str, data: &[u8]) -> Result<(), ServerError> {
self.client.write_packet(&Packet::build(
match self.state {
ConnectionState::Configuration => clientbound::configuration::STORE_COOKIE,
ConnectionState::Play => clientbound::play::STORE_COOKIE,
_ => { return Err(ServerError::UnexpectedState) },
},
|p| {
p.write_string(id)?;
p.write_bytes(data)
},
)?)?;
Ok(())
}
/// Leave from Configuration to Play state
pub fn leave_configuration(&self) -> Result<(), ServerError> {
match self.state {
@ -132,6 +158,24 @@ impl ProtocolHelper {
None
};
Ok(data)
},
ConnectionState::Play => {
let mut packet = Packet::empty(clientbound::play::COOKIE_REQUEST);
packet.write_string(id)?;
self.client.write_packet(&packet)?;
let mut packet = self
.client
.read_packet(serverbound::play::COOKIE_RESPONSE)?;
packet.read_string()?;
let data = if packet.read_boolean()? {
let n = packet.read_usize_varint()?;
Some(packet.read_bytes(n)?)
} else {
None
};
Ok(data)
}
_ => Err(ServerError::UnexpectedState),