protocol: add SPluginMessage

This commit is contained in:
Snowiiii 2024-08-02 12:31:44 +02:00
parent a548c3ee40
commit e9b475639a
6 changed files with 45 additions and 7 deletions

View File

@ -280,4 +280,8 @@ impl ByteBuffer {
pub fn put_slice(&mut self, src: &[u8]) { pub fn put_slice(&mut self, src: &[u8]) {
self.buffer.put_slice(src) self.buffer.put_slice(src)
} }
pub fn get_slice(&mut self) -> BytesMut {
self.buffer.split()
}
} }

View File

@ -14,6 +14,8 @@ pub mod packet_encoder;
pub const MAX_PACKET_SIZE: i32 = 2097152; pub const MAX_PACKET_SIZE: i32 = 2097152;
/// usally uses a namespace like "minecraft:thing"
pub type Identifier = String;
pub type VarInt = i32; pub type VarInt = i32;
pub type VarLong = i64; pub type VarLong = i64;

View File

@ -3,10 +3,7 @@ use std::io::Write;
use aes::cipher::{generic_array::GenericArray, BlockEncryptMut, BlockSizeUser, KeyIvInit}; use aes::cipher::{generic_array::GenericArray, BlockEncryptMut, BlockSizeUser, KeyIvInit};
use bytes::{BufMut, BytesMut}; use bytes::{BufMut, BytesMut};
use crate::{ use crate::{bytebuf::ByteBuffer, ClientPacket, PacketError, VarInt32, MAX_PACKET_SIZE};
bytebuf::ByteBuffer, ClientPacket, PacketError, VarInt32,
MAX_PACKET_SIZE,
};
type Cipher = cfb8::Encryptor<aes::Aes128>; type Cipher = cfb8::Encryptor<aes::Aes128>;

View File

@ -1,4 +1,4 @@
use crate::{bytebuf::ByteBuffer, ServerPacket, VarInt}; use crate::{bytebuf::ByteBuffer, Identifier, ServerPacket, VarInt};
pub struct SClientInformation { pub struct SClientInformation {
pub locale: String, // 16 pub locale: String, // 16
@ -28,6 +28,22 @@ impl ServerPacket for SClientInformation {
} }
} }
pub struct SPluginMessage {
pub channel: Identifier,
pub data: Vec<u8>,
}
impl ServerPacket for SPluginMessage {
const PACKET_ID: VarInt = 0x02;
fn read(bytebuf: &mut ByteBuffer) -> Self {
Self {
channel: bytebuf.get_string().unwrap(),
data: bytebuf.get_slice().to_vec(),
}
}
}
pub struct SAcknowledgeFinishConfig {} pub struct SAcknowledgeFinishConfig {}
impl ServerPacket for SAcknowledgeFinishConfig { impl ServerPacket for SAcknowledgeFinishConfig {

View File

@ -1,3 +1,4 @@
use pumpkin_protocol::{ use pumpkin_protocol::{
client::{ client::{
config::{CFinishConfig, CKnownPacks, CRegistryData, Entry}, config::{CFinishConfig, CKnownPacks, CRegistryData, Entry},
@ -5,7 +6,7 @@ use pumpkin_protocol::{
status::{CPingResponse, CStatusResponse}, status::{CPingResponse, CStatusResponse},
}, },
server::{ server::{
config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks}, config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks, SPluginMessage},
handshake::SHandShake, handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
status::{SPingRequest, SStatusRequest}, status::{SPingRequest, SStatusRequest},
@ -52,6 +53,7 @@ pub trait ClientPacketProcessor {
server: &mut Server, server: &mut Server,
client_information: SClientInformation, client_information: SClientInformation,
); );
fn handle_plugin_message(&mut self, server: &mut Server, plugin_message: SPluginMessage);
fn handle_known_packs(&mut self, server: &mut Server, config_acknowledged: SKnownPacks); fn handle_known_packs(&mut self, server: &mut Server, config_acknowledged: SKnownPacks);
fn handle_config_acknowledged( fn handle_config_acknowledged(
&mut self, &mut self,
@ -161,6 +163,18 @@ impl ClientPacketProcessor for Client {
}); });
} }
fn handle_plugin_message(&mut self, _server: &mut Server, plugin_message: SPluginMessage) {
if plugin_message.channel.starts_with("minecraft:brand")
|| plugin_message.channel.starts_with("MC|Brand")
{
dbg!("got a client brand");
match String::from_utf8(plugin_message.data) {
Ok(brand) => self.brand = Some(brand),
Err(e) => self.kick(&e.to_string()),
}
}
}
fn handle_known_packs(&mut self, server: &mut Server, config_acknowledged: SKnownPacks) { fn handle_known_packs(&mut self, server: &mut Server, config_acknowledged: SKnownPacks) {
self.send_packet(CRegistryData::new( self.send_packet(CRegistryData::new(
"dimension_type", "dimension_type",

View File

@ -15,7 +15,7 @@ use pumpkin_protocol::{
packet_decoder::PacketDecoder, packet_decoder::PacketDecoder,
packet_encoder::PacketEncoder, packet_encoder::PacketEncoder,
server::{ server::{
config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks}, config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks, SPluginMessage},
handshake::SHandShake, handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
status::{SPingRequest, SStatusRequest}, status::{SPingRequest, SStatusRequest},
@ -49,6 +49,7 @@ pub struct Client {
pub name: Option<String>, pub name: Option<String>,
pub uuid: Option<uuid::Uuid>, pub uuid: Option<uuid::Uuid>,
pub config: Option<PlayerConfig>, pub config: Option<PlayerConfig>,
pub brand: Option<String>,
pub connection_state: ConnectionState, pub connection_state: ConnectionState,
pub encrytion: bool, pub encrytion: bool,
@ -66,6 +67,7 @@ impl Client {
name: None, name: None,
uuid: None, uuid: None,
config: None, config: None,
brand: None,
token, token,
player: None, player: None,
connection_state: ConnectionState::HandShake, connection_state: ConnectionState::HandShake,
@ -169,6 +171,9 @@ impl Client {
SClientInformation::PACKET_ID => { SClientInformation::PACKET_ID => {
self.handle_client_information(server, SClientInformation::read(bytebuf)) self.handle_client_information(server, SClientInformation::read(bytebuf))
} }
SPluginMessage::PACKET_ID => {
self.handle_plugin_message(server, SPluginMessage::read(bytebuf))
}
SAcknowledgeFinishConfig::PACKET_ID => { SAcknowledgeFinishConfig::PACKET_ID => {
self.handle_config_acknowledged(server, SAcknowledgeFinishConfig::read(bytebuf)) self.handle_config_acknowledged(server, SAcknowledgeFinishConfig::read(bytebuf))
} }