From 65c2207c0ae76024866f754389afd8b36ef67771 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 12 Aug 2024 15:09:34 +0200 Subject: [PATCH] Add Player Tab list --- .../src/client/play/c_player_remove.rs | 20 ++++++++++++ pumpkin-protocol/src/client/play/mod.rs | 2 ++ pumpkin-protocol/src/lib.rs | 1 + pumpkin-protocol/src/uuid.rs | 12 +++++++ pumpkin/src/client/mod.rs | 4 +-- pumpkin/src/server.rs | 31 ++++++++++++------- 6 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 pumpkin-protocol/src/client/play/c_player_remove.rs create mode 100644 pumpkin-protocol/src/uuid.rs diff --git a/pumpkin-protocol/src/client/play/c_player_remove.rs b/pumpkin-protocol/src/client/play/c_player_remove.rs new file mode 100644 index 00000000..7cd5db84 --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_player_remove.rs @@ -0,0 +1,20 @@ +use pumpkin_macros::packet; +use serde::Serialize; + +use crate::{uuid::UUID, VarInt}; + +#[derive(Serialize, Clone)] +#[packet(0x3D)] +pub struct CRemovePlayerInfo<'a> { + players_count: VarInt, + players: &'a [UUID], +} + +impl<'a> CRemovePlayerInfo<'a> { + pub fn new(players_count: VarInt, players: &'a [UUID]) -> Self { + Self { + players_count, + players, + } + } +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 84b92e1d..c7eba0bb 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -12,6 +12,7 @@ mod c_play_disconnect; mod c_player_abilities; mod c_player_chat_message; mod c_player_info_update; +mod c_player_remove; mod c_remove_entities; mod c_set_held_item; mod c_set_title; @@ -38,6 +39,7 @@ pub use c_play_disconnect::*; pub use c_player_abilities::*; pub use c_player_chat_message::*; pub use c_player_info_update::*; +pub use c_player_remove::*; pub use c_remove_entities::*; pub use c_set_held_item::*; pub use c_set_title::*; diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index 2fe1b7cd..70af5148 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -8,6 +8,7 @@ use thiserror::Error; pub mod bytebuf; pub mod client; pub mod server; +pub mod uuid; pub mod packet_decoder; pub mod packet_encoder; diff --git a/pumpkin-protocol/src/uuid.rs b/pumpkin-protocol/src/uuid.rs new file mode 100644 index 00000000..49691bec --- /dev/null +++ b/pumpkin-protocol/src/uuid.rs @@ -0,0 +1,12 @@ +use serde::Serialize; + +pub struct UUID(pub uuid::Uuid); + +impl Serialize for UUID { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_bytes(self.0.as_bytes()) + } +} diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index ac355d67..9f60a893 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -294,9 +294,7 @@ impl Client { } Ok(n) => { bytes_read += n; - if bytes_read == received_data.len() { - received_data.resize(received_data.len() + 1024, 0); - } + received_data.extend(&vec![0; n]); } // Would block "errors" are the OS's way of saying that the // connection is not actually ready to perform this I/O operation. diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index fc2c4738..26ceddcd 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -17,9 +17,10 @@ use pumpkin_protocol::{ config::CPluginMessage, play::{ CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, CPlayerInfoUpdate, - CRemoveEntities, CSpawnEntity, PlayerAction, + CRemoveEntities, CRemovePlayerInfo, CSpawnEntity, PlayerAction, }, }, + uuid::UUID, BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL, }; use pumpkin_registry::Registry; @@ -128,6 +129,8 @@ impl Server { // todo: put this into the entitiy struct if client.is_player() { let id = client.player.as_ref().unwrap().entity_id(); + let uuid = client.gameprofile.as_ref().unwrap().id; + self.broadcast_packet(&mut client, CRemovePlayerInfo::new(1.into(), &[UUID(uuid)])); self.broadcast_packet(&mut client, CRemoveEntities::new(&[id.into()])) } } @@ -185,13 +188,16 @@ impl Server { self.broadcast_packet( client, CPlayerInfoUpdate::new( - 0x01, + 0x01 | 0x08, &[pumpkin_protocol::client::play::Player { uuid: gameprofile.id, - actions: vec![PlayerAction::AddPlayer { - name: gameprofile.name.clone(), - properties: gameprofile.properties.clone(), - }], + actions: vec![ + PlayerAction::AddPlayer { + name: gameprofile.name.clone(), + properties: gameprofile.properties.clone(), + }, + PlayerAction::UpdateListed { listed: true }, + ], }], ), ); @@ -204,14 +210,17 @@ impl Server { let gameprofile = client.gameprofile.as_ref().unwrap(); entries.push(pumpkin_protocol::client::play::Player { uuid: gameprofile.id, - actions: vec![PlayerAction::AddPlayer { - name: gameprofile.name.clone(), - properties: gameprofile.properties.clone(), - }], + actions: vec![ + PlayerAction::AddPlayer { + name: gameprofile.name.clone(), + properties: gameprofile.properties.clone(), + }, + PlayerAction::UpdateListed { listed: true }, + ], }) } } - client.send_packet(CPlayerInfoUpdate::new(0x01, &entries)); + client.send_packet(CPlayerInfoUpdate::new(0x01 | 0x08, &entries)); // Start waiting for level chunks client.send_packet(CGameEvent::new(13, 0.0));