Skip to content

Commit

Permalink
Add Clientbound Client Informations (Play)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 12, 2024
1 parent 1ab05e0 commit cfa2077
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 45 deletions.
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/c_center_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use crate::VarInt;
#[derive(serde::Serialize)]
#[packet(0x54)]
pub struct CCenterChunk {
pub chunk_x: VarInt,
pub chunk_z: VarInt
}
pub chunk_x: VarInt,
pub chunk_z: VarInt,
}
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/packet_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/server/config/s_client_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pumpkin-protocol/src/server/play/c_client_information.rs
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod c_client_information;
mod s_chat_command;
mod s_chat_message;
mod s_confirm_teleport;
Expand All @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 3 additions & 3 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 18 additions & 9 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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),
}
}
Expand Down
25 changes: 21 additions & 4 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use pumpkin_protocol::{
CUpdateEntityRot,
},
server::play::{
SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSwingArm,
SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SPlayerCommand,
SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SSwingArm,
},
VarInt,
};
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 {
Expand Down Expand Up @@ -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,
});
}
}
48 changes: 27 additions & 21 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down

0 comments on commit cfa2077

Please sign in to comment.