@@ -971,7 +971,7 @@ pub enum Direction {
971971
972972/// The protocol has multiple 'sub-protocols' or states which control which
973973/// packet an id points to.
974- #[ derive( Clone , Copy , Debug ) ]
974+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
975975pub enum State {
976976 Handshaking ,
977977 Play ,
@@ -1036,7 +1036,7 @@ pub struct Conn {
10361036
10371037 cipher : Option < Aes128Cfb > ,
10381038
1039- compression_threshold : i32 ,
1039+ pub compression_threshold : i32 ,
10401040}
10411041
10421042impl Conn {
@@ -1099,17 +1099,91 @@ impl Conn {
10991099 }
11001100 self . write_all ( & buf) ?;
11011101
1102- Result :: Ok ( ( ) )
1102+ Ok ( ( ) )
11031103 }
11041104
1105- pub fn read_packet ( & mut self ) -> Result < packet:: Packet , Error > {
1106- let len = VarInt :: read_from ( self ) ?. 0 as usize ;
1105+ pub fn write_plugin_message ( & mut self , channel : & str , data : & [ u8 ] ) -> Result < ( ) , Error > {
1106+ if is_network_debug ( ) {
1107+ debug ! (
1108+ "Sending plugin message: channel={}, data={:?}" ,
1109+ channel, data
1110+ ) ;
1111+ }
1112+ debug_assert ! ( self . state == State :: Play ) ;
1113+ if self . protocol_version >= 47 {
1114+ self . write_packet ( packet:: play:: serverbound:: PluginMessageServerbound {
1115+ channel : channel. to_string ( ) ,
1116+ data : data. to_vec ( ) ,
1117+ } ) ?;
1118+ } else {
1119+ self . write_packet ( packet:: play:: serverbound:: PluginMessageServerbound_i16 {
1120+ channel : channel. to_string ( ) ,
1121+ data : LenPrefixedBytes :: < VarShort > :: new ( data. to_vec ( ) ) ,
1122+ } ) ?;
1123+ }
1124+
1125+ Ok ( ( ) )
1126+ }
1127+
1128+ pub fn write_fmlhs_plugin_message ( & mut self , msg : & forge:: FmlHs ) -> Result < ( ) , Error > {
1129+ let mut buf: Vec < u8 > = vec ! [ ] ;
1130+ msg. write_to ( & mut buf) ?;
1131+
1132+ self . write_plugin_message ( "FML|HS" , & buf)
1133+ }
1134+
1135+ pub fn write_login_plugin_response (
1136+ & mut self ,
1137+ message_id : VarInt ,
1138+ successful : bool ,
1139+ data : & [ u8 ] ,
1140+ ) -> Result < ( ) , Error > {
1141+ if is_network_debug ( ) {
1142+ debug ! (
1143+ "Sending login plugin message: message_id={:?}, successful={:?}, data={:?}" ,
1144+ message_id, successful, data,
1145+ ) ;
1146+ }
1147+ debug_assert ! ( self . state == State :: Login ) ;
1148+ self . write_packet ( packet:: login:: serverbound:: LoginPluginResponse {
1149+ message_id,
1150+ successful,
1151+ data : data. to_vec ( ) ,
1152+ } )
1153+ }
1154+
1155+ pub fn write_fml2_handshake_plugin_message (
1156+ & mut self ,
1157+ message_id : VarInt ,
1158+ msg : Option < & forge:: fml2:: FmlHandshake > ,
1159+ ) -> Result < ( ) , Error > {
1160+ if let Some ( msg) = msg {
1161+ let mut inner_buf: Vec < u8 > = vec ! [ ] ;
1162+ msg. write_to ( & mut inner_buf) ?;
1163+
1164+ let mut outer_buf: Vec < u8 > = vec ! [ ] ;
1165+ "fml:handshake" . to_string ( ) . write_to ( & mut outer_buf) ?;
1166+ VarInt ( inner_buf. len ( ) as i32 ) . write_to ( & mut outer_buf) ?;
1167+ inner_buf. write_to ( & mut outer_buf) ?;
1168+
1169+ self . write_login_plugin_response ( message_id, true , & outer_buf)
1170+ } else {
1171+ unimplemented ! ( ) // successful: false, no payload
1172+ }
1173+ }
1174+
1175+ #[ allow( clippy:: type_complexity) ]
1176+ pub fn read_raw_packet_from < R : io:: Read > (
1177+ buf : & mut R ,
1178+ compression_threshold : i32 ,
1179+ ) -> Result < ( i32 , Box < io:: Cursor < Vec < u8 > > > ) , Error > {
1180+ let len = VarInt :: read_from ( buf) ?. 0 as usize ;
11071181 let mut ibuf = vec ! [ 0 ; len] ;
1108- self . read_exact ( & mut ibuf) ?;
1182+ buf . read_exact ( & mut ibuf) ?;
11091183
11101184 let mut buf = io:: Cursor :: new ( ibuf) ;
11111185
1112- if self . compression_threshold >= 0 {
1186+ if compression_threshold >= 0 {
11131187 let uncompressed_size = VarInt :: read_from ( & mut buf) ?. 0 ;
11141188 if uncompressed_size != 0 {
11151189 let mut new = Vec :: with_capacity ( uncompressed_size as usize ) ;
@@ -1120,7 +1194,7 @@ impl Conn {
11201194 if is_network_debug ( ) {
11211195 debug ! (
11221196 "Decompressed threshold={} len={} uncompressed_size={} to {} bytes" ,
1123- self . compression_threshold,
1197+ compression_threshold,
11241198 len,
11251199 uncompressed_size,
11261200 new. len( )
@@ -1131,6 +1205,13 @@ impl Conn {
11311205 }
11321206 let id = VarInt :: read_from ( & mut buf) ?. 0 ;
11331207
1208+ Ok ( ( id, Box :: new ( buf) ) )
1209+ }
1210+
1211+ pub fn read_packet ( & mut self ) -> Result < packet:: Packet , Error > {
1212+ let compression_threshold = self . compression_threshold ;
1213+ let ( id, mut buf) = Conn :: read_raw_packet_from ( self , compression_threshold) ?;
1214+
11341215 let dir = match self . direction {
11351216 Direction :: Clientbound => Direction :: Serverbound ,
11361217 Direction :: Serverbound => Direction :: Clientbound ,
@@ -1224,6 +1305,7 @@ impl Conn {
12241305
12251306 // For modded servers, get the list of Forge mods installed
12261307 let mut forge_mods: std:: vec:: Vec < crate :: protocol:: forge:: ForgeMod > = vec ! [ ] ;
1308+ let mut fml_network_version: Option < i64 > = None ;
12271309 if let Some ( modinfo) = val. get ( "modinfo" ) {
12281310 if let Some ( modinfo_type) = modinfo. get ( "type" ) {
12291311 if modinfo_type == "FML" {
@@ -1240,6 +1322,7 @@ impl Conn {
12401322 . push ( crate :: protocol:: forge:: ForgeMod { modid, version } ) ;
12411323 }
12421324 }
1325+ fml_network_version = Some ( 1 ) ;
12431326 }
12441327 }
12451328 } else {
@@ -1267,6 +1350,13 @@ impl Conn {
12671350 }
12681351 }
12691352 }
1353+ fml_network_version = Some (
1354+ forge_data
1355+ . get ( "fmlNetworkVersion" )
1356+ . unwrap ( )
1357+ . as_i64 ( )
1358+ . unwrap ( ) ,
1359+ ) ;
12701360 }
12711361
12721362 Ok ( (
@@ -1301,6 +1391,7 @@ impl Conn {
13011391 . and_then ( Value :: as_str)
13021392 . map ( |v| v. to_owned ( ) ) ,
13031393 forge_mods,
1394+ fml_network_version,
13041395 } ,
13051396 ping,
13061397 ) )
@@ -1352,6 +1443,7 @@ pub struct Status {
13521443 pub description : format:: Component ,
13531444 pub favicon : Option < String > ,
13541445 pub forge_mods : Vec < crate :: protocol:: forge:: ForgeMod > ,
1446+ pub fml_network_version : Option < i64 > ,
13551447}
13561448
13571449#[ derive( Debug ) ]
0 commit comments