Skip to content

Commit

Permalink
Remove player when leaving
Browse files Browse the repository at this point in the history
- Add Clientbound Remove Entities
  • Loading branch information
Snowiiii committed Aug 7, 2024
1 parent 4f06496 commit 3998524
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
16 changes: 16 additions & 0 deletions pumpkin-protocol/src/client/play/c_remove_entities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::VarInt;

#[derive(Serialize, Clone)]
#[packet(0x42)]
pub struct CRemoveEntities {
entitiy_ids: Vec<VarInt>,
}

impl CRemoveEntities {
pub fn new(entitiy_ids: Vec<VarInt>) -> Self {
Self { entitiy_ids }
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod c_play_disconnect;
mod c_player_abilities;
mod c_player_chat_message;
mod c_player_info_update;
mod c_remove_entities;
mod c_set_held_item;
mod c_spawn_player;
mod c_sync_player_position;
Expand All @@ -29,6 +30,7 @@ pub use c_play_disconnect::*;
pub use c_player_abilities::*;
pub use c_player_chat_message::*;
pub use c_player_info_update::*;
pub use c_remove_entities::*;
pub use c_set_held_item::*;
pub use c_spawn_player::*;
pub use c_sync_player_position::*;
Expand Down
4 changes: 3 additions & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ impl Client {
SPlayerCommand::PACKET_ID => {
self.handle_player_command(server, SPlayerCommand::read(bytebuf).unwrap())
}
SSwingArm::PACKET_ID => self.handle_swing_arm(server, SSwingArm::read(bytebuf).unwrap()),
SSwingArm::PACKET_ID => {
self.handle_swing_arm(server, SSwingArm::read(bytebuf).unwrap())
}
_ => log::error!("Failed to handle player packet id {}", packet.id.0),
}
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ fn main() -> io::Result<()> {
};
if done {
if let Some(client) = connections.remove(&token) {
let mut client = client.borrow_mut();
server.remove_client(&token);
let mut client = client.borrow_mut();
poll.registry().deregister(&mut client.connection)?;
}
}
Expand Down
11 changes: 9 additions & 2 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use pumpkin_protocol::{
config::CPluginMessage,
play::{
CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, CPlayerInfoUpdate,
CSpawnEntity, PlayerAction,
CRemoveEntities, CSpawnEntity, PlayerAction,
},
},
BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL,
Expand Down Expand Up @@ -109,7 +109,14 @@ impl Server {
}

pub fn remove_client(&mut self, token: &Token) {
self.current_clients.remove(token);
let client = self.current_clients.remove(token).unwrap();
let mut client = client.borrow_mut();
// despawn the player
// todo: put this into the entitiy struct
if client.is_player() {
let id = client.player.as_ref().unwrap().entity_id();
self.broadcast_packet(&mut client, CRemoveEntities::new(vec![id.into()]))
}
}

// here is where the magic happens
Expand Down

0 comments on commit 3998524

Please sign in to comment.