From a6d9ed216d58ee3f11b19cea5f736c046b2500ba Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 19 Aug 2024 20:05:18 +0200 Subject: [PATCH] Use Cow's in TextComponent We now don't always convert &str to Strings allowing us to use stack allocations --- pumpkin-protocol/src/bytebuf/packet_id.rs | 2 +- .../src/client/config/c_add_resource_pack.rs | 4 +- .../src/client/config/c_plugin_message.rs | 1 - .../src/client/play/c_actionbar.rs | 8 +- .../client/play/c_disguised_chat_message.rs | 16 ++-- pumpkin-protocol/src/client/play/c_login.rs | 6 +- .../src/client/play/c_open_screen.rs | 8 +- .../src/client/play/c_play_disconnect.rs | 8 +- .../src/client/play/c_player_chat_message.rs | 22 +++--- .../src/client/play/c_set_title.rs | 8 +- .../src/client/play/c_subtitle.rs | 8 +- .../src/client/play/c_system_chat_message.rs | 8 +- pumpkin-protocol/src/lib.rs | 2 +- .../src/server/play/s_chat_message.rs | 6 +- .../src/server/play/s_interact.rs | 1 - pumpkin-registry/src/chat_type.rs | 2 +- pumpkin-text/src/click.rs | 12 +-- pumpkin-text/src/hover.rs | 14 ++-- pumpkin-text/src/lib.rs | 74 ++++++++----------- pumpkin-text/src/style.rs | 12 +-- pumpkin/src/client/client_packet.rs | 3 +- pumpkin/src/client/mod.rs | 2 +- pumpkin/src/client/player_packet.rs | 37 ++++------ pumpkin/src/commands/gamemode.rs | 15 ++-- pumpkin/src/commands/mod.rs | 2 +- pumpkin/src/commands/pumpkin.rs | 2 +- 26 files changed, 134 insertions(+), 149 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/packet_id.rs b/pumpkin-protocol/src/bytebuf/packet_id.rs index f5dde8413..635a8051e 100644 --- a/pumpkin-protocol/src/bytebuf/packet_id.rs +++ b/pumpkin-protocol/src/bytebuf/packet_id.rs @@ -7,7 +7,7 @@ use crate::{BitSet, ClientPacket, ServerPacket, VarInt, VarIntType}; use super::{deserializer, serializer, ByteBuffer, DeserializerError}; -impl Serialize for BitSet<'static> { +impl<'a> Serialize for BitSet<'a> { fn serialize(&self, serializer: S) -> Result where S: Serializer, diff --git a/pumpkin-protocol/src/client/config/c_add_resource_pack.rs b/pumpkin-protocol/src/client/config/c_add_resource_pack.rs index 6a112e36e..ae8212aba 100644 --- a/pumpkin-protocol/src/client/config/c_add_resource_pack.rs +++ b/pumpkin-protocol/src/client/config/c_add_resource_pack.rs @@ -11,7 +11,7 @@ pub struct CConfigAddResourcePack<'a> { url: &'a str, hash: &'a str, // max 40 forced: bool, - prompt_message: Option, + prompt_message: Option>, } impl<'a> CConfigAddResourcePack<'a> { @@ -20,7 +20,7 @@ impl<'a> CConfigAddResourcePack<'a> { url: &'a str, hash: &'a str, forced: bool, - prompt_message: Option, + prompt_message: Option>, ) -> Self { Self { uuid, diff --git a/pumpkin-protocol/src/client/config/c_plugin_message.rs b/pumpkin-protocol/src/client/config/c_plugin_message.rs index bdb7692b4..01f40580b 100644 --- a/pumpkin-protocol/src/client/config/c_plugin_message.rs +++ b/pumpkin-protocol/src/client/config/c_plugin_message.rs @@ -1,7 +1,6 @@ use pumpkin_macros::packet; use serde::Serialize; -use crate::{bytebuf::ByteBuffer, ClientPacket}; #[derive(Serialize)] #[packet(0x01)] diff --git a/pumpkin-protocol/src/client/play/c_actionbar.rs b/pumpkin-protocol/src/client/play/c_actionbar.rs index d9046de06..fb8fe68ba 100644 --- a/pumpkin-protocol/src/client/play/c_actionbar.rs +++ b/pumpkin-protocol/src/client/play/c_actionbar.rs @@ -4,12 +4,12 @@ use serde::Serialize; #[derive(Serialize)] #[packet(0x4C)] -pub struct CActionBar { - action_bar: TextComponent, +pub struct CActionBar<'a> { + action_bar: TextComponent<'a>, } -impl CActionBar { - pub fn new(action_bar: TextComponent) -> Self { +impl<'a> CActionBar<'a> { + pub fn new(action_bar: TextComponent<'a>) -> Self { Self { action_bar } } } diff --git a/pumpkin-protocol/src/client/play/c_disguised_chat_message.rs b/pumpkin-protocol/src/client/play/c_disguised_chat_message.rs index c1228080c..65b43b9b8 100644 --- a/pumpkin-protocol/src/client/play/c_disguised_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_disguised_chat_message.rs @@ -6,19 +6,19 @@ use crate::VarInt; #[derive(Serialize)] #[packet(0x1E)] -pub struct CDisguisedChatMessage { - message: TextComponent, +pub struct CDisguisedChatMessage<'a> { + message: TextComponent<'a>, chat_type: VarInt, - sender_name: TextComponent, - target_name: Option, + sender_name: TextComponent<'a>, + target_name: Option>, } -impl CDisguisedChatMessage { +impl<'a> CDisguisedChatMessage<'a> { pub fn new( - message: TextComponent, + message: TextComponent<'a>, chat_type: VarInt, - sender_name: TextComponent, - target_name: Option, + sender_name: TextComponent<'a>, + target_name: Option>, ) -> Self { Self { message, diff --git a/pumpkin-protocol/src/client/play/c_login.rs b/pumpkin-protocol/src/client/play/c_login.rs index 11f923347..0e0b12224 100644 --- a/pumpkin-protocol/src/client/play/c_login.rs +++ b/pumpkin-protocol/src/client/play/c_login.rs @@ -1,7 +1,7 @@ use pumpkin_macros::packet; use serde::Serialize; -use crate::VarInt; +use crate::{position::WorldPosition, VarInt}; #[derive(Serialize)] #[packet(0x2B)] @@ -23,7 +23,7 @@ pub struct CLogin<'a> { previous_gamemode: i8, debug: bool, is_flat: bool, - death_dimension_name: Option<(String, i64)>, // POSITION NOT STRING + death_dimension_name: Option<(WorldPosition, i64)>, portal_cooldown: VarInt, enforce_secure_chat: bool, } @@ -47,7 +47,7 @@ impl<'a> CLogin<'a> { previous_gamemode: i8, debug: bool, is_flat: bool, - death_dimension_name: Option<(String, i64)>, + death_dimension_name: Option<(WorldPosition, i64)>, portal_cooldown: VarInt, enforce_secure_chat: bool, ) -> Self { diff --git a/pumpkin-protocol/src/client/play/c_open_screen.rs b/pumpkin-protocol/src/client/play/c_open_screen.rs index 1b5905230..7c5b07a22 100644 --- a/pumpkin-protocol/src/client/play/c_open_screen.rs +++ b/pumpkin-protocol/src/client/play/c_open_screen.rs @@ -6,14 +6,14 @@ use crate::VarInt; #[derive(Serialize)] #[packet(0x33)] -pub struct COpenScreen { +pub struct COpenScreen<'a> { window_id: VarInt, window_type: VarInt, - window_title: TextComponent, + window_title: TextComponent<'a>, } -impl COpenScreen { - pub fn new(window_id: VarInt, window_type: VarInt, window_title: TextComponent) -> Self { +impl<'a> COpenScreen<'a> { + pub fn new(window_id: VarInt, window_type: VarInt, window_title: TextComponent<'a>) -> Self { Self { window_id, window_type, diff --git a/pumpkin-protocol/src/client/play/c_play_disconnect.rs b/pumpkin-protocol/src/client/play/c_play_disconnect.rs index e5aa3f197..dd861d732 100644 --- a/pumpkin-protocol/src/client/play/c_play_disconnect.rs +++ b/pumpkin-protocol/src/client/play/c_play_disconnect.rs @@ -4,12 +4,12 @@ use serde::Serialize; #[derive(Serialize)] #[packet(0x1D)] -pub struct CPlayDisconnect { - reason: TextComponent, +pub struct CPlayDisconnect<'a> { + reason: TextComponent<'a>, } -impl CPlayDisconnect { - pub fn new(reason: TextComponent) -> Self { +impl<'a> CPlayDisconnect<'a> { + pub fn new(reason: TextComponent<'a>) -> Self { Self { reason } } } diff --git a/pumpkin-protocol/src/client/play/c_player_chat_message.rs b/pumpkin-protocol/src/client/play/c_player_chat_message.rs index 47726a1f3..ade16d855 100644 --- a/pumpkin-protocol/src/client/play/c_player_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_player_chat_message.rs @@ -11,19 +11,18 @@ pub struct CPlayerChatMessage<'a> { sender: UUID, index: VarInt, message_signature: Option<&'a [u8]>, - message: String, + message: &'a str, timestamp: i64, salt: i64, previous_messages_count: VarInt, previous_messages: &'a [PreviousMessage<'a>], // max 20 - unsigned_content: Option, + unsigned_content: Option>, /// See `FilterType` filter_type: VarInt, - // TODO: THIS IS A HACK, We currently don't support writing or reading bitsets - filter_type_bits: bool, + filter_type_bits: Option>, chat_type: VarInt, - sender_name: TextComponent, - target_name: Option, + sender_name: TextComponent<'a>, + target_name: Option>, } impl<'a> CPlayerChatMessage<'a> { @@ -32,15 +31,16 @@ impl<'a> CPlayerChatMessage<'a> { sender: UUID, index: VarInt, message_signature: Option<&'a [u8]>, - message: String, + message: &'a str, timestamp: i64, salt: i64, previous_messages: &'a [PreviousMessage<'a>], - unsigned_content: Option, + unsigned_content: Option>, filter_type: VarInt, + filter_type_bits: Option>, chat_type: VarInt, - sender_name: TextComponent, - target_name: Option, + sender_name: TextComponent<'a>, + target_name: Option>, ) -> Self { Self { sender, @@ -53,7 +53,7 @@ impl<'a> CPlayerChatMessage<'a> { previous_messages, unsigned_content, filter_type, - filter_type_bits: false, + filter_type_bits, chat_type, sender_name, target_name, diff --git a/pumpkin-protocol/src/client/play/c_set_title.rs b/pumpkin-protocol/src/client/play/c_set_title.rs index 7a549b904..4e043da0f 100644 --- a/pumpkin-protocol/src/client/play/c_set_title.rs +++ b/pumpkin-protocol/src/client/play/c_set_title.rs @@ -4,12 +4,12 @@ use serde::Serialize; #[derive(Serialize)] #[packet(0x65)] -pub struct CTitleText { - title: TextComponent, +pub struct CTitleText<'a> { + title: TextComponent<'a>, } -impl CTitleText { - pub fn new(title: TextComponent) -> Self { +impl<'a> CTitleText<'a> { + pub fn new(title: TextComponent<'a>) -> Self { Self { title } } } diff --git a/pumpkin-protocol/src/client/play/c_subtitle.rs b/pumpkin-protocol/src/client/play/c_subtitle.rs index 35ebbe50b..10f86d1ef 100644 --- a/pumpkin-protocol/src/client/play/c_subtitle.rs +++ b/pumpkin-protocol/src/client/play/c_subtitle.rs @@ -4,12 +4,12 @@ use serde::Serialize; #[derive(Serialize)] #[packet(0x63)] -pub struct CSubtitle { - subtitle: TextComponent, +pub struct CSubtitle<'a> { + subtitle: TextComponent<'a>, } -impl CSubtitle { - pub fn new(subtitle: TextComponent) -> Self { +impl<'a> CSubtitle<'a> { + pub fn new(subtitle: TextComponent<'a>) -> Self { Self { subtitle } } } diff --git a/pumpkin-protocol/src/client/play/c_system_chat_message.rs b/pumpkin-protocol/src/client/play/c_system_chat_message.rs index d0d2727eb..fd88e622a 100644 --- a/pumpkin-protocol/src/client/play/c_system_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_system_chat_message.rs @@ -4,13 +4,13 @@ use serde::Serialize; #[derive(Serialize)] #[packet(0x6C)] -pub struct CSystemChatMessge { - content: TextComponent, +pub struct CSystemChatMessge<'a> { + content: TextComponent<'a>, overlay: bool, } -impl CSystemChatMessge { - pub fn new(content: TextComponent, overlay: bool) -> Self { +impl<'a> CSystemChatMessge<'a> { + pub fn new(content: TextComponent<'a>, overlay: bool) -> Self { Self { content, overlay } } } diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index f0c1a6711..fda17b544 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -1,6 +1,6 @@ use bytebuf::{packet_id::Packet, ByteBuffer, DeserializerError}; use bytes::Buf; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::io::{self, Write}; use thiserror::Error; diff --git a/pumpkin-protocol/src/server/play/s_chat_message.rs b/pumpkin-protocol/src/server/play/s_chat_message.rs index 51d47bd17..bcd7dd91a 100644 --- a/pumpkin-protocol/src/server/play/s_chat_message.rs +++ b/pumpkin-protocol/src/server/play/s_chat_message.rs @@ -2,7 +2,7 @@ use pumpkin_macros::packet; use crate::{ bytebuf::{ByteBuffer, DeserializerError}, - ServerPacket, + ServerPacket, VarInt, }; // derive(Deserialize)] @@ -12,7 +12,7 @@ pub struct SChatMessage { pub timestamp: i64, pub salt: i64, pub signature: Option>, - // pub messagee_count: VarInt, + pub messagee_count: VarInt, // acknowledged: BitSet, } @@ -24,7 +24,7 @@ impl ServerPacket for SChatMessage { timestamp: bytebuf.get_i64(), salt: bytebuf.get_i64(), signature: bytebuf.get_option(|v| v.get_slice().to_vec()), - //messagee_count: bytebuf.get_var_int(), + messagee_count: bytebuf.get_var_int(), }) } } diff --git a/pumpkin-protocol/src/server/play/s_interact.rs b/pumpkin-protocol/src/server/play/s_interact.rs index c141358b9..a3c31db5d 100644 --- a/pumpkin-protocol/src/server/play/s_interact.rs +++ b/pumpkin-protocol/src/server/play/s_interact.rs @@ -1,4 +1,3 @@ -use std::char::MAX; use num_derive::FromPrimitive; use num_traits::FromPrimitive; diff --git a/pumpkin-registry/src/chat_type.rs b/pumpkin-registry/src/chat_type.rs index 45f324c26..e5d533527 100644 --- a/pumpkin-registry/src/chat_type.rs +++ b/pumpkin-registry/src/chat_type.rs @@ -11,7 +11,7 @@ pub struct ChatType { pub struct Decoration { translation_key: String, #[serde(default, skip_serializing_if = "Option::is_none")] - style: Option