Skip to content

Commit

Permalink
Prepare ChatType registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 11, 2024
1 parent 41442c5 commit 5b63611
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 46 deletions.
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/login/c_login_success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{bytebuf::ByteBuffer, ClientPacket, Property};
#[packet(0x02)]
pub struct CLoginSuccess<'a> {
pub uuid: uuid::Uuid,
pub username: String, // 16
pub username: &'a str, // 16
pub properties: &'a [Property],
pub strict_error_handling: bool,
}

impl<'a> CLoginSuccess<'a> {
pub fn new(
uuid: uuid::Uuid,
username: String,
username: &'a str,
properties: &'a [Property],
strict_error_handling: bool,
) -> Self {
Expand Down
12 changes: 6 additions & 6 deletions pumpkin-protocol/src/client/play/c_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use crate::VarInt;

#[derive(Serialize)]
#[packet(0x2B)]
pub struct CLogin {
pub struct CLogin<'a> {
entity_id: i32,
is_hardcore: bool,
dimension_count: VarInt,
dimension_names: Vec<String>,
dimension_names: &'a [&'a str],
max_players: VarInt,
view_distance: VarInt,
simulated_distance: VarInt,
reduced_debug_info: bool,
enabled_respawn_screen: bool,
limited_crafting: bool,
dimension_type: VarInt,
dimension_name: String,
dimension_name: &'a str,
hashed_seed: i64,
game_mode: u8,
previous_gamemode: i8,
Expand All @@ -28,20 +28,20 @@ pub struct CLogin {
enforce_secure_chat: bool,
}

impl CLogin {
impl<'a> CLogin<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
entity_id: i32,
is_hardcore: bool,
dimension_names: Vec<String>,
dimension_names: &'a [&'a str],
max_players: VarInt,
view_distance: VarInt,
simulated_distance: VarInt,
reduced_debug_info: bool,
enabled_respawn_screen: bool,
limited_crafting: bool,
dimension_type: VarInt,
dimension_name: String,
dimension_name: &'a str,
hashed_seed: i64,
game_mode: u8,
previous_gamemode: i8,
Expand Down
7 changes: 6 additions & 1 deletion pumpkin-protocol/src/client/play/c_player_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pumpkin_macros::packet;
use pumpkin_text::TextComponent;
use serde::Serialize;

use crate::VarInt;
use crate::{BitSet, VarInt};

#[derive(Serialize, Clone)]
#[packet(0x39)]
Expand All @@ -14,10 +14,13 @@ pub struct CPlayerChatMessage<'a> {
message: String,
timestamp: i64,
salt: i64,
previous_messages_count: VarInt,
previous_messages: &'a [PreviousMessage<'a>], // max 20
unsigned_content: Option<TextComponent>,
/// See `FilterType`
filter_type: VarInt,
// TODO: THIS IS A HACK, We currently don't support writing or reading bitsets
filter_type_bits: bool,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
Expand Down Expand Up @@ -46,9 +49,11 @@ impl<'a> CPlayerChatMessage<'a> {
message,
timestamp,
salt,
previous_messages_count: previous_messages.len().into(),
previous_messages,
unsigned_content,
filter_type,
filter_type_bits: false,
chat_type,
sender_name,
target_name,
Expand Down
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_remove_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::VarInt;

#[derive(Serialize, Clone)]
#[packet(0x42)]
pub struct CRemoveEntities {
pub struct CRemoveEntities<'a> {
count: VarInt,
entitiy_ids: Vec<VarInt>,
entitiy_ids: &'a [VarInt],
}

impl CRemoveEntities {
pub fn new(entitiy_ids: Vec<VarInt>) -> Self {
impl<'a> CRemoveEntities<'a> {
pub fn new(entitiy_ids: &'a [VarInt]) -> Self {
Self {
count: VarInt(entitiy_ids.len() as i32),
entitiy_ids,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::{self, Write};

use bytebuf::{packet_id::Packet, ByteBuffer, DeserializerError};
use bytes::Buf;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};
use thiserror::Error;

pub mod bytebuf;
Expand Down
30 changes: 28 additions & 2 deletions pumpkin-registry/src/chat_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
pub struct ChatType {}
use serde::Serialize;

#[derive(Debug, Clone, Serialize)]
pub struct ChatType {
chat: Decoration,
narration: Decoration,
}

#[derive(Debug, Clone, Serialize)]
pub struct Decoration {
translation_key: String,
// style: Option,
style: u8,
// TODO
// style: Option<Styles>,
parameters: Vec<String>,
}

impl Default for ChatType {
fn default() -> Self {
Self {
chat: Decoration {
style: 0,
parameters: vec!["sender".into(), "content".into()],
translation_key: "chat.type.text".into(),
},
narration: Decoration {
style: 0,
parameters: vec!["sender".into(), "content".into()],
translation_key: "chat.type.text.narrate".into(),
},
}
}
}
20 changes: 19 additions & 1 deletion pumpkin-registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biomes::Biome;
use chat_type::ChatType;
use dimensions::Dimension;
use fastnbt::SerOpts;
use paint::Painting;
Expand Down Expand Up @@ -50,6 +51,16 @@ impl Registry {
.unwrap(),
}],
};

let _chat_types = Registry {
registry_id: "minecraft:chat_type".to_string(),
registry_entries: vec![RegistryEntry {
entry_id: "minecraft:chat",
data: fastnbt::to_bytes_with_opts(&ChatType::default(), SerOpts::network_nbt())
.unwrap(),
}],
};

let damage_types = Registry {
registry_id: "minecraft:damage_type".to_string(),
registry_entries: damage_type::entires(),
Expand All @@ -62,6 +73,13 @@ impl Registry {
.unwrap(),
}],
};
vec![dimensions, damage_types, biomes, wolf_variants, paintings]
vec![
dimensions,
damage_types,
biomes,
wolf_variants,
paintings,
// chat_types,
]
}
}
7 changes: 3 additions & 4 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use pumpkin_protocol::{
},
ConnectionState, KnownPack, RawBytes,
};
use pumpkin_registry::Registry;
use pumpkin_text::TextComponent;
use rsa::Pkcs1v15Encrypt;
use sha1::{Digest, Sha1};
Expand Down Expand Up @@ -150,7 +149,7 @@ impl Client {
}

if let Some(profile) = self.gameprofile.as_ref().cloned() {
let packet = CLoginSuccess::new(profile.id, profile.name, &profile.properties, false);
let packet = CLoginSuccess::new(profile.id, &profile.name, &profile.properties, false);
self.send_packet(packet);
} else {
self.kick("game profile is none");
Expand Down Expand Up @@ -225,8 +224,8 @@ impl Client {
}
}

pub fn handle_known_packs(&mut self, _server: &mut Server, _config_acknowledged: SKnownPacks) {
for registry in Registry::get_static() {
pub fn handle_known_packs(&mut self, server: &mut Server, _config_acknowledged: SKnownPacks) {
for registry in &server.cached_registry {
self.send_packet(CRegistryData::new(
&registry.registry_id,
&registry.registry_entries,
Expand Down
44 changes: 22 additions & 22 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_inventory::WindowType;
use pumpkin_protocol::{
client::play::{
Animation, CEntityAnimation, CHeadRot, COpenScreen, CSystemChatMessge, CUpdateEntityPos,
CUpdateEntityPosRot, CUpdateEntityRot,
Animation, CEntityAnimation, CHeadRot, COpenScreen, CPlayerChatMessage, CUpdateEntityPos,
CUpdateEntityPosRot, CUpdateEntityRot, FilterType,
},
server::play::{
SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
Expand Down Expand Up @@ -203,30 +203,30 @@ impl Client {
let gameprofile = self.gameprofile.as_ref().unwrap();
dbg!("got message");
// yeah a "raw system message", the ugly way to do that, but it works
// server.broadcast_packet(
// self,
// CSystemChatMessge::new(
// TextComponent::from(format!("{}: {}", gameprofile.name, message)),
// false,
// ),
// );
server.broadcast_packet(
self,
CSystemChatMessge::new(
TextComponent::from(format!("{}: {}", gameprofile.name, message)),
false,
CPlayerChatMessage::new(
gameprofile.id,
0.into(),
None,
message.clone(),
chat_message.timestamp,
chat_message.salt,
&[],
Some(TextComponent::from(message.clone())),
pumpkin_protocol::VarInt(FilterType::PassThrough as i32),
0.into(),
TextComponent::from(gameprofile.name.clone()),
None,
),
)
/*server.broadcast_packet(
self,
CPlayerChatMessage::new(
gameprofile.id,
0.into(),
None,
message.clone(),
chat_message.timestamp,
chat_message.salt,
&[],
Some(TextComponent::from(message.clone())),
pumpkin_protocol::VarInt(FilterType::PassThrough as i32),
0.into(),
TextComponent::from(gameprofile.name.clone()),
None,
),
) */
/* server.broadcast_packet(
self,
CDisguisedChatMessage::new(
Expand Down
11 changes: 8 additions & 3 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use pumpkin_protocol::{
},
BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL,
};
use pumpkin_registry::Registry;
use pumpkin_world::chunk::TestChunk;
use rsa::{traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -50,6 +51,9 @@ pub struct Server {
/// Cache the Server brand buffer so we don't have to rebuild them every time a player joins
pub cached_server_brand: Vec<u8>,

/// Cache the registry so we don't have to parse it every time a player joins
pub cached_registry: Vec<Registry>,

pub current_clients: HashMap<Rc<Token>, Rc<RefCell<Client>>>,

// TODO: replace with HashMap <World, Player>
Expand Down Expand Up @@ -89,6 +93,7 @@ impl Server {
};

Self {
cached_registry: Registry::get_static(),
// 0 is invalid
entity_id: 2.into(),
// world: World::load(""),
Expand Down Expand Up @@ -123,7 +128,7 @@ impl Server {
// todo: put this into the entitiy struct
if client.is_player() {
let id = client.player.as_ref().unwrap().entity_id();
self.broadcast_packet(&mut client, CRemoveEntities::new(vec![id.into()]))
self.broadcast_packet(&mut client, CRemoveEntities::new(&[id.into()]))
}
}

Expand All @@ -140,15 +145,15 @@ impl Server {
client.send_packet(CLogin::new(
entity_id,
self.base_config.hardcore,
vec!["minecraft:overworld".into()],
&["minecraft:overworld"],
self.base_config.max_players.into(),
self.base_config.view_distance.into(), // TODO: view distance
self.base_config.simulation_distance.into(), // TODO: sim view dinstance
false,
false,
false,
0.into(),
"minecraft:overworld".into(),
"minecraft:overworld",
0, // seed
match self.base_config.default_gamemode {
GameMode::Undefined => GameMode::Survival,
Expand Down

0 comments on commit 5b63611

Please sign in to comment.