Make some packets ServerPacket
This commit is contained in:
parent
306a007e3c
commit
43e8d9b6d9
@ -4,7 +4,10 @@ use pumpkin_data::packet::clientbound::PLAY_PLAYER_POSITION;
|
||||
use pumpkin_macros::packet;
|
||||
use pumpkin_util::math::vector3::Vector3;
|
||||
|
||||
use crate::{ClientPacket, PositionFlag, VarInt, WritingError, ser::NetworkWriteExt};
|
||||
use crate::{
|
||||
ClientPacket, PositionFlag, ServerPacket, VarInt, WritingError, ser::NetworkReadExt,
|
||||
ser::NetworkWriteExt,
|
||||
};
|
||||
|
||||
#[packet(PLAY_PLAYER_POSITION)]
|
||||
pub struct CPlayerPosition<'a> {
|
||||
@ -54,3 +57,17 @@ impl ClientPacket for CPlayerPosition<'_> {
|
||||
write.write_i32_be(PositionFlag::get_bitfield(self.releatives))
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerPacket for CPlayerPosition<'_> {
|
||||
fn read(mut read: impl std::io::Read) -> Result<Self, crate::ser::ReadingError> {
|
||||
Ok(Self {
|
||||
teleport_id: read.get_var_int()?,
|
||||
// TODO
|
||||
position: Vector3::new(0.0, 0.0, 0.0),
|
||||
delta: Vector3::new(0.0, 0.0, 0.0),
|
||||
yaw: 0.0,
|
||||
pitch: 0.0,
|
||||
releatives: &[],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
use std::io::Read;
|
||||
|
||||
use crate::{ConnectionState, ReadingError, ServerPacket, VarInt, ser::NetworkReadExt};
|
||||
use crate::{
|
||||
ClientPacket, ConnectionState, ReadingError, ServerPacket, VarInt, ser::NetworkReadExt,
|
||||
ser::NetworkWriteExt,
|
||||
};
|
||||
use pumpkin_data::packet::serverbound::HANDSHAKE_INTENTION;
|
||||
use pumpkin_macros::packet;
|
||||
|
||||
@ -26,3 +29,16 @@ impl ServerPacket for SHandShake {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientPacket for SHandShake {
|
||||
fn write_packet_data(
|
||||
&self,
|
||||
mut write: impl std::io::Write,
|
||||
) -> Result<(), crate::ser::WritingError> {
|
||||
write.write_var_int(&self.protocol_version)?;
|
||||
write.write_string(&self.server_address)?;
|
||||
write.write_u16_be(self.server_port)?;
|
||||
write.write_var_int(&VarInt(self.next_state as i32))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ use std::io::Read;
|
||||
|
||||
use pumpkin_data::packet::serverbound::LOGIN_HELLO;
|
||||
use pumpkin_macros::packet;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
ServerPacket,
|
||||
@ -9,6 +10,7 @@ use crate::{
|
||||
};
|
||||
|
||||
#[packet(LOGIN_HELLO)]
|
||||
#[derive(Serialize)]
|
||||
pub struct SLoginStart {
|
||||
pub name: String, // 16
|
||||
pub uuid: uuid::Uuid,
|
||||
|
||||
@ -2,15 +2,14 @@ use std::io::Read;
|
||||
|
||||
use pumpkin_data::packet::serverbound::PLAY_CHAT;
|
||||
use pumpkin_macros::packet;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
ServerPacket,
|
||||
ClientPacket, ServerPacket,
|
||||
codec::var_int::VarInt,
|
||||
ser::NetworkWriteExt,
|
||||
ser::{NetworkReadExt, ReadingError},
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[packet(PLAY_CHAT)]
|
||||
pub struct SChatMessage {
|
||||
pub message: String,
|
||||
@ -36,3 +35,20 @@ impl ServerPacket for SChatMessage {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientPacket for SChatMessage {
|
||||
fn write_packet_data(
|
||||
&self,
|
||||
mut write: impl std::io::Write,
|
||||
) -> Result<(), crate::ser::WritingError> {
|
||||
write.write_string(&self.message)?;
|
||||
write.write_i64_be(self.timestamp)?;
|
||||
write.write_i64_be(self.salt)?;
|
||||
write.write_option(&self.signature, |p, v| p.write_slice(v))?;
|
||||
write.write_var_int(&self.message_count)?;
|
||||
write.write_fixed_bitset(20, self.acknowledged.clone())?;
|
||||
write.write_u8(self.checksum)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -260,6 +260,8 @@ pub trait NetworkWriteExt {
|
||||
self.write_u8(0)
|
||||
}
|
||||
}
|
||||
fn write_fixed_bitset(&mut self, bits: usize, bit_set: FixedBitSet)
|
||||
-> Result<(), WritingError>;
|
||||
fn write_var_int(&mut self, data: &VarInt) -> Result<(), WritingError>;
|
||||
fn write_var_uint(&mut self, data: &VarUInt) -> Result<(), WritingError>;
|
||||
fn write_var_long(&mut self, data: &VarLong) -> Result<(), WritingError>;
|
||||
@ -340,6 +342,21 @@ impl<W: Write> NetworkWriteExt for W {
|
||||
self.write_all(data).map_err(WritingError::IoError)
|
||||
}
|
||||
|
||||
fn write_fixed_bitset(
|
||||
&mut self,
|
||||
bits: usize,
|
||||
bit_set: FixedBitSet,
|
||||
) -> Result<(), WritingError> {
|
||||
let new_length = bits.div_ceil(8);
|
||||
let mut new_vec = vec![0u8; new_length];
|
||||
let bytes_to_copy = std::cmp::min(bit_set.len(), new_length);
|
||||
|
||||
new_vec[..bytes_to_copy].copy_from_slice(&bit_set[..bytes_to_copy]);
|
||||
self.write_slice(&new_vec)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_var_int(&mut self, data: &VarInt) -> Result<(), WritingError> {
|
||||
data.encode(self)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user