Skip to content

Commit

Permalink
Add Multiplayer support
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 6, 2024
1 parent d1c3b2f commit 32569ba
Show file tree
Hide file tree
Showing 34 changed files with 445 additions and 243 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = [ "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/" ]
members = [ "pumpkin-entity", "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/" ]

[workspace.package]
version = "0.1.0"
Expand Down
8 changes: 8 additions & 0 deletions pumpkin-entity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "pumpkin-entity"
version.workspace = true
edition.workspace = true

[dependencies]
num-traits = "0.2"
num-derive = "0.4"
8 changes: 8 additions & 0 deletions pumpkin-entity/src/entity_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use num_derive::ToPrimitive;

// todo
#[derive(ToPrimitive, Clone)]
pub enum EntityType {
Zombie = 124,
Player = 128,
}
19 changes: 19 additions & 0 deletions pumpkin-entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use entity_type::EntityType;

pub mod entity_type;

pub type EntityId = i32;

pub struct Entity {
pub entity_id: EntityId,
pub entity_type: EntityType,
}

impl Entity {
pub fn new(entity_id: EntityId, entity_type: EntityType) -> Self {
Self {
entity_id,
entity_type,
}
}
}
22 changes: 11 additions & 11 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}

Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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));
Expand All @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/bytebuf/packet_id.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -27,7 +27,7 @@ impl Serialize for VarInt32 {
}

pub trait Packet {
const PACKET_ID: VarInt;
const PACKET_ID: VarIntType;
}

impl<P> ClientPacket for P
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/login/c_encryption_request.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/login/c_set_compression.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}
10 changes: 5 additions & 5 deletions pumpkin-protocol/src/client/play/c_chunk_data_update_light.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
});
}
Expand Down
10 changes: 5 additions & 5 deletions pumpkin-protocol/src/client/play/c_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
9 changes: 5 additions & 4 deletions pumpkin-protocol/src/client/play/c_player_info_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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);
Expand Down
73 changes: 73 additions & 0 deletions pumpkin-protocol/src/client/play/c_spawn_player.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 32569ba

Please sign in to comment.