Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/minecraft_packets/src/play/legacy_chat_message_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ pub struct LegacyChatMessagePacket {
}

impl LegacyChatMessagePacket {
pub fn component(component: &Component) -> Self {
pub fn system(component: &Component) -> Self {
Self {
content: component.to_json(),
position: 1,
sender: UuidAsString::default(),
}
}

pub fn game_info(component: &Component) -> Self {
Self {
content: component.to_legacy(),
position: 2,
sender: UuidAsString::default(),
}
}
}
128 changes: 128 additions & 0 deletions crates/minecraft_packets/src/play/legacy_set_title_packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use minecraft_protocol::prelude::*;
use pico_text_component::prelude::Component;

#[derive(PacketOut)]
pub struct LegacySetTitlePacket {
action: LegacySetTitleAction,
}

impl LegacySetTitlePacket {
pub fn action_bar(action_bar: &Component) -> Self {
Self {
action: LegacySetTitleAction::SetActionBar {
action_bar: action_bar.clone(),
},
}
}

pub fn set_title(title: &Component) -> Self {
Self {
action: LegacySetTitleAction::SetTitle {
title: title.clone(),
},
}
}

pub fn set_subtitle(subtitle: &Component) -> Self {
Self {
action: LegacySetTitleAction::SetSubtitle {
subtitle: subtitle.clone(),
},
}
}

pub fn set_animation(fade_in: i32, stay: i32, fade_out: i32) -> Self {
Self {
action: LegacySetTitleAction::SetTimesAndDisplay {
fade_in,
stay,
fade_out,
},
}
}
}

#[allow(dead_code)]
enum LegacySetTitleAction {
SetTitle {
title: Component,
},
SetSubtitle {
subtitle: Component,
},
SetActionBar {
action_bar: Component,
},
SetTimesAndDisplay {
fade_in: i32,
stay: i32,
fade_out: i32,
},
Hide,
Reset,
}

impl LegacySetTitleAction {
fn type_id(&self, protocol_version: ProtocolVersion) -> Result<i32, BinaryWriterError> {
if protocol_version.is_after_inclusive(ProtocolVersion::V1_11) {
let type_id = match self {
LegacySetTitleAction::SetTitle { .. } => 0,
LegacySetTitleAction::SetSubtitle { .. } => 1,
LegacySetTitleAction::SetActionBar { .. } => 2,
LegacySetTitleAction::SetTimesAndDisplay { .. } => 3,
LegacySetTitleAction::Hide => 4,
LegacySetTitleAction::Reset => 5,
};
Ok(type_id)
} else {
match self {
LegacySetTitleAction::SetTitle { .. } => Ok(0),
LegacySetTitleAction::SetSubtitle { .. } => Ok(1),
LegacySetTitleAction::SetTimesAndDisplay { .. } => Ok(2),
LegacySetTitleAction::Hide => Ok(3),
LegacySetTitleAction::Reset => Ok(4),
LegacySetTitleAction::SetActionBar { .. } => {
Err(BinaryWriterError::UnsupportedOperation)
}
}
}
}
}

impl EncodePacket for LegacySetTitleAction {
fn encode(
&self,
writer: &mut BinaryWriter,
protocol_version: ProtocolVersion,
) -> Result<(), BinaryWriterError> {
let type_id = VarInt::new(self.type_id(protocol_version)?);
type_id.encode(writer, protocol_version)?;
match self {
LegacySetTitleAction::SetTitle { title } => {
title.encode(writer, protocol_version)?;
}
LegacySetTitleAction::SetSubtitle { subtitle } => {
subtitle.encode(writer, protocol_version)?;
}
LegacySetTitleAction::SetActionBar { action_bar } => {
if protocol_version.is_before_inclusive(ProtocolVersion::V1_10) {
Err(BinaryWriterError::UnsupportedOperation)?;
}
action_bar.encode(writer, protocol_version)?;
}
LegacySetTitleAction::SetTimesAndDisplay {
fade_in,
stay,
fade_out,
} => {
fade_in.encode(writer, protocol_version)?;
stay.encode(writer, protocol_version)?;
fade_out.encode(writer, protocol_version)?;
}
LegacySetTitleAction::Hide | LegacySetTitleAction::Reset => {
// Nothing to encode
}
}
Ok(())
}
}
5 changes: 5 additions & 0 deletions crates/minecraft_packets/src/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ mod data;
pub mod disconnect_packet;
pub mod game_event_packet;
pub mod legacy_chat_message_packet;
pub mod legacy_set_title_packet;
pub mod login_packet;
pub mod play_client_bound_plugin_message_packet;
pub mod player_info_update_packet;
pub mod player_position_packet;
pub mod set_action_bar_text_packet;
pub mod set_chunk_cache_center_packet;
pub mod set_default_spawn_position_packet;
pub mod set_entity_data_packet;
pub mod set_player_position_and_rotation_packet;
pub mod set_player_position_packet;
pub mod set_subtitle_text_packet;
pub mod set_title_text_packet;
pub mod set_titles_animation;
pub mod synchronize_player_position_packet;
pub mod system_chat_message_packet;
pub mod tab_list_packet;
Expand Down
13 changes: 13 additions & 0 deletions crates/minecraft_packets/src/play/set_action_bar_text_packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use minecraft_protocol::prelude::*;
use pico_text_component::prelude::Component;

#[derive(PacketOut)]
pub struct SetActionBarTextPacket {
text: Component,
}

impl SetActionBarTextPacket {
pub fn new(text: &Component) -> Self {
Self { text: text.clone() }
}
}
15 changes: 15 additions & 0 deletions crates/minecraft_packets/src/play/set_subtitle_text_packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use minecraft_protocol::prelude::*;
use pico_text_component::prelude::Component;

#[derive(PacketOut)]
pub struct SetSubtitleTextPacket {
subtitle: Component,
}

impl SetSubtitleTextPacket {
pub fn new(subtitle: &Component) -> Self {
Self {
subtitle: subtitle.clone(),
}
}
}
13 changes: 13 additions & 0 deletions crates/minecraft_packets/src/play/set_title_text_packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use minecraft_protocol::prelude::*;
use pico_text_component::prelude::Component;

#[derive(PacketOut)]
pub struct SetTitleTextPacket {
text: Component,
}

impl SetTitleTextPacket {
pub fn new(text: &Component) -> Self {
Self { text: text.clone() }
}
}
18 changes: 18 additions & 0 deletions crates/minecraft_packets/src/play/set_titles_animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use minecraft_protocol::prelude::*;

#[derive(PacketOut)]
pub struct SetTitlesAnimationPacket {
fade_in: i32,
stay: i32,
fade_out: i32,
}

impl SetTitlesAnimationPacket {
pub fn new(fade_in: i32, stay: i32, fade_out: i32) -> Self {
Self {
fade_in,
stay,
fade_out,
}
}
}
2 changes: 2 additions & 0 deletions crates/pico_binutils/src/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum BinaryWriterError {
Io(#[from] std::io::Error),
#[error(transparent)]
TryFromInt(#[from] TryFromIntError),
#[error("This operation is unsupported")]
UnsupportedOperation,
}

impl BinaryWriter {
Expand Down
74 changes: 74 additions & 0 deletions crates/pico_text_component/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,80 @@ impl Component {

Nbt::compound("", compound)
}

pub fn to_legacy(&self) -> String {
#[derive(Serialize)]
struct TextComponent {
#[serde(default)]
text: String,
}
serde_json::to_string(&TextComponent {
text: self.to_legacy_impl(true),
})
.unwrap_or_default()
}

fn to_legacy_impl(&self, is_root: bool) -> String {
let mut s = String::new();

if !is_root {
s.push('§');
s.push('r');
}

if let Some(color) = &self.color {
let color_letter = match color.as_str() {
"black" => '0',
"dark_blue" => '1',
"dark_green" => '2',
"dark_aqua" => '3',
"dark_red" => '4',
"dark_purple" => '5',
"gold" => '6',
"gray" => '7',
"dark_gray" => '8',
"blue" => '9',
"green" => 'a',
"aqua" => 'b',
"red" => 'c',
"light_purple" => 'd',
"yellow" => 'e',
"white" => 'f',
_ => 'f',
};
s.push('§');
s.push(color_letter);
}

if self.bold {
s.push('§');
s.push('l');
}
if self.italic {
s.push('§');
s.push('o');
}
if self.underlined {
s.push('§');
s.push('n');
}
if self.strikethrough {
s.push('§');
s.push('m');
}
if self.obfuscated {
s.push('§');
s.push('k');
}

s.push_str(&self.text);

for extra in &self.extra {
s.push_str(&extra.to_legacy_impl(false));
}

s
}
}

impl EncodePacket for Component {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_10/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 69
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_11/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 69
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_12/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 71
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_12_1/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 72
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_13/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 75
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_14/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
},
"minecraft:boss_event": {
"protocol_id": 12
},
"minecraft:legacy_set_title": {
"protocol_id": 79
}
},
"serverbound": {
Expand Down
3 changes: 3 additions & 0 deletions data/generated/V1_15/reports/packets.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
},
"minecraft:boss_event": {
"protocol_id": 13
},
"minecraft:legacy_set_title": {
"protocol_id": 80
}
},
"serverbound": {
Expand Down
Loading