Skip to content

Commit c6cb112

Browse files
authored
Merge pull request #220 from Sunnickel/feature/join-message
feature: Added Join and Leave messages
2 parents e2d901b + e163515 commit c6cb112

File tree

6 files changed

+78
-30
lines changed

6 files changed

+78
-30
lines changed

src/bin/src/systems/connection_killer.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::systems::system_messages;
12
use bevy_ecs::prelude::{Commands, Entity, Query, Res};
23
use ferrumc_core::identity::player_identity::PlayerIdentity;
34
use ferrumc_net::connection::StreamWriter;
@@ -11,40 +12,48 @@ pub fn connection_killer(
1112
state: Res<GlobalStateResource>,
1213
) {
1314
while let Some((disconnecting_entity, reason)) = state.0.players.disconnection_queue.pop() {
14-
for (entity, conn, player_identity) in query.iter() {
15-
if disconnecting_entity == entity {
16-
info!(
17-
"Player {} ({}) disconnected: {}",
18-
player_identity.username,
19-
player_identity.uuid,
20-
reason.as_deref().unwrap_or("No reason")
21-
);
22-
if conn.running.load(std::sync::atomic::Ordering::Relaxed) {
23-
trace!(
24-
"Sending disconnect packet to player {}",
25-
player_identity.username
26-
);
27-
if let Err(e) = conn.send_packet_ref(
28-
&ferrumc_net::packets::outgoing::disconnect::DisconnectPacket {
29-
reason: TextComponent::from(
30-
reason.as_deref().unwrap_or("Disconnected"),
31-
),
32-
},
33-
) {
34-
warn!(
35-
"Failed to send disconnect packet to player {}: {:?}",
36-
player_identity.username, e
15+
let entity_result = query.get(disconnecting_entity);
16+
match entity_result {
17+
Ok(disconnecting_player) => {
18+
for (entity, conn, player_identity) in query.iter() {
19+
if disconnecting_entity == entity {
20+
info!(
21+
"Player {} ({}) disconnected: {}",
22+
player_identity.username,
23+
player_identity.uuid,
24+
reason.as_deref().unwrap_or("No reason")
3725
);
38-
}
39-
} else {
40-
trace!(
26+
if conn.running.load(std::sync::atomic::Ordering::Relaxed) {
27+
trace!(
28+
"Sending disconnect packet to player {}",
29+
player_identity.username
30+
);
31+
if let Err(e) = conn.send_packet_ref(
32+
&ferrumc_net::packets::outgoing::disconnect::DisconnectPacket {
33+
reason: TextComponent::from(
34+
reason.as_deref().unwrap_or("Disconnected"),
35+
),
36+
},
37+
) {
38+
warn!(
39+
"Failed to send disconnect packet to player {}: {:?}",
40+
player_identity.username, e
41+
);
42+
}
43+
} else {
44+
trace!(
4145
"Connection for player {} is not running, skipping disconnect packet",
4246
player_identity.username
4347
);
48+
}
49+
} else {
50+
system_messages::player_leave::handle(disconnecting_player.2, entity);
51+
}
52+
cmd.entity(entity).despawn();
4453
}
45-
cmd.entity(entity).despawn();
46-
} else {
47-
// Broadcast the disconnection to other players
54+
}
55+
Err(e) => {
56+
warn!("Player's entity has already been removed: {}", e);
4857
}
4958
}
5059
}

src/bin/src/systems/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod new_connections;
77
pub mod player_count_update;
88
pub mod send_chunks;
99
pub mod shutdown_systems;
10+
mod system_messages;
1011
pub mod world_sync;
1112

1213
pub fn register_game_systems(schedule: &mut bevy_ecs::schedule::Schedule) {

src/bin/src/systems/new_connections.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::systems::system_messages;
12
use bevy_ecs::prelude::{Commands, Res, Resource};
23
use crossbeam_channel::Receiver;
34
use ferrumc_core::chunks::chunk_receiver::ChunkReceiver;
@@ -55,9 +56,19 @@ pub fn accept_new_connections(
5556
entity.id(),
5657
(
5758
new_connection.player_identity.uuid.as_u128(),
58-
new_connection.player_identity.username,
59+
new_connection.player_identity.username.clone(),
5960
),
6061
);
62+
63+
for player in &state.0.players.player_list {
64+
if player.value().0 != new_connection.player_identity.uuid.as_u128() {
65+
system_messages::player_join::handle(
66+
&new_connection.player_identity,
67+
*player.key(),
68+
);
69+
}
70+
}
71+
6172
if let Err(err) = return_sender.send(entity.id()) {
6273
error!(
6374
"Failed to send entity ID back to the networking thread: {:?}",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod player_join;
2+
pub mod player_leave;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use bevy_ecs::entity::Entity;
2+
use ferrumc_core::identity::player_identity::PlayerIdentity;
3+
use ferrumc_core::mq;
4+
use ferrumc_text::{Color, NamedColor, TextComponent};
5+
6+
pub fn handle(disconnecting_player: &PlayerIdentity, receiver_player: Entity) {
7+
let mut message =
8+
TextComponent::from(format!("{} joined the game", disconnecting_player.username));
9+
let color: Color = Color::Named(NamedColor::Yellow);
10+
message.color = Some(color);
11+
12+
mq::queue(message, false, receiver_player);
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use bevy_ecs::entity::Entity;
2+
use ferrumc_core::identity::player_identity::PlayerIdentity;
3+
use ferrumc_core::mq;
4+
use ferrumc_text::{Color, NamedColor, TextComponent};
5+
6+
pub fn handle(joining_player: &PlayerIdentity, receiver_player: Entity) {
7+
let mut message = TextComponent::from(format!("{} left the game", joining_player.username));
8+
let color: Color = Color::Named(NamedColor::Yellow);
9+
message.color = Some(color);
10+
11+
mq::queue(message, false, receiver_player);
12+
}

0 commit comments

Comments
 (0)