Skip to content

Commit

Permalink
Add Particles
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 16, 2024
1 parent 3b43322 commit bf92fe2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ and customizable experience. It prioritizes performance and player enjoyment whi
- [ ] Query
- [x] RCON
- [x] Inventories
- [x] Particles
- [x] Chat
- [x] Commands
- Proxy
Expand Down
51 changes: 51 additions & 0 deletions pumpkin-protocol/src/client/play/c_particle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::VarInt;

#[derive(Serialize)]
#[packet(0x29)]
pub struct CParticle<'a> {
/// If true, particle distance increases from 256 to 65536.
long_distance: bool,
x: f64,
y: f64,
z: f64,
offset_x: f32,
offset_y: f32,
offset_z: f32,
max_speed: f32,
particle_count: i32,
pariticle_id: VarInt,
data: &'a [u8],
}

impl<'a> CParticle<'a> {
pub fn new(
long_distance: bool,
x: f64,
y: f64,
z: f64,
offset_x: f32,
offset_y: f32,
offset_z: f32,
max_speed: f32,
particle_count: i32,
pariticle_id: VarInt,
data: &'a [u8],
) -> Self {
Self {
long_distance,
x,
y,
z,
offset_x,
offset_y,
offset_z,
max_speed,
particle_count,
pariticle_id,
data,
}
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod c_head_rot;
mod c_hurt_animation;
mod c_login;
mod c_open_screen;
mod c_particle;
mod c_play_disconnect;
mod c_player_abilities;
mod c_player_chat_message;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub use c_head_rot::*;
pub use c_hurt_animation::*;
pub use c_login::*;
pub use c_open_screen::*;
pub use c_particle::*;
pub use c_play_disconnect::*;
pub use c_player_abilities::*;
pub use c_player_chat_message::*;
Expand Down
27 changes: 0 additions & 27 deletions pumpkin-world/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,8 @@ impl ChunkData {
};

let mask = (1 << block_size) - 1;
// println!("{block_size} {:#08b}", mask);
// println!(
// "{} {}",
// block_size,
// palette.iter().map(|v| v.to_string()).join(",")
// );
let mut blocks_left = 16 * 16 * 16;
'block_loop: for (j, block) in block_data.iter().enumerate() {
// if at == (0,0) && block_size != 4 {
// println!("{} {:#066b}", 64 - (64 / block_size) * block_size, *block);
// }
for i in 0..64 / block_size {
if blocks_left <= 0 {
break 'block_loop;
Expand All @@ -138,24 +129,6 @@ impl ChunkData {
}
}

// std::fs::File::create_new("../mafaile.txt")
// .unwrap()
// .write_all(
// blocks
// .iter()
// .map(|v| v.to_string())
// .collect::<Vec<_>>()
// .join(",")
// .as_bytes(),
// );

// if at == (0, 0) {
// println!(
// "[{}]",
// &blocks[0..10000].iter().map(|v| v.to_string()).join(",")
// );
// }
// dbg!("{}", blocks.iter().filter(|v| **v == 2005).collect_vec().len());
Ok(ChunkData {
blocks,
position: at,
Expand Down
34 changes: 23 additions & 11 deletions pumpkin-world/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ pub enum Compression {
LZ4,
}

impl Compression {
pub fn from_byte(byte: u8) -> Option<Self> {
match byte {
1 => Some(Self::Gzip),
2 => Some(Self::Zlib),
3 => Some(Self::None),
4 => Some(Self::LZ4),
_ => None,
}
}
}

impl Level {
pub fn from_root_folder(root_folder: PathBuf) -> Self {
// TODO: Check if exists
Expand Down Expand Up @@ -177,13 +189,10 @@ impl Level {
// TODO: check checksum to make sure chunk is not corrupted
let header = file_buf.drain(0..5).collect_vec();

let compression = match header[4] {
1 => Compression::Gzip,
2 => Compression::Zlib,
3 => Compression::None,
4 => Compression::LZ4,
_ => {
let _ = channel.send((
let compression = match Compression::from_byte(header[4]) {
Some(c) => c,
None => {
let _ = channel.blocking_send((
(chunk.0, chunk.1),
Err(WorldError::Compression(
CompressionError::UnknownCompression,
Expand All @@ -193,20 +202,23 @@ impl Level {
}
};

let size = u32::from_be_bytes(header[0..4].try_into().unwrap());
let size = u32::from_be_bytes(header[..4].try_into().unwrap());

// size includes the compression scheme byte, so we need to subtract 1
let chunk_data = file_buf.drain(0..size as usize - 1).collect_vec();
let decompressed_chunk = match Self::decompress_data(compression, chunk_data) {
Ok(data) => data,
Err(e) => {
let _ = channel.send(((chunk.0, chunk.1), Err(WorldError::Compression(e))));
channel
.blocking_send(((chunk.0, chunk.1), Err(WorldError::Compression(e))))
.expect("Failed to send Compression error");
return;
}
};

let _ = channel
.blocking_send((chunk, ChunkData::from_bytes(decompressed_chunk, chunk)));
channel
.blocking_send((chunk, ChunkData::from_bytes(decompressed_chunk, chunk)))
.expect("Error sending decompressed chunk");
})
.collect::<Vec<()>>();
}
Expand Down
5 changes: 3 additions & 2 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use num_traits::FromPrimitive;
use pumpkin_entity::EntityId;
use pumpkin_protocol::{
client::play::{
Animation, CEntityAnimation, CEntityVelocity, CHeadRot, CHurtAnimation, CSystemChatMessge,
CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot,
Animation, CEntityAnimation, CEntityVelocity, CHeadRot, CHurtAnimation,
CSystemChatMessge, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot,
},
server::play::{
Action, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract,
Expand Down Expand Up @@ -217,6 +217,7 @@ impl Client {
false,
),
);

/* server.broadcast_packet(
self,
CPlayerChatMessage::new(
Expand Down

0 comments on commit bf92fe2

Please sign in to comment.