Skip to content

Commit

Permalink
Text API redesign (#417)
Browse files Browse the repository at this point in the history
# Objective

- Close #411 

# Solution

- Redesign `Color` to make `reset` representable, separate `NormalColor`
and `RgbColor`.
- Make contents of `Text`, `InnerText`, `TextContent`, etc public.
- Redesign `TextFormat` trait to allow mutating a `&mut Text` without
cloning.
- Create `IntoText` trait which is basically the same as `Into<Cow<'a,
Text>>` (can't implement the latter because of Rust's trait coherence
rules)
- Fallback to normal colors instead of webcolors for pre-1.16 server
list pings.

I couldn't figure out a good way to meaningfully reduce the size of
`TextInner` so that it wouldn't need to be boxed in `Text`,
  • Loading branch information
PonasKovas authored Jul 22, 2023
1 parent 11eecb8 commit 9c599dd
Show file tree
Hide file tree
Showing 15 changed files with 966 additions and 547 deletions.
2 changes: 1 addition & 1 deletion benches/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use valence::protocol::byte_angle::ByteAngle;
use valence::protocol::decode::PacketDecoder;
use valence::protocol::encode::PacketEncoder;
use valence::protocol::var_int::VarInt;
use valence::text::TextFormat;
use valence::text::IntoText;
use valence_core::protocol::encode::{PacketWriter, WritePacket};
use valence_entity::packet::EntitySpawnS2c;
use valence_instance::packet::ChunkDataS2c;
Expand Down
6 changes: 3 additions & 3 deletions crates/valence_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use valence_core::protocol::global_pos::GlobalPos;
use valence_core::protocol::packet::sound::{PlaySoundS2c, Sound, SoundCategory};
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{Encode, Packet};
use valence_core::text::Text;
use valence_core::text::{IntoText, Text};
use valence_core::uuid::UniqueId;
use valence_entity::packet::{
EntitiesDestroyS2c, EntitySetHeadYawS2c, EntitySpawnS2c, EntityStatusS2c,
Expand Down Expand Up @@ -331,10 +331,10 @@ impl Client {

/// Kills the client and shows `message` on the death screen. If an entity
/// killed the player, you should supply it as `killer`.
pub fn kill(&mut self, message: impl Into<Text>) {
pub fn kill<'a>(&mut self, message: impl IntoText<'a>) {
self.write_packet(&DeathMessageS2c {
player_id: VarInt(0),
message: message.into().into(),
message: message.into_cow_text(),
});
}

Expand Down
14 changes: 7 additions & 7 deletions crates/valence_client/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_core::protocol::encode::WritePacket;
use valence_core::protocol::packet::chat::{ChatMessageC2s, GameMessageS2c};
use valence_core::text::Text;
use valence_core::text::IntoText;

use crate::event_loop::{EventLoopPreUpdate, PacketEvent};

Expand All @@ -15,22 +15,22 @@ pub(super) fn build(app: &mut App) {

pub trait SendMessage {
/// Sends a system message visible in the chat.
fn send_chat_message(&mut self, msg: impl Into<Text>);
fn send_chat_message<'a>(&mut self, msg: impl IntoText<'a>);
/// Displays a message in the player's action bar (text above the hotbar).
fn send_action_bar_message(&mut self, msg: impl Into<Text>);
fn send_action_bar_message<'a>(&mut self, msg: impl IntoText<'a>);
}

impl<T: WritePacket> SendMessage for T {
fn send_chat_message(&mut self, msg: impl Into<Text>) {
fn send_chat_message<'a>(&mut self, msg: impl IntoText<'a>) {
self.write_packet(&GameMessageS2c {
chat: msg.into().into(),
chat: msg.into_cow_text(),
overlay: false,
});
}

fn send_action_bar_message(&mut self, msg: impl Into<Text>) {
fn send_action_bar_message<'a>(&mut self, msg: impl IntoText<'a>) {
self.write_packet(&GameMessageS2c {
chat: msg.into().into(),
chat: msg.into_cow_text(),
overlay: true,
});
}
Expand Down
19 changes: 10 additions & 9 deletions crates/valence_client/src/title.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use valence_core::protocol::{packet_id, Decode, Encode};
use valence_core::text::IntoText;

use super::*;

Expand All @@ -9,11 +10,11 @@ pub trait SetTitle {
/// which may also include a subtitle underneath it. The title can be
/// configured to fade in and out using
/// [`set_title_times`](Self::set_title_times).
fn set_title(&mut self, text: impl Into<Text>);
fn set_title<'a>(&mut self, text: impl IntoText<'a>);

fn set_subtitle(&mut self, text: impl Into<Text>);
fn set_subtitle<'a>(&mut self, text: impl IntoText<'a>);

fn set_action_bar(&mut self, text: impl Into<Text>);
fn set_action_bar<'a>(&mut self, text: impl IntoText<'a>);

/// - `fade_in`: Ticks to spend fading in.
/// - `stay`: Ticks to keep the title displayed.
Expand All @@ -26,21 +27,21 @@ pub trait SetTitle {
}

impl<T: WritePacket> SetTitle for T {
fn set_title(&mut self, text: impl Into<Text>) {
fn set_title<'a>(&mut self, text: impl IntoText<'a>) {
self.write_packet(&TitleS2c {
title_text: text.into().into(),
title_text: text.into_cow_text(),
});
}

fn set_subtitle(&mut self, text: impl Into<Text>) {
fn set_subtitle<'a>(&mut self, text: impl IntoText<'a>) {
self.write_packet(&SubtitleS2c {
subtitle_text: text.into().into(),
subtitle_text: text.into_cow_text(),
});
}

fn set_action_bar(&mut self, text: impl Into<Text>) {
fn set_action_bar<'a>(&mut self, text: impl IntoText<'a>) {
self.write_packet(&OverlayMessageS2c {
action_bar_text: text.into().into(),
action_bar_text: text.into_cow_text(),
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/valence_core/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ mod tests {
use crate::item::{ItemKind, ItemStack};
use crate::protocol::var_int::VarInt;
use crate::protocol::var_long::VarLong;
use crate::text::{Text, TextFormat};
use crate::text::{IntoText, Text};

#[cfg(feature = "encryption")]
const CRYPT_KEY: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
Expand Down
Loading

0 comments on commit 9c599dd

Please sign in to comment.