Skip to content

Commit

Permalink
Remove lifetime from TextComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Dec 30, 2024
1 parent 6f7433e commit f772d97
Show file tree
Hide file tree
Showing 66 changed files with 228 additions and 244 deletions.
12 changes: 6 additions & 6 deletions pumpkin-core/src/text/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use serde::{Deserialize, Serialize};
/// Action to take on click of the text.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, Eq, Hash)]
#[serde(tag = "action", content = "value", rename_all = "snake_case")]
pub enum ClickEvent<'a> {
pub enum ClickEvent {
/// Opens a URL
OpenUrl(Cow<'a, str>),
OpenUrl(Cow<'static, str>),
/// Opens a File
OpenFile(Cow<'a, str>),
OpenFile(Cow<'static, str>),
/// Works in signs, but only on the root text component
RunCommand(Cow<'a, str>),
RunCommand(Cow<'static, str>),
/// Replaces the contents of the chat box with the text, not necessarily a
/// command.
SuggestCommand(Cow<'a, str>),
SuggestCommand(Cow<'static, str>),
/// Only usable within written books. Changes the page of the book. Indexing
/// starts at 1.
ChangePage(i32),
/// Copies the given text to system clipboard
CopyToClipboard(Cow<'a, str>),
CopyToClipboard(Cow<'static, str>),
}
12 changes: 6 additions & 6 deletions pumpkin-core/src/text/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use super::TextComponent;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(tag = "action", content = "contents", rename_all = "snake_case")]
pub enum HoverEvent<'a> {
pub enum HoverEvent {
/// Displays a tooltip with the given text.
ShowText(Cow<'a, str>),
ShowText(Cow<'static, str>),
/// Shows an item.
ShowItem {
/// Resource identifier of the item
id: Cow<'a, str>,
id: Cow<'static, str>,
/// Number of the items in the stack
count: Option<i32>,
/// NBT information about the item (sNBT format)
tag: Cow<'a, str>,
tag: Cow<'static, str>,
},
/// Shows an entity.
ShowEntity {
Expand All @@ -25,9 +25,9 @@ pub enum HoverEvent<'a> {
/// Resource identifier of the entity
#[serde(rename = "type")]
#[serde(default, skip_serializing_if = "Option::is_none")]
kind: Option<Cow<'a, str>>,
kind: Option<Cow<'static, str>>,
/// Optional custom name for the entity
#[serde(default, skip_serializing_if = "Option::is_none")]
name: Option<Box<TextComponent<'a>>>,
name: Option<Box<TextComponent>>,
},
}
55 changes: 25 additions & 30 deletions pumpkin-core/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,32 @@ pub mod style;
/// Represents a Text component
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct TextComponent<'a> {
pub struct TextComponent {
/// The actual text
#[serde(flatten)]
pub content: TextContent<'a>,
pub content: TextContent,
/// Style of the text. Bold, Italic, underline, Color...
/// Also has `ClickEvent
#[serde(flatten)]
pub style: Style<'a>,
pub style: Style,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
/// Extra text components
pub extra: Vec<TextComponent<'a>>,
pub extra: Vec<TextComponent>,
}

impl<'a> TextComponent<'a> {
pub fn text(text: &'a str) -> Self {
Self {
content: TextContent::Text { text: text.into() },
style: Style::default(),
extra: vec![],
}
}

pub fn text_string(text: String) -> Self {
impl TextComponent {
pub fn text<P>(plain: P) -> Self
where
P: Into<Cow<'static, str>>,
{
Self {
content: TextContent::Text { text: text.into() },
content: TextContent::Text { text: plain.into() },
style: Style::default(),
extra: vec![],
}
}

pub fn add_child(mut self, child: TextComponent<'a>) -> Self {
pub fn add_child(mut self, child: TextComponent) -> Self {
self.extra.push(child);
self
}
Expand Down Expand Up @@ -92,7 +87,7 @@ impl<'a> TextComponent<'a> {
}
}

impl serde::Serialize for TextComponent<'_> {
impl serde::Serialize for TextComponent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -101,7 +96,7 @@ impl serde::Serialize for TextComponent<'_> {
}
}

impl<'a> TextComponent<'a> {
impl TextComponent {
pub fn color(mut self, color: Color) -> Self {
self.style.color = Some(color);
self
Expand Down Expand Up @@ -154,13 +149,13 @@ impl<'a> TextComponent<'a> {
}

/// Allows for events to occur when the player clicks on text. Only work in chat.
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
pub fn click_event(mut self, event: ClickEvent) -> Self {
self.style.click_event = Some(event);
self
}

/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
pub fn hover_event(mut self, event: HoverEvent) -> Self {
self.style.hover_event = Some(event);
self
}
Expand All @@ -184,14 +179,14 @@ impl<'a> TextComponent<'a> {
#[serde(rename_all = "camelCase")]
struct TempStruct<'a> {
#[serde(flatten)]
text: &'a TextContent<'a>,
text: &'a TextContent,
#[serde(flatten)]
style: &'a Style<'a>,
style: &'a Style,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(rename = "extra")]
extra: Vec<TempStruct<'a>>,
}
fn convert_extra<'a>(extra: &'a [TextComponent<'a>]) -> Vec<TempStruct<'a>> {
fn convert_extra(extra: &[TextComponent]) -> Vec<TempStruct<'_>> {
extra
.iter()
.map(|x| TempStruct {
Expand All @@ -218,22 +213,22 @@ impl<'a> TextComponent<'a> {

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum TextContent<'a> {
pub enum TextContent {
/// Raw Text
Text { text: Cow<'a, str> },
Text { text: Cow<'static, str> },
/// Translated text
Translate {
translate: Cow<'a, str>,
translate: Cow<'static, str>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
with: Vec<TextComponent<'a>>,
with: Vec<TextComponent>,
},
/// Displays the name of one or more entities found by a selector.
EntityNames {
selector: Cow<'a, str>,
selector: Cow<'static, str>,
#[serde(default, skip_serializing_if = "Option::is_none")]
separator: Option<Cow<'a, str>>,
separator: Option<Cow<'static, str>>,
},
/// A keybind identifier
/// https://minecraft.wiki/w/Controls#Configurable_controls
Keybind { keybind: Cow<'a, str> },
Keybind { keybind: Cow<'static, str> },
}
12 changes: 6 additions & 6 deletions pumpkin-core/src/text/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct Style<'a> {
pub struct Style {
/// Changes the color to render the content
#[serde(default, skip_serializing_if = "Option::is_none")]
pub color: Option<Color>,
Expand All @@ -31,10 +31,10 @@ pub struct Style<'a> {
pub insertion: Option<String>,
/// Allows for events to occur when the player clicks on text. Only work in chat.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub click_event: Option<ClickEvent<'a>>,
pub click_event: Option<ClickEvent>,
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hover_event: Option<HoverEvent<'a>>,
pub hover_event: Option<HoverEvent>,
/// Allows you to change the font of the text.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub font: Option<String>,
Expand All @@ -46,7 +46,7 @@ pub struct Style<'a> {
pub shadow_color: Option<ARGBColor>,
}

impl<'a> Style<'a> {
impl Style {
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
Expand Down Expand Up @@ -94,13 +94,13 @@ impl<'a> Style<'a> {
}

/// Allows for events to occur when the player clicks on text. Only work in chat.
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
pub fn click_event(mut self, event: ClickEvent) -> Self {
self.click_event = Some(event);
self
}

/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
pub fn hover_event(mut self, event: HoverEvent) -> Self {
self.hover_event = Some(event);
self
}
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub trait ByteBufMut {

fn put_string_len(&mut self, val: &str, max_size: usize);

fn put_string_array(&mut self, array: &[String]);
fn put_string_array(&mut self, array: &[&str]);

fn put_bit_set(&mut self, set: &BitSet);

Expand Down Expand Up @@ -346,7 +346,7 @@ impl<T: BufMut> ByteBufMut for T {
self.put(val.as_bytes());
}

fn put_string_array(&mut self, array: &[String]) {
fn put_string_array(&mut self, array: &[&str]) {
for string in array {
self.put_string(string)
}
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/config/c_add_resource_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct CConfigAddResourcePack<'a> {
url: &'a str,
hash: &'a str, // max 40
forced: bool,
prompt_message: Option<TextComponent<'a>>,
prompt_message: Option<TextComponent>,
}

impl<'a> CConfigAddResourcePack<'a> {
Expand All @@ -19,7 +19,7 @@ impl<'a> CConfigAddResourcePack<'a> {
url: &'a str,
hash: &'a str,
forced: bool,
prompt_message: Option<TextComponent<'a>>,
prompt_message: Option<TextComponent>,
) -> Self {
Self {
uuid,
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/bossevent_action.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::VarInt;
use pumpkin_core::text::TextComponent;

pub enum BosseventAction<'a> {
pub enum BosseventAction {
Add {
title: TextComponent<'a>,
title: TextComponent,
health: f32,
color: VarInt,
division: VarInt,
flags: u8,
},
Remove,
UpdateHealth(f32),
UpdateTile(TextComponent<'a>),
UpdateTile(TextComponent),
UpdateStyle {
color: VarInt,
dividers: VarInt,
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/c_actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use serde::Serialize;
#[derive(Serialize)]
#[client_packet("play:set_action_bar_text")]
pub struct CActionBar<'a> {
action_bar: TextComponent<'a>,
action_bar: &'a TextComponent,
}

impl<'a> CActionBar<'a> {
pub fn new(action_bar: TextComponent<'a>) -> Self {
pub fn new(action_bar: &'a TextComponent) -> Self {
Self { action_bar }
}
}
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_boss_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use pumpkin_macros::client_packet;

#[client_packet("play:boss_event")]
pub struct CBossEvent<'a> {
pub uuid: uuid::Uuid,
pub action: BosseventAction<'a>,
pub uuid: &'a uuid::Uuid,
pub action: BosseventAction,
}

impl<'a> CBossEvent<'a> {
pub fn new(uuid: uuid::Uuid, action: BosseventAction<'a>) -> Self {
pub fn new(uuid: &'a uuid::Uuid, action: BosseventAction) -> Self {
Self { uuid, action }
}
}

impl ClientPacket for CBossEvent<'_> {
fn write(&self, bytebuf: &mut impl BufMut) {
bytebuf.put_uuid(&self.uuid);
bytebuf.put_uuid(self.uuid);
let action = &self.action;
match action {
BosseventAction::Add {
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/c_combat_death.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use crate::VarInt;
#[client_packet("play:player_combat_kill")]
pub struct CCombatDeath<'a> {
player_id: VarInt,
message: TextComponent<'a>,
message: &'a TextComponent,
}

impl<'a> CCombatDeath<'a> {
pub fn new(player_id: VarInt, message: TextComponent<'a>) -> Self {
pub fn new(player_id: VarInt, message: &'a TextComponent) -> Self {
Self { player_id, message }
}
}
23 changes: 9 additions & 14 deletions pumpkin-protocol/src/client/play/c_command_suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ use pumpkin_macros::client_packet;
use crate::{bytebuf::ByteBufMut, ClientPacket, VarInt};

#[client_packet("play:command_suggestions")]
pub struct CCommandSuggestions<'a> {
pub struct CCommandSuggestions {
id: VarInt,
start: VarInt,
length: VarInt,
matches: Vec<CommandSuggestion<'a>>,
matches: Vec<CommandSuggestion>,
}

impl<'a> CCommandSuggestions<'a> {
pub fn new(
id: VarInt,
start: VarInt,
length: VarInt,
matches: Vec<CommandSuggestion<'a>>,
) -> Self {
impl CCommandSuggestions {
pub fn new(id: VarInt, start: VarInt, length: VarInt, matches: Vec<CommandSuggestion>) -> Self {
Self {
id,
start,
Expand All @@ -28,7 +23,7 @@ impl<'a> CCommandSuggestions<'a> {
}
}

impl ClientPacket for CCommandSuggestions<'_> {
impl ClientPacket for CCommandSuggestions {
fn write(&self, bytebuf: &mut impl BufMut) {
bytebuf.put_var_int(&self.id);
bytebuf.put_var_int(&self.start);
Expand All @@ -45,13 +40,13 @@ impl ClientPacket for CCommandSuggestions<'_> {
}

#[derive(PartialEq, Eq, Hash, Debug)]
pub struct CommandSuggestion<'a> {
pub struct CommandSuggestion {
pub suggestion: String,
pub tooltip: Option<TextComponent<'a>>,
pub tooltip: Option<TextComponent>,
}

impl<'a> CommandSuggestion<'a> {
pub fn new(suggestion: String, tooltip: Option<TextComponent<'a>>) -> Self {
impl CommandSuggestion {
pub fn new(suggestion: String, tooltip: Option<TextComponent>) -> Self {
Self {
suggestion,
tooltip,
Expand Down
Loading

0 comments on commit f772d97

Please sign in to comment.