Add Multiplayer support

This commit is contained in:
Snowiiii committed 2024-08-06 16:19:03 +02:00
1 parent d1c3b2f10c
commit 32569bafe5
34 files changed
+445 -243

No files matched your search

+11 -11
View File
@@ -1,4 +1,4 @@
use crate::{BitSet, VarInt, VarLong};
use crate::{BitSet, VarInt, VarLongType};
use bytes::{Buf, BufMut, BytesMut};
use core::str;
use std::io::{self, Error, ErrorKind};
@@ -44,10 +44,10 @@ impl ByteBuffer {
}
}
value
VarInt(value)
}
pub fn get_var_long(&mut self) -> VarLong {
pub fn get_var_long(&mut self) -> VarLongType {
let mut value: i64 = 0;
let mut position: i64 = 0;
@@ -75,7 +75,7 @@ impl ByteBuffer {
}
pub fn get_string_len(&mut self, max_size: usize) -> Result<String, io::Error> {
let size = self.get_var_int();
let size = self.get_var_int().0;
if size as usize > max_size {
return Err(Error::new(
ErrorKind::InvalidData,
@@ -121,7 +121,7 @@ impl ByteBuffer {
}
pub fn put_string(&mut self, val: &str) {
self.put_var_int(val.len() as VarInt);
self.put_var_int(&val.len().into());
self.buffer.put(val.as_bytes());
}
@@ -131,8 +131,8 @@ impl ByteBuffer {
}
}
pub fn put_var_int(&mut self, value: VarInt) {
let mut val = value;
pub fn put_var_int(&mut self, value: &VarInt) {
let mut val = value.0;
for _ in 0..5 {
let mut b: u8 = val as u8 & 0b01111111;
val >>= 7;
@@ -147,7 +147,7 @@ impl ByteBuffer {
}
pub fn put_bit_set(&mut self, set: &BitSet) {
self.put_var_int(set.0);
self.put_var_int(&set.0);
for b in &set.1 {
self.put_i64(*b);
}
@@ -173,7 +173,7 @@ impl ByteBuffer {
}
pub fn get_list<T>(&mut self, val: impl Fn(&mut Self) -> T) -> Vec<T> {
let len = self.get_var_int().try_into().unwrap();
let len = self.get_var_int().0 as usize;
let mut list = Vec::with_capacity(len);
for _ in 0..len {
list.push(val(self));
@@ -182,14 +182,14 @@ impl ByteBuffer {
}
/// Writes a list to the buffer.
pub fn put_list<T>(&mut self, list: &[T], write: impl Fn(&mut Self, &T)) {
self.put_var_int(list.len().try_into().unwrap());
self.put_var_int(&list.len().into());
for v in list {
write(self, v);
}
}
pub fn put_varint_arr(&mut self, v: &[i32]) {
self.put_list(v, |p, &v| p.put_var_int(v))
self.put_list(v, |p, &v| p.put_var_int(&v.into()))
}
/* pub fn get_nbt(&mut self) -> Option<fastnbt::value::Value> {
+3 -3
View File
@@ -1,10 +1,10 @@
use serde::{Serialize, Serializer};
use crate::{ClientPacket, VarInt, VarInt32};
use crate::{ClientPacket, VarInt, VarIntType};
use super::{serializer, ByteBuffer};
impl Serialize for VarInt32 {
impl Serialize for VarInt {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@@ -27,7 +27,7 @@ impl Serialize for VarInt32 {
}
pub trait Packet {
const PACKET_ID: VarInt;
const PACKET_ID: VarIntType;
}
impl<P> ClientPacket for P
@@ -1,6 +1,6 @@
use pumpkin_macros::packet;
use crate::{bytebuf::ByteBuffer, ClientPacket, VarInt};
use crate::{bytebuf::ByteBuffer, ClientPacket, VarIntType};
#[packet(0x01)]
pub struct CEncryptionRequest<'a> {
@@ -29,9 +29,9 @@ impl<'a> CEncryptionRequest<'a> {
impl<'a> ClientPacket for CEncryptionRequest<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(self.server_id);
bytebuf.put_var_int(self.public_key.len() as VarInt);
bytebuf.put_var_int(&(self.public_key.len() as VarIntType).into());
bytebuf.put_slice(self.public_key);
bytebuf.put_var_int(self.verify_token.len() as VarInt);
bytebuf.put_var_int(&(self.verify_token.len() as VarIntType).into());
bytebuf.put_slice(self.verify_token);
bytebuf.put_bool(self.should_authenticate);
}
@@ -1,16 +1,16 @@
use pumpkin_macros::packet;
use serde::Serialize;
use crate::VarInt32;
use crate::VarInt;
#[derive(Serialize)]
#[packet(0x03)]
pub struct CSetCompression {
threshold: VarInt32,
threshold: VarInt,
}
impl CSetCompression {
pub fn new(threshold: VarInt32) -> Self {
pub fn new(threshold: VarInt) -> Self {
Self { threshold }
}
}
@@ -1,6 +1,6 @@
use pumpkin_macros::packet;
use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt};
use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt, VarIntType};
#[packet(0x27)]
pub struct CChunkDataUpdateLight {
@@ -50,23 +50,23 @@ impl ClientPacket for CChunkDataUpdateLight {
bytebuf.put_i32(self.chunk_x);
bytebuf.put_i32(self.chunk_y);
bytebuf.put_slice(&self.heightmaps);
bytebuf.put_var_int(self.data.len() as VarInt);
bytebuf.put_var_int(&(self.data.len() as VarIntType).into());
bytebuf.put_slice(&self.data);
bytebuf.put_list::<BlockEntity>(&self.block_entites, |p, v| {
p.put_u8(v.packed_xz);
p.put_i16(v.y);
p.put_var_int(v.typee);
p.put_var_int(&v.typee);
p.put_slice(&v.data);
});
bytebuf.put_bit_set(&self.sky_light_mask);
bytebuf.put_bit_set(&self.block_light_mask);
bytebuf.put_bit_set(&self.empty_sky_light_mask);
bytebuf.put_list::<SkyLight>(&self.sky_lights, |p, v| {
p.put_var_int(v.array.len() as VarInt);
p.put_var_int(&(v.array.len() as VarIntType).into());
p.put_slice(&v.array);
});
bytebuf.put_list::<BlockLight>(&self.block_lights, |p, v| {
p.put_var_int(v.array.len() as VarInt);
p.put_var_int(&(v.array.len() as VarIntType).into());
p.put_slice(&v.array);
});
}
+5 -5
View File
@@ -83,13 +83,13 @@ impl ClientPacket for CLogin {
bytebuf.put_i32(self.entity_id);
bytebuf.put_bool(self.is_hardcore);
bytebuf.put_list(&self.dimension_names, |buf, v| buf.put_string(v));
bytebuf.put_var_int(self.max_players);
bytebuf.put_var_int(self.view_distance);
bytebuf.put_var_int(self.simulated_distance);
bytebuf.put_var_int(&self.max_players);
bytebuf.put_var_int(&self.view_distance);
bytebuf.put_var_int(&self.simulated_distance);
bytebuf.put_bool(self.reduced_debug_info);
bytebuf.put_bool(self.enabled_respawn_screen);
bytebuf.put_bool(self.limited_crafting);
bytebuf.put_var_int(self.dimension_type);
bytebuf.put_var_int(&self.dimension_type);
bytebuf.put_string(&self.dimension_name);
bytebuf.put_i64(self.hashed_seed);
bytebuf.put_u8(self.game_mode);
@@ -101,7 +101,7 @@ impl ClientPacket for CLogin {
bytebuf.put_string(self.death_dimension_name.as_ref().unwrap());
bytebuf.put_i64(self.death_loc.unwrap());
}
bytebuf.put_var_int(self.portal_cooldown);
bytebuf.put_var_int(&self.portal_cooldown);
bytebuf.put_bool(self.enforce_secure_chat);
}
}
@@ -4,15 +4,16 @@ use crate::{bytebuf::ByteBuffer, ClientPacket, Property};
use super::PlayerAction;
#[derive(Clone)]
#[packet(0x3E)]
pub struct CPlayerInfoUpdate<'a> {
pub actions: i8,
pub players: &'a [Player<'a>],
pub players: &'a [Player],
}
pub struct Player<'a> {
pub struct Player {
pub uuid: uuid::Uuid,
pub actions: &'a [PlayerAction],
pub actions: Vec<PlayerAction>,
}
impl<'a> CPlayerInfoUpdate<'a> {
@@ -26,7 +27,7 @@ impl<'a> ClientPacket for CPlayerInfoUpdate<'a> {
bytebuf.put_i8(self.actions);
bytebuf.put_list::<Player>(self.players, |p, v| {
p.put_uuid(v.uuid);
for action in v.actions {
for action in &v.actions {
match action {
PlayerAction::AddPlayer { name, properties } => {
p.put_string(name);
@@ -0,0 +1,73 @@
use pumpkin_macros::packet;
use crate::{ClientPacket, VarInt};
#[derive(Clone)]
#[packet(0x01)]
pub struct CSpawnEntity {
entity_id: VarInt,
entity_uuid: uuid::Uuid,
typ: VarInt,
x: f64,
y: f64,
z: f64,
pitch: u8, // angle
yaw: u8, // angle
head_yaw: u8, // angle
data: VarInt,
velocity_x: i16,
velocity_y: i16,
velocity_z: i16,
}
impl CSpawnEntity {
pub fn new(
entity_id: VarInt,
entity_uuid: uuid::Uuid,
typ: VarInt,
x: f64,
y: f64,
z: f64,
pitch: u8, // angle
yaw: u8, // angle
head_yaw: u8, // angle
data: VarInt,
velocity_x: i16,
velocity_y: i16,
velocity_z: i16,
) -> Self {
Self {
entity_id,
entity_uuid,
typ,
x,
y,
z,
pitch,
yaw,
head_yaw,
data,
velocity_x,
velocity_y,
velocity_z,
}
}
}
impl ClientPacket for CSpawnEntity {
fn write(&self, bytebuf: &mut crate::bytebuf::ByteBuffer) {
bytebuf.put_var_int(&self.entity_id);
bytebuf.put_uuid(self.entity_uuid);
bytebuf.put_var_int(&self.typ);
bytebuf.put_f64(self.x);
bytebuf.put_f64(self.y);
bytebuf.put_f64(self.z);
bytebuf.put_u8(self.pitch);
bytebuf.put_u8(self.yaw);
bytebuf.put_u8(self.head_yaw);
bytebuf.put_var_int(&self.data);
bytebuf.put_i16(self.velocity_x);
bytebuf.put_i16(self.velocity_y);
bytebuf.put_i16(self.velocity_z);
}
}
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;
use crate::VarInt32;
use crate::VarInt;
#[derive(Serialize)]
#[packet(0x40)]
@@ -12,7 +12,7 @@ pub struct CSyncPlayerPostion {
yaw: f32,
pitch: f32,
flags: i8,
teleport_id: VarInt32,
teleport_id: VarInt,
}
impl CSyncPlayerPostion {
@@ -23,7 +23,7 @@ impl CSyncPlayerPostion {
yaw: f32,
pitch: f32,
flags: i8,
teleport_id: VarInt32,
teleport_id: VarInt,
) -> Self {
Self {
x,
+2
View File
@@ -6,6 +6,7 @@ mod c_play_disconnect;
mod c_player_abilities;
mod c_player_info_update;
mod c_set_held_item;
mod c_spawn_player;
mod c_sync_player_position;
mod c_system_chat_message;
mod player_action;
@@ -18,6 +19,7 @@ pub use c_play_disconnect::*;
pub use c_player_abilities::*;
pub use c_player_info_update::*;
pub use c_set_held_item::*;
pub use c_spawn_player::*;
pub use c_sync_player_position::*;
pub use c_system_chat_message::*;
pub use player_action::*;
+33 -8
View File
@@ -19,14 +19,15 @@ pub const MAX_PACKET_SIZE: i32 = 2097152;
/// usally uses a namespace like "minecraft:thing"
pub type Identifier = String;
pub type VarInt = i32;
pub type VarLong = i64;
pub type VarIntType = i32;
pub type VarLongType = i64;
pub struct BitSet(pub VarInt, pub Vec<i64>);
pub struct VarInt32(pub i32);
#[derive(Debug, Clone, PartialEq)]
pub struct VarInt(pub VarIntType);
impl VarInt32 {
impl VarInt {
/// The maximum number of bytes a `VarInt` could occupy when read from and
/// written to the Minecraft protocol.
pub const MAX_SIZE: usize = 5;
@@ -83,16 +84,40 @@ impl VarInt32 {
let byte = r.get_u8();
val |= (i32::from(byte) & 0b01111111) << (i * 7);
if byte & 0b10000000 == 0 {
return Ok(VarInt32(val));
return Ok(VarInt(val));
}
}
Err(VarIntDecodeError::TooLarge)
}
}
impl From<i32> for VarInt32 {
impl From<i32> for VarInt {
fn from(value: i32) -> Self {
VarInt32(value)
VarInt(value)
}
}
impl From<u32> for VarInt {
fn from(value: u32) -> Self {
VarInt(value as i32)
}
}
impl From<u8> for VarInt {
fn from(value: u8) -> Self {
VarInt(value as i32)
}
}
impl From<usize> for VarInt {
fn from(value: usize) -> Self {
VarInt(value as i32)
}
}
impl From<VarInt> for i32 {
fn from(value: VarInt) -> Self {
value.0
}
}
@@ -134,6 +159,7 @@ pub enum ConnectionState {
impl From<VarInt> for ConnectionState {
fn from(value: VarInt) -> Self {
let value = value.0;
match value {
1 => Self::Status,
2 => Self::Login,
@@ -147,7 +173,6 @@ impl From<VarInt> for ConnectionState {
}
pub struct RawPacket {
pub len: VarInt,
pub id: VarInt,
pub bytebuf: ByteBuffer,
}
+4 -7
View File
@@ -2,7 +2,7 @@ use aes::cipher::{generic_array::GenericArray, BlockDecryptMut, BlockSizeUser, K
use bytes::{Buf, BytesMut};
use crate::{
bytebuf::ByteBuffer, PacketError, RawPacket, VarInt32, VarIntDecodeError, MAX_PACKET_SIZE,
bytebuf::ByteBuffer, PacketError, RawPacket, VarInt, VarIntDecodeError, MAX_PACKET_SIZE,
};
type Cipher = cfb8::Decryptor<aes::Aes128>;
@@ -19,7 +19,7 @@ impl PacketDecoder {
pub fn decode(&mut self) -> Result<Option<RawPacket>, PacketError> {
let mut r = &self.buf[..];
let packet_len = match VarInt32::decode_partial(&mut r) {
let packet_len = match VarInt::decode_partial(&mut r) {
Ok(len) => len,
Err(VarIntDecodeError::Incomplete) => return Ok(None),
Err(VarIntDecodeError::TooLarge) => Err(PacketError::MailformedLength)?,
@@ -34,7 +34,7 @@ impl PacketDecoder {
return Ok(None);
}
let packet_len_len = VarInt32(packet_len).written_size();
let packet_len_len = VarInt(packet_len).written_size();
let mut data;
@@ -44,13 +44,10 @@ impl PacketDecoder {
data = self.buf.split_to(packet_len as usize);
r = &data[..];
let packet_id = VarInt32::decode(&mut r)
.map_err(|_| PacketError::DecodeID)?
.0;
let packet_id = VarInt::decode(&mut r).map_err(|_| PacketError::DecodeID)?;
data.advance(data.len() - r.len());
Ok(Some(RawPacket {
len: packet_len,
id: packet_id,
bytebuf: ByteBuffer::new(data),
}))
+4 -4
View File
@@ -3,7 +3,7 @@ use std::io::Write;
use aes::cipher::{generic_array::GenericArray, BlockEncryptMut, BlockSizeUser, KeyIvInit};
use bytes::{BufMut, BytesMut};
use crate::{bytebuf::ByteBuffer, ClientPacket, PacketError, VarInt32, MAX_PACKET_SIZE};
use crate::{bytebuf::ByteBuffer, ClientPacket, PacketError, VarInt, MAX_PACKET_SIZE};
type Cipher = cfb8::Encryptor<aes::Aes128>;
@@ -22,7 +22,7 @@ impl PacketEncoder {
let mut writer = (&mut self.buf).writer();
let mut packet_buf = ByteBuffer::empty();
VarInt32(P::PACKET_ID)
VarInt(P::PACKET_ID)
.encode(&mut writer)
.map_err(|_| PacketError::EncodeID)?;
packet.write(&mut packet_buf);
@@ -41,14 +41,14 @@ impl PacketEncoder {
Err(PacketError::TooLong)?
}
let packet_len_size = VarInt32(packet_len as i32).written_size();
let packet_len_size = VarInt(packet_len as i32).written_size();
self.buf.put_bytes(0, packet_len_size);
self.buf
.copy_within(start_len..start_len + data_len, start_len + packet_len_size);
let front = &mut self.buf[start_len..];
VarInt32(packet_len as i32)
VarInt(packet_len as i32)
.encode(front)
.map_err(|_| PacketError::EncodeID)?;
Ok(())
+4 -4
View File
@@ -12,7 +12,7 @@ pub struct SClientInformation {
}
impl ServerPacket for SClientInformation {
const PACKET_ID: VarInt = 0x00;
const PACKET_ID: VarInt = VarInt(0x00);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
@@ -34,7 +34,7 @@ pub struct SPluginMessage {
}
impl ServerPacket for SPluginMessage {
const PACKET_ID: VarInt = 0x02;
const PACKET_ID: VarInt = VarInt(0x02);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
@@ -47,7 +47,7 @@ impl ServerPacket for SPluginMessage {
pub struct SAcknowledgeFinishConfig {}
impl ServerPacket for SAcknowledgeFinishConfig {
const PACKET_ID: VarInt = 0x03;
const PACKET_ID: VarInt = VarInt(0x03);
fn read(_bytebuf: &mut ByteBuffer) -> Self {
Self {}
@@ -60,7 +60,7 @@ pub struct SKnownPacks {
}
impl ServerPacket for SKnownPacks {
const PACKET_ID: VarInt = 0x07;
const PACKET_ID: VarInt = VarInt(0x07);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
+1 -1
View File
@@ -8,7 +8,7 @@ pub struct SHandShake {
}
impl ServerPacket for SHandShake {
const PACKET_ID: VarInt = 0x00;
const PACKET_ID: VarInt = VarInt(0x00);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
+6 -6
View File
@@ -6,7 +6,7 @@ pub struct SLoginStart {
}
impl ServerPacket for SLoginStart {
const PACKET_ID: VarInt = 0x00;
const PACKET_ID: VarInt = VarInt(0x00);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
@@ -24,13 +24,13 @@ pub struct SEncryptionResponse {
}
impl ServerPacket for SEncryptionResponse {
const PACKET_ID: VarInt = 0x01;
const PACKET_ID: VarInt = VarInt(0x01);
fn read(bytebuf: &mut ByteBuffer) -> Self {
let shared_secret_length = bytebuf.get_var_int();
let shared_secret = bytebuf.copy_to_bytes(shared_secret_length as usize);
let shared_secret = bytebuf.copy_to_bytes(shared_secret_length.0 as usize);
let verify_token_length = bytebuf.get_var_int();
let verify_token = bytebuf.copy_to_bytes(shared_secret_length as usize);
let verify_token = bytebuf.copy_to_bytes(shared_secret_length.0 as usize);
Self {
shared_secret_length,
shared_secret: shared_secret.to_vec(),
@@ -47,7 +47,7 @@ pub struct SLoginPluginResponse<'a> {
}
impl<'a> ServerPacket for SLoginPluginResponse<'a> {
const PACKET_ID: VarInt = 0x02;
const PACKET_ID: VarInt = VarInt(0x02);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
@@ -64,7 +64,7 @@ pub struct SLoginAcknowledged {
}
impl ServerPacket for SLoginAcknowledged {
const PACKET_ID: VarInt = 0x03;
const PACKET_ID: VarInt = VarInt(0x03);
fn read(_bytebuf: &mut ByteBuffer) -> Self {
Self {}
+7 -7
View File
@@ -8,7 +8,7 @@ pub struct SConfirmTeleport {
}
impl ServerPacket for SConfirmTeleport {
const PACKET_ID: VarInt = 0x00;
const PACKET_ID: VarInt = VarInt(0x00);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
@@ -22,7 +22,7 @@ pub struct SChatCommand {
}
impl ServerPacket for SChatCommand {
const PACKET_ID: VarInt = 0x04;
const PACKET_ID: VarInt = VarInt(0x04);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
@@ -39,7 +39,7 @@ pub struct SPlayerPosition {
}
impl ServerPacket for SPlayerPosition {
const PACKET_ID: VarInt = 0x1A;
const PACKET_ID: VarInt = VarInt(0x1A);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
@@ -69,12 +69,12 @@ pub enum Action {
}
impl ServerPacket for SPlayerCommand {
const PACKET_ID: VarInt = 0x25;
const PACKET_ID: VarInt = VarInt(0x25);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
entitiy_id: bytebuf.get_var_int(),
action: Action::from_i32(bytebuf.get_var_int()).unwrap(),
action: Action::from_i32(bytebuf.get_var_int().into()).unwrap(),
jump_boost: bytebuf.get_var_int(),
}
}
@@ -90,7 +90,7 @@ pub struct SPlayerPositionRotation {
}
impl ServerPacket for SPlayerPositionRotation {
const PACKET_ID: VarInt = 0x1B;
const PACKET_ID: VarInt = VarInt(0x1B);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
@@ -111,7 +111,7 @@ pub struct SPlayerRotation {
}
impl ServerPacket for SPlayerRotation {
const PACKET_ID: VarInt = 0x1C;
const PACKET_ID: VarInt = VarInt(0x1C);
fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
+2 -2
View File
@@ -5,7 +5,7 @@ pub struct SStatusRequest {
}
impl ServerPacket for SStatusRequest {
const PACKET_ID: VarInt = 0x00;
const PACKET_ID: VarInt = VarInt(0x00);
fn read(_bytebuf: &mut ByteBuffer) -> Self {
Self {}
@@ -17,7 +17,7 @@ pub struct SPingRequest {
}
impl ServerPacket for SPingRequest {
const PACKET_ID: VarInt = 0x01;
const PACKET_ID: VarInt = VarInt(0x01);
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {