Get system chat working

This commit is contained in:
Ryan 2022-07-01 19:18:59 -07:00
parent 0fcedd3656
commit 340318b9b6
3 changed files with 53 additions and 36 deletions

View file

@ -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();

View file

@ -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<Settings>,
dug_blocks: Vec<i32>,
msgs_to_send: Vec<(Text, PlayerChatType)>,
msgs_to_send: Vec<Text>,
/// 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<Text>, 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<Text>) {
self.msgs_to_send.push(msg.into());
}
pub fn position(&self) -> Vec3<f64> {
@ -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(),
},
},
}],
},
}
}

View file

@ -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<PlayerInfoAddPlayer> = 0,
@ -1176,7 +1178,8 @@ pub mod play {
def_struct! {
SystemChat 0x5f {
chat: Text,
typ: PlayerChatType,
/// Index into the chat type registry.
typ: VarInt,
}
}