forked from Quozul/PicoLimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacy_set_title_packet.rs
More file actions
128 lines (120 loc) · 3.86 KB
/
legacy_set_title_packet.rs
File metadata and controls
128 lines (120 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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(())
}
}