Skip to content

Commit

Permalink
feat: basic unsigned chat
Browse files Browse the repository at this point in the history
  • Loading branch information
radstevee committed Dec 29, 2024
1 parent 1771a50 commit c6b576b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/bin/src/packet_handlers/chat_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use ferrumc_core::identity::player_identity::PlayerIdentity;
use ferrumc_macros::event_handler;
use ferrumc_net::{packets::{incoming::chat_message::ChatMessageEvent, outgoing::system_message::SystemMessagePacket}, utils::broadcast::{BroadcastOptions, BroadcastToAll}, NetResult};
use ferrumc_state::GlobalState;
use ferrumc_text::TextComponentBuilder;

#[event_handler]
async fn chat_message(
event: ChatMessageEvent,
state: GlobalState
) -> NetResult<ChatMessageEvent> {
let identity = state.universe.get::<PlayerIdentity>(event.player_conn_id)?;
let message = TextComponentBuilder::new(format!("<{}> {}", identity.username, event.message)).build();
let packet = SystemMessagePacket::new(message, false);
state.broadcast(&packet, BroadcastOptions::default().all()).await?;

Ok(event)
}
1 change: 1 addition & 0 deletions src/bin/src/packet_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mod handshake;
mod login_process;
mod tick_handler;
mod transform;
mod chat_message;
18 changes: 18 additions & 0 deletions src/lib/net/crates/codec/src/decode/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ where
}
}

/// This implementation assumes that the optional was written using PacketByteBuf#writeNullable and has a leading bool.
impl<T> NetDecode for Option<T>
where
T: NetDecode,
{
fn decode<R: Read>(reader: &mut R, opts: &NetDecodeOpts) -> NetDecodeResult<Self> {
let is_some = <bool as NetDecode>::decode(reader, opts)?;

if !is_some {
return Ok(None)
}

let value = <T as NetDecode>::decode(reader, opts)?;

Ok(Some(value))
}
}

/// This isn't actually a type in the Minecraft Protocol. This is just for saving data/ or for general use.
/// It was created for saving/reading chunks!
impl<K, V> NetDecode for HashMap<K, V>
Expand Down
42 changes: 42 additions & 0 deletions src/lib/net/src/packets/incoming/chat_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::sync::Arc;

use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use ferrumc_net_codec::net_types::var_int::VarInt;
use ferrumc_state::ServerState;

use crate::packets::IncomingPacket;

#[derive(NetDecode, Debug, Clone)]
#[packet(packet_id = 0x06, state = "play")]
pub struct ChatMessagePacket {
pub message: String,
pub timestamp: u64,
pub salt: u64,
pub has_signature: bool,
pub signature: Option<Vec<u64>>,
pub message_count: VarInt,
pub acknowledged: Vec<u8>
}

impl IncomingPacket for ChatMessagePacket {
async fn handle(
self,
conn_id: usize,
state: Arc<ServerState>,
) -> crate::NetResult<()> {
ChatMessageEvent::trigger(ChatMessageEvent::new(conn_id, self.message), state).await
}
}

#[derive(Debug, Event, Clone)]
pub struct ChatMessageEvent {
pub player_conn_id: usize,
pub message: String,
}

impl ChatMessageEvent {
pub fn new(player_conn_id: usize, message: String) -> Self {
Self { player_conn_id, message }
}
}
1 change: 1 addition & 0 deletions src/lib/net/src/packets/incoming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pub mod set_player_position_and_rotation;
pub mod set_player_rotation;

pub mod command;
pub mod chat_message;

0 comments on commit c6b576b

Please sign in to comment.