Skip to content

Commit f91f592

Browse files
committed
Move plugin message handling to plugin_messages::PluginMessageHandler
1 parent e9c2a8f commit f91f592

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

src/server/mod.rs

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub struct Server {
5656
resources: Arc<RwLock<resources::Manager>>,
5757
version: usize,
5858

59+
plugin_message_handler: plugin_messages::PluginMessageHandler,
60+
5961
// Entity accessors
6062
game_info: ecs::Key<entity::GameInfo>,
6163
player_movement: ecs::Key<entity::player::PlayerMovement>,
@@ -292,6 +294,8 @@ impl Server {
292294
version,
293295
resources,
294296

297+
plugin_message_handler: plugin_messages::PluginMessageHandler {},
298+
295299
// Entity accessors
296300
game_info,
297301
player_movement: entities.get_key(),
@@ -670,46 +674,11 @@ impl Server {
670674
}
671675

672676
fn on_plugin_message_clientbound_i16(&mut self, msg: packet::play::clientbound::PluginMessageClientbound_i16) {
673-
self.on_plugin_message_clientbound(&msg.channel, msg.data.data.as_slice())
677+
self.plugin_message_handler.on_plugin_message_clientbound(&msg.channel, msg.data.data.as_slice())
674678
}
675679

676680
fn on_plugin_message_clientbound_1(&mut self, msg: packet::play::clientbound::PluginMessageClientbound) {
677-
self.on_plugin_message_clientbound(&msg.channel, &msg.data)
678-
}
679-
680-
fn on_plugin_message_clientbound(&mut self, channel: &str, data: &[u8]) {
681-
println!("Received plugin message: channel={}, data={:?}", channel, data);
682-
683-
match channel {
684-
// TODO: "REGISTER" =>
685-
// TODO: "UNREGISTER" =>
686-
"FML|HS" => {
687-
// https://wiki.vg/Minecraft_Forge_Handshake
688-
let discriminator = data[0];
689-
690-
match discriminator {
691-
0 => {
692-
// ServerHello
693-
let fml_protocol_version = data[1];
694-
let dimension = if fml_protocol_version > 1 {
695-
use byteorder::{BigEndian, ReadBytesExt};
696-
let dimension = (&data[2..2 + 4]).read_u32::<BigEndian>().unwrap();
697-
Some(dimension)
698-
} else {
699-
None
700-
};
701-
702-
println!("FML|HS ServerHello: fml_protocol_version={}, dimension={:?}", fml_protocol_version, dimension);
703-
704-
// TODO: send reply
705-
},
706-
_ => {
707-
println!("Unhandled FML|HS packet: discriminator={}", discriminator);
708-
}
709-
}
710-
}
711-
_ => ()
712-
}
681+
self.plugin_message_handler.on_plugin_message_clientbound(&msg.channel, &msg.data)
713682
}
714683

715684
fn on_game_join_i32_viewdistance(&mut self, join: packet::play::clientbound::JoinGame_i32_ViewDistance) {

src/server/plugin_messages.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,47 @@ use crate::protocol::Serializable;
33
use crate::protocol::packet::play::serverbound::PluginMessageServerbound;
44
use crate::protocol::packet::play::serverbound::PluginMessageServerbound_i16;
55

6+
pub struct PluginMessageHandler {
7+
}
8+
9+
impl PluginMessageHandler {
10+
pub fn on_plugin_message_clientbound(&mut self, channel: &str, data: &[u8]) {
11+
println!("Received plugin message: channel={}, data={:?}", channel, data);
12+
13+
match channel {
14+
// TODO: "REGISTER" =>
15+
// TODO: "UNREGISTER" =>
16+
"FML|HS" => {
17+
// https://wiki.vg/Minecraft_Forge_Handshake
18+
let discriminator = data[0];
19+
20+
match discriminator {
21+
0 => {
22+
// ServerHello
23+
let fml_protocol_version = data[1];
24+
let dimension = if fml_protocol_version > 1 {
25+
use byteorder::{BigEndian, ReadBytesExt};
26+
let dimension = (&data[2..2 + 4]).read_u32::<BigEndian>().unwrap();
27+
Some(dimension)
28+
} else {
29+
None
30+
};
31+
32+
println!("FML|HS ServerHello: fml_protocol_version={}, dimension={:?}", fml_protocol_version, dimension);
33+
34+
// TODO: send reply
35+
},
36+
_ => {
37+
println!("Unhandled FML|HS packet: discriminator={}", discriminator);
38+
}
39+
}
40+
}
41+
_ => ()
42+
}
43+
}
44+
}
45+
46+
647
pub struct Brand {
748
pub brand: String,
849
}

0 commit comments

Comments
 (0)