Use Cow's in TextComponent

We now don't always convert &str to Strings allowing us to use stack allocations
This commit is contained in:
Snowiiii committed 2024-08-19 20:05:18 +02:00
1 parent dfc0b8afe9
commit a6d9ed216d
26 files changed
+134 -149

No files matched your search

+1 -2
View File
@@ -1,4 +1,3 @@
use num_traits::FromPrimitive;
use pumpkin_protocol::{
client::{
@@ -199,7 +198,7 @@ impl Client {
let prompt_message = if resource_config.prompt_message.is_empty() {
None
} else {
Some(TextComponent::from(resource_config.prompt_message.clone()))
Some(TextComponent::text(&resource_config.prompt_message))
};
self.send_packet(&CConfigAddResourcePack::new(
pumpkin_protocol::uuid::UUID(uuid::Uuid::new_v3(
+1 -1
View File
@@ -375,7 +375,7 @@ impl Client {
.unwrap_or_else(|_| self.close());
}
ConnectionState::Play => {
self.try_send_packet(&CPlayDisconnect::new(TextComponent::from(reason)))
self.try_send_packet(&CPlayDisconnect::new(TextComponent::text(reason)))
.unwrap_or_else(|_| self.close());
}
_ => {
+16 -21
View File
@@ -5,7 +5,8 @@ use pumpkin_entity::EntityId;
use pumpkin_protocol::{
client::play::{
Animation, CBlockUpdate, CEntityAnimation, CEntityVelocity, CHeadRot, CHurtAnimation,
CSystemChatMessge, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot,
CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot,
CUpdateEntityRot, FilterType,
},
position::WorldPosition,
server::play::{
@@ -209,38 +210,30 @@ impl Client {
}
pub fn handle_chat_message(&mut self, server: &mut Server, chat_message: SChatMessage) {
dbg!("got message");
let message = chat_message.message;
// TODO: filter message & validation
let gameprofile = self.gameprofile.as_ref().unwrap();
dbg!("got message");
// yeah a "raw system message", the ugly way to do that, but it works
server.broadcast_packet(
self,
&CSystemChatMessge::new(
TextComponent::from(format!("{}: {}", gameprofile.name, message)),
false,
),
);
/* server.broadcast_packet(
self,
CPlayerChatMessage::new(
&CPlayerChatMessage::new(
pumpkin_protocol::uuid::UUID(gameprofile.id),
0.into(),
None,
message.clone(),
chat_message.messagee_count,
chat_message.signature.as_deref(),
&message,
chat_message.timestamp,
chat_message.salt,
&[],
Some(TextComponent::from(message.clone())),
Some(TextComponent::text(&message)),
pumpkin_protocol::VarInt(FilterType::PassThrough as i32),
0.into(),
TextComponent::from(gameprofile.name.clone()),
None,
1.into(),
TextComponent::text(&gameprofile.name.clone()),
None,
),
)
*/
/* server.broadcast_packet(
self,
&CDisguisedChatMessage::new(
@@ -272,12 +265,13 @@ impl Client {
pub fn handle_interact(&mut self, server: &mut Server, interact: SInteract) {
let action = ActionType::from_i32(interact.typ.0).unwrap();
if action == ActionType::Attack {
let attacker_player = self.player.as_ref().unwrap();
let entity_id = interact.entity_id;
// TODO: do validation and stuff
let config = &server.advanced_config.pvp;
if config.enabled {
let attacked_client = server.get_by_entityid(self, entity_id.0 as EntityId);
let attacker_player = self.player.as_mut().unwrap();
attacker_player.sneaking = interact.sneaking;
if let Some(mut client) = attacked_client {
let token = client.token.clone();
let player = client.player.as_mut().unwrap();
@@ -299,9 +293,10 @@ impl Client {
player.velocity.y as f32,
player.velocity.z as f32,
);
attacker_player.velocity = attacker_player.velocity.multiply(0.6, 1.0, 0.6);
player.velocity = velo;
client.send_packet(packet);
// attacker_player.velocity = attacker_player.velocity.multiply(0.6, 1.0, 0.6);
}
if config.hurt_animation {
// TODO
+10 -5
View File
@@ -18,7 +18,7 @@ impl<'a> Command<'a> for GamemodeCommand {
if args.len() != 2 {
player.send_system_message(
TextComponent::from("Usage: /gamemode <mode>")
TextComponent::text("Usage: /gamemode <mode>")
.color_named(pumpkin_text::color::NamedColor::Red),
);
return;
@@ -28,21 +28,26 @@ impl<'a> Command<'a> for GamemodeCommand {
match mode_str.parse() {
Ok(mode) => {
player.set_gamemode(mode);
player.send_system_message(format!("Set own game mode to {:?}", mode).into());
player.send_system_message(TextComponent::text(&format!(
"Set own game mode to {:?}",
mode
)));
}
Err(_) => {
// try to parse from number
if let Ok(i) = mode_str.parse::<u8>() {
if let Some(mode) = GameMode::from_u8(i) {
player.set_gamemode(mode);
player
.send_system_message(format!("Set own game mode to {:?}", mode).into());
player.send_system_message(TextComponent::text(&format!(
"Set own game mode to {:?}",
mode
)));
return;
}
}
player.send_system_message(
TextComponent::from("Invalid gamemode")
TextComponent::text("Invalid gamemode")
.color_named(pumpkin_text::color::NamedColor::Red),
);
}
+1 -1
View File
@@ -79,5 +79,5 @@ pub fn handle_command(sender: &mut CommandSender, command: &str) {
return;
}
// TODO: red color
sender.send_message("Command not Found".into());
sender.send_message(TextComponent::text("Command not Found"));
}
+1 -1
View File
@@ -15,6 +15,6 @@ impl<'a> Command<'a> for PumpkinCommand {
fn on_execute(sender: &mut super::CommandSender<'a>, _command: String) {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");
sender.send_message(TextComponent::from(format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")).color_named(NamedColor::Green))
sender.send_message(TextComponent::text(&format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")).color_named(NamedColor::Green))
}
}