From 340318b9b6cfa863a8065dae2cd3d483ac027209 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 1 Jul 2022 19:18:59 -0700 Subject: [PATCH] Get system chat working --- examples/terrain.rs | 4 ++-- src/client.rs | 46 +++++++++++++++++++++++++++-------------- src/protocol/packets.rs | 39 ++++++++++++++++++---------------- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/examples/terrain.rs b/examples/terrain.rs index 93e107c..9808c4d 100644 --- a/examples/terrain.rs +++ b/examples/terrain.rs @@ -6,7 +6,7 @@ use log::LevelFilter; use noise::{NoiseFn, Seedable, SuperSimplex}; use rayon::iter::ParallelIterator; use valence::block::{BlockState, PropName, PropValue}; -use valence::client::{GameMode, PlayerChatType}; +use valence::client::GameMode; use valence::config::{Config, ServerListPing}; use valence::text::Color; use valence::util::chunks_in_view_distance; @@ -114,7 +114,7 @@ impl Config for Game { None, ); - client.send_message("welcome!", PlayerChatType::Chat); + client.send_message("Welcome to the terrain example!".italic()); } let dist = client.view_distance(); diff --git a/src/client.rs b/src/client.rs index 7c583ae..3ea3e41 100644 --- a/src/client.rs +++ b/src/client.rs @@ -16,16 +16,15 @@ use crate::entity::types::Player; use crate::entity::{velocity_to_packet_units, EntityType}; use crate::player_textures::SignedPlayerTextures; use crate::protocol::packets::play::c2s::{C2sPlayPacket, DiggingStatus, InteractType}; -pub use crate::protocol::packets::play::s2c::PlayerChatType; use crate::protocol::packets::play::s2c::{ Biome as BiomeRegistryBiome, BiomeAdditionsSound, BiomeEffects, BiomeMoodSound, BiomeMusic, - BiomeParticle, BiomeParticleOptions, BiomeProperty, BiomeRegistry, BlockChangeAck, - ChatTypeRegistry, DimensionType, DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, - ForgetLevelChunk, GameEvent, GameEventReason, KeepAlive, Login, MoveEntityPosition, - MoveEntityPositionAndRotation, MoveEntityRotation, PlayerPosition, PlayerPositionFlags, - RegistryCodec, RemoveEntities, RotateHead, S2cPlayPacket, SetChunkCacheCenter, - SetChunkCacheRadius, SetEntityMetadata, SetEntityMotion, SpawnPosition, SystemChat, - TeleportEntity, + BiomeParticle, BiomeParticleOptions, BiomeProperty, BiomeRegistry, BlockChangeAck, ChatType, + ChatTypeChat, ChatTypeNarration, ChatTypeRegistry, ChatTypeRegistryEntry, DimensionType, + DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, ForgetLevelChunk, GameEvent, + GameEventReason, KeepAlive, Login, MoveEntityPosition, MoveEntityPositionAndRotation, + MoveEntityRotation, PlayerPosition, PlayerPositionFlags, RegistryCodec, RemoveEntities, + RotateHead, S2cPlayPacket, SetChunkCacheCenter, SetChunkCacheRadius, SetEntityMetadata, + SetEntityMotion, SpawnPosition, SystemChat, TeleportEntity, }; use crate::protocol::{BoundedInt, ByteAngle, Nbt, RawBytes, VarInt}; use crate::server::C2sPacketChannels; @@ -136,7 +135,7 @@ pub struct Client { old_game_mode: GameMode, settings: Option, dug_blocks: Vec, - msgs_to_send: Vec<(Text, PlayerChatType)>, + msgs_to_send: Vec, /// The metadata for the client's own player entity. player_meta: Player, } @@ -200,8 +199,9 @@ impl Client { self.textures.as_ref() } - pub fn send_message(&mut self, msg: impl Into, typ: PlayerChatType) { - self.msgs_to_send.push((msg.into(), typ)); + /// Sends a system message to the player. + pub fn send_message(&mut self, msg: impl Into) { + self.msgs_to_send.push(msg.into()); } pub fn position(&self) -> Vec3 { @@ -727,9 +727,14 @@ impl Client { }); } - for (msg, typ) in self.msgs_to_send.drain(..) { - // TODO: wont work without proper chat registry. - send_packet(&mut self.send, SystemChat { chat: msg, typ }); + for msg in self.msgs_to_send.drain(..) { + send_packet( + &mut self.send, + SystemChat { + chat: msg, + typ: VarInt(0), + }, + ); } let mut entities_to_unload = Vec::new(); @@ -917,7 +922,7 @@ fn make_dimension_codec(server: &Server) -> RegistryCodec { assert_eq!(biome.name, ident!("plains")); biomes.push(to_biome_registry_item(&biome, 0)); } - + RegistryCodec { dimension_type_registry: DimensionTypeRegistry { typ: ident!("dimension_type"), @@ -929,7 +934,16 @@ fn make_dimension_codec(server: &Server) -> RegistryCodec { }, chat_type_registry: ChatTypeRegistry { typ: ident!("chat_type"), - value: Vec::new(), + value: vec![ChatTypeRegistryEntry { + name: ident!("system"), + id: 0, + element: ChatType { + chat: ChatTypeChat {}, + narration: ChatTypeNarration { + priority: "system".to_string(), + }, + }, + }], }, } } diff --git a/src/protocol/packets.rs b/src/protocol/packets.rs index ec9d4b5..944210e 100644 --- a/src/protocol/packets.rs +++ b/src/protocol/packets.rs @@ -993,7 +993,23 @@ pub mod play { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ChatTypeRegistryEntry { - // TODO + pub name: Ident, + pub id: i32, + pub element: ChatType, + } + + #[derive(Clone, Debug, Serialize, Deserialize)] + pub struct ChatType { + pub chat: ChatTypeChat, + pub narration: ChatTypeNarration, + } + + #[derive(Clone, Debug, Serialize, Deserialize)] + pub struct ChatTypeChat {} + + #[derive(Clone, Debug, Serialize, Deserialize)] + pub struct ChatTypeNarration { + pub priority: String, } def_enum! { @@ -1037,27 +1053,13 @@ pub mod play { def_struct! { PlayerChat 0x30 { message: Text, - typ: PlayerChatType, + /// Index into the chat type registry + typ: VarInt, sender: Uuid, // TODO more fields } } - def_enum! { - #[derive(Copy, PartialEq, Eq, Default)] - PlayerChatType: VarInt { - #[default] - Chat = 0, - SystemMessage = 1, - GameInfo = 2, - SayCommand = 3, - MsgCommand = 4, - TeamMsgCommand = 5, - EmoteCommand = 6, - TellrawCommand = 7, - } - } - def_enum! { PlayerInfo 0x34: VarInt { AddPlayer: Vec = 0, @@ -1176,7 +1178,8 @@ pub mod play { def_struct! { SystemChat 0x5f { chat: Text, - typ: PlayerChatType, + /// Index into the chat type registry. + typ: VarInt, } }