From cfa20776f6c31445d73870bab3467db756edf73b Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 12 Aug 2024 21:26:09 +0200 Subject: [PATCH] Add Clientbound Client Informations (Play) --- .../src/client/play/c_center_chunk.rs | 6 +-- pumpkin-protocol/src/client/play/mod.rs | 4 +- pumpkin-protocol/src/packet_encoder.rs | 2 +- .../src/server/config/s_client_information.rs | 2 +- .../src/server/play/c_client_information.rs | 16 +++++++ pumpkin-protocol/src/server/play/mod.rs | 2 + pumpkin-world/src/lib.rs | 2 +- pumpkin/src/client/client_packet.rs | 6 +-- pumpkin/src/client/mod.rs | 27 +++++++---- pumpkin/src/client/player_packet.rs | 25 ++++++++-- pumpkin/src/server.rs | 48 +++++++++++-------- 11 files changed, 95 insertions(+), 45 deletions(-) create mode 100644 pumpkin-protocol/src/server/play/c_client_information.rs diff --git a/pumpkin-protocol/src/client/play/c_center_chunk.rs b/pumpkin-protocol/src/client/play/c_center_chunk.rs index 28652f38c..01c73c8d0 100644 --- a/pumpkin-protocol/src/client/play/c_center_chunk.rs +++ b/pumpkin-protocol/src/client/play/c_center_chunk.rs @@ -5,6 +5,6 @@ use crate::VarInt; #[derive(serde::Serialize)] #[packet(0x54)] pub struct CCenterChunk { - pub chunk_x: VarInt, - pub chunk_z: VarInt -} \ No newline at end of file + pub chunk_x: VarInt, + pub chunk_z: VarInt, +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 823feec03..dd35343ee 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -1,5 +1,5 @@ -mod c_center_chunk; mod c_actionbar; +mod c_center_chunk; mod c_change_difficulty; mod c_chunk_data; mod c_chunk_data_update_light; @@ -27,8 +27,8 @@ mod c_update_entity_pos; mod c_update_entity_rot; mod player_action; -pub use c_center_chunk::*; pub use c_actionbar::*; +pub use c_center_chunk::*; pub use c_change_difficulty::*; pub use c_chunk_data::*; pub use c_chunk_data_update_light::*; diff --git a/pumpkin-protocol/src/packet_encoder.rs b/pumpkin-protocol/src/packet_encoder.rs index e18ab44c2..1c545804b 100644 --- a/pumpkin-protocol/src/packet_encoder.rs +++ b/pumpkin-protocol/src/packet_encoder.rs @@ -32,7 +32,7 @@ impl PacketEncoder { .encode(&mut writer) .map_err(|_| PacketError::EncodeID)?; packet.write(&mut packet_buf); - + writer .write(packet_buf.buf()) .map_err(|_| PacketError::EncodeFailedWrite)?; diff --git a/pumpkin-protocol/src/server/config/s_client_information.rs b/pumpkin-protocol/src/server/config/s_client_information.rs index b5d2cdf73..df6d2314c 100644 --- a/pumpkin-protocol/src/server/config/s_client_information.rs +++ b/pumpkin-protocol/src/server/config/s_client_information.rs @@ -4,7 +4,7 @@ use crate::VarInt; #[derive(serde::Deserialize)] #[packet(0x00)] -pub struct SClientInformation { +pub struct SClientInformationConfig { pub locale: String, // 16 pub view_distance: i8, pub chat_mode: VarInt, // Varint diff --git a/pumpkin-protocol/src/server/play/c_client_information.rs b/pumpkin-protocol/src/server/play/c_client_information.rs new file mode 100644 index 000000000..78cf02bfa --- /dev/null +++ b/pumpkin-protocol/src/server/play/c_client_information.rs @@ -0,0 +1,16 @@ +use pumpkin_macros::packet; + +use crate::VarInt; + +#[derive(serde::Deserialize)] +#[packet(0x0A)] +pub struct SClientInformationPlay { + pub locale: String, // 16 + pub view_distance: i8, + pub chat_mode: VarInt, // Varint + pub chat_colors: bool, + pub skin_parts: u8, + pub main_hand: VarInt, + pub text_filtering: bool, + pub server_listing: bool, +} diff --git a/pumpkin-protocol/src/server/play/mod.rs b/pumpkin-protocol/src/server/play/mod.rs index fe2fe0274..977320703 100644 --- a/pumpkin-protocol/src/server/play/mod.rs +++ b/pumpkin-protocol/src/server/play/mod.rs @@ -1,3 +1,4 @@ +mod c_client_information; mod s_chat_command; mod s_chat_message; mod s_confirm_teleport; @@ -7,6 +8,7 @@ mod s_player_position_rotation; mod s_player_rotation; mod s_swing_arm; +pub use c_client_information::*; pub use s_chat_command::*; pub use s_chat_message::*; pub use s_confirm_teleport::*; diff --git a/pumpkin-world/src/lib.rs b/pumpkin-world/src/lib.rs index dd42f3e46..b99c06691 100644 --- a/pumpkin-world/src/lib.rs +++ b/pumpkin-world/src/lib.rs @@ -3,5 +3,5 @@ pub mod dimension; pub const WORLD_HEIGHT: usize = 384; pub const WORLD_Y_START_AT: i32 = -64; pub const DIRECT_PALETTE_BITS: u32 = 15; -mod world; mod block_registry; +mod world; diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index d815e54da..d9f9d5335 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -8,7 +8,7 @@ use pumpkin_protocol::{ status::{CPingResponse, CStatusResponse}, }, server::{ - config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks, SPluginMessage}, + config::{SAcknowledgeFinishConfig, SClientInformationConfig, SKnownPacks, SPluginMessage}, handshake::SHandShake, login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, status::{SPingRequest, SStatusRequest}, @@ -195,10 +195,10 @@ impl Client { }])); dbg!("login achnowlaged"); } - pub fn handle_client_information( + pub fn handle_client_information_config( &mut self, _server: &mut Server, - client_information: SClientInformation, + client_information: SClientInformationConfig, ) { dbg!("got client settings"); self.config = Some(PlayerConfig { diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index 96941a393..a7cd44f53 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -23,12 +23,12 @@ use pumpkin_protocol::{ packet_decoder::PacketDecoder, packet_encoder::PacketEncoder, server::{ - config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks, SPluginMessage}, + config::{SAcknowledgeFinishConfig, SClientInformationConfig, SKnownPacks, SPluginMessage}, handshake::SHandShake, login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, play::{ - SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition, - SPlayerPositionRotation, SPlayerRotation, SSwingArm, + SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SPlayerCommand, + SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SSwingArm, }, status::{SPingRequest, SStatusRequest}, }, @@ -218,15 +218,20 @@ impl Client { ), }, pumpkin_protocol::ConnectionState::Config => match packet.id.0 { - SClientInformation::PACKET_ID => self - .handle_client_information(server, SClientInformation::read(bytebuf).unwrap()), + SClientInformationConfig::PACKET_ID => self.handle_client_information_config( + server, + SClientInformationConfig::read(bytebuf).unwrap(), + ), SPluginMessage::PACKET_ID => { self.handle_plugin_message(server, SPluginMessage::read(bytebuf).unwrap()) } - SAcknowledgeFinishConfig::PACKET_ID => self.handle_config_acknowledged( - server, - SAcknowledgeFinishConfig::read(bytebuf).unwrap(), - ).await, + SAcknowledgeFinishConfig::PACKET_ID => { + self.handle_config_acknowledged( + server, + SAcknowledgeFinishConfig::read(bytebuf).unwrap(), + ) + .await + } SKnownPacks::PACKET_ID => { self.handle_known_packs(server, SKnownPacks::read(bytebuf).unwrap()) } @@ -273,6 +278,10 @@ impl Client { SChatMessage::PACKET_ID => { self.handle_chat_message(server, SChatMessage::read(bytebuf).unwrap()) } + SClientInformationPlay::PACKET_ID => self.handle_client_information_play( + server, + SClientInformationPlay::read(bytebuf).unwrap(), + ), _ => log::error!("Failed to handle player packet id {}", packet.id.0), } } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 1b8c943b0..615cd15f8 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -6,8 +6,8 @@ use pumpkin_protocol::{ CUpdateEntityRot, }, server::play::{ - SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition, - SPlayerPositionRotation, SPlayerRotation, SSwingArm, + SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SPlayerCommand, + SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SSwingArm, }, VarInt, }; @@ -15,12 +15,12 @@ use pumpkin_text::TextComponent; use crate::{ commands::{handle_command, CommandSender}, - entity::player::Hand, + entity::player::{ChatMode, Hand}, server::Server, util::math::wrap_degrees, }; -use super::Client; +use super::{Client, PlayerConfig}; /// Handles all Play Packets send by a real Player impl Client { @@ -239,4 +239,21 @@ impl Client { ), ) */ } + + pub fn handle_client_information_play( + &mut self, + _server: &mut Server, + client_information: SClientInformationPlay, + ) { + self.config = Some(PlayerConfig { + locale: client_information.locale, + view_distance: client_information.view_distance, + chat_mode: ChatMode::from_i32(client_information.chat_mode.into()).unwrap(), + chat_colors: client_information.chat_colors, + skin_parts: client_information.skin_parts, + main_hand: Hand::from_i32(client_information.main_hand.into()).unwrap(), + text_filtering: client_information.text_filtering, + server_listing: client_information.server_listing, + }); + } } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 134d7bfa1..c3ba2f68d 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -13,12 +13,17 @@ use mio::{event::Event, Poll, Token}; use num_traits::ToPrimitive; use pumpkin_entity::{entity_type::EntityType, EntityId}; use pumpkin_protocol::{ - bytebuf::ByteBuffer, client::{ + bytebuf::ByteBuffer, + client::{ config::CPluginMessage, play::{ - CCenterChunk, CChunkData, CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, CPlayerInfoUpdate, CRemoveEntities, CRemovePlayerInfo, CSetEntityMetadata, CSpawnEntity, Metadata, PlayerAction + CCenterChunk, CChunkData, CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, + CPlayerInfoUpdate, CRemoveEntities, CRemovePlayerInfo, CSetEntityMetadata, + CSpawnEntity, Metadata, PlayerAction, }, - }, uuid::UUID, BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL + }, + uuid::UUID, + BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL, }; use pumpkin_world::dimension::Dimension; @@ -329,9 +334,7 @@ impl Server { let chunks = Dimension::OverWorld .into_level( // TODO: load form config - "./world" - .parse() - .unwrap(), + "./world".parse().unwrap(), ) .read_chunks(wanted_chunks) .await; @@ -342,22 +345,25 @@ impl Server { }); chunks.iter().for_each(|chunk| { - if chunk.0 == (0,0) { - let mut test = ByteBuffer::empty(); - CChunkData(chunk.1.as_ref().unwrap()).write(&mut test); - let len = test.buf().len(); - dbg!("Chunk packet size: {}B {}KB {}MB", len, len/1024, len/(1024*1024)); - } + if chunk.0 == (0, 0) { + let mut test = ByteBuffer::empty(); + CChunkData(chunk.1.as_ref().unwrap()).write(&mut test); + let len = test.buf().len(); + dbg!( + "Chunk packet size: {}B {}KB {}MB", + len, + len / 1024, + len / (1024 * 1024) + ); + } match &chunk.1 { - Err(err) => println!( - "Chunk loading failed for chunk ({},{}): {}", - chunk.0 .0, chunk.0 .1, err - ), - Ok(data) => client.send_packet(&CChunkData(data)), - }}); - - - + Err(err) => println!( + "Chunk loading failed for chunk ({},{}): {}", + chunk.0 .0, chunk.0 .1, err + ), + Ok(data) => client.send_packet(&CChunkData(data)), + } + }); // let test_chunk = TestChunk::new(); // client.send_packet(CChunkDataUpdateLight::new(