From f7d89a28aa09c79c0685aa0fb762bea39dc75400 Mon Sep 17 00:00:00 2001 From: timvisee Date: Sun, 14 Nov 2021 15:34:55 +0100 Subject: [PATCH] Play sound effect in lobby when server is ready --- Cargo.lock | 4 ++-- src/lobby.rs | 37 +++++++++++++++++++++++++++++++++---- src/proto.rs | 1 + 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a90fad3..e7c95ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -684,7 +684,7 @@ checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "minecraft-protocol" version = "0.1.0" -source = "git+https://github.com/timvisee/rust-minecraft-protocol?branch=lazymc-v1_17_1#bef4fa8c009857e3753d85d8e2df1f71c3ae08f6" +source = "git+https://github.com/timvisee/rust-minecraft-protocol?branch=lazymc-v1_17_1#d26a525c7b29b61d2db64805181fb5471ea4317a" dependencies = [ "byteorder", "minecraft-protocol-derive", @@ -697,7 +697,7 @@ dependencies = [ [[package]] name = "minecraft-protocol-derive" version = "0.0.0" -source = "git+https://github.com/timvisee/rust-minecraft-protocol?branch=lazymc-v1_17_1#bef4fa8c009857e3753d85d8e2df1f71c3ae08f6" +source = "git+https://github.com/timvisee/rust-minecraft-protocol?branch=lazymc-v1_17_1#d26a525c7b29b61d2db64805181fb5471ea4317a" dependencies = [ "proc-macro2", "quote", diff --git a/src/lobby.rs b/src/lobby.rs index cf96361..0c36082 100644 --- a/src/lobby.rs +++ b/src/lobby.rs @@ -11,8 +11,8 @@ use minecraft_protocol::encoder::Encoder; use minecraft_protocol::version::v1_14_4::handshake::Handshake; use minecraft_protocol::version::v1_14_4::login::{LoginStart, LoginSuccess}; use minecraft_protocol::version::v1_17_1::game::{ - ClientBoundKeepAlive, JoinGame, PlayerPositionAndLook, PluginMessage, Respawn, - SetTitleSubtitle, SetTitleText, SetTitleTimes, TimeUpdate, + ClientBoundKeepAlive, JoinGame, NamedSoundEffect, PlayerPositionAndLook, PluginMessage, + Respawn, SetTitleSubtitle, SetTitleText, SetTitleTimes, TimeUpdate, }; use nbt::CompoundTag; use tokio::io::{self, AsyncWriteExt}; @@ -32,6 +32,8 @@ use crate::server::{Server, State}; pub const USE_LOBBY: bool = true; pub const DONT_START_SERVER: bool = false; const STARTING_BANNER: &str = "§2Server is starting\n§7⌛ Please wait..."; +const JOIN_SOUND: bool = true; +const JOIN_SOUND_NAME: &str = "block.note_block.chime"; /// Interval for server state polling when waiting on server to come online. const SERVER_POLL_INTERVAL: Duration = Duration::from_millis(500); @@ -123,13 +125,18 @@ pub async fn serve( // Grab join game packet from server let join_game = wait_for_server_join_game(&mut outbound, &mut server_buf).await?; + // Reset lobby title, play sound effect + send_lobby_title(&mut writer, "").await?; + if JOIN_SOUND { + send_lobby_sound_effect(&mut writer).await?; + } + // Wait a second because Notchian servers are slow // See: https://wiki.vg/Protocol#Login_Success trace!(target: "lazymc::lobby", "Waiting a second before relaying client connection..."); time::sleep(SERVER_WARMUP).await; - // Reset our lobby title, send respawn packet to teleport to real server world - send_lobby_title(&mut writer, "").await?; + // Send respawn packet, initiates teleport to real server world send_respawn_from_join(&mut writer, join_game).await?; // Drain inbound connection so we don't confuse the server @@ -387,6 +394,28 @@ async fn send_lobby_title(writer: &mut WriteHalf<'_>, text: &str) -> Result<(), Ok(()) } +/// Send lobby ready sound effect to client. +async fn send_lobby_sound_effect(writer: &mut WriteHalf<'_>) -> Result<(), ()> { + let packet = NamedSoundEffect { + sound_name: JOIN_SOUND_NAME.into(), + sound_category: 0, + effect_pos_x: 0, + effect_pos_y: 0, + effect_pos_z: 0, + volume: 1.0, + pitch: 1.0, + }; + + let mut data = Vec::new(); + packet.encode(&mut data).map_err(|_| ())?; + + let response = + RawPacket::new(proto::packets::play::CLIENT_NAMED_SOUND_EFFECT, data).encode()?; + writer.write_all(&response).await.map_err(|_| ())?; + + Ok(()) +} + /// Send respawn packet to client to jump from lobby into now loaded server. /// /// The required details will be fetched from the `join_game` packet as provided by the server. diff --git a/src/proto.rs b/src/proto.rs index 877c900..13bb7df 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -46,6 +46,7 @@ pub mod packets { pub mod play { pub const CLIENT_CHAT_MSG: i32 = 0x0F; pub const CLIENT_PLUGIN_MESSAGE: i32 = 0x18; + pub const CLIENT_NAMED_SOUND_EFFECT: i32 = 0x19; pub const CLIENT_DISCONNECT: i32 = 0x1A; pub const CLIENT_KEEP_ALIVE: i32 = 0x21; pub const CLIENT_JOIN_GAME: i32 = 0x26;