-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathplayer_leave.rs
More file actions
153 lines (145 loc) · 4.7 KB
/
player_leave.rs
File metadata and controls
153 lines (145 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use num_traits::cast::ToPrimitive;
use base::anvil::entity::{AnimalData, BaseEntityData};
use base::anvil::player::{InventorySlot, PlayerAbilities, PlayerData};
use base::{Gamemode, Inventory, Position, Text};
use common::entities::player::HotbarSlot;
use common::{chat::ChatKind, Game};
use ecs::{SysResult, SystemExecutor};
use quill_common::components::{
CanBuild, CanCreativeFly, CreativeFlying, CreativeFlyingSpeed, Health, Instabreak,
Invulnerable, Name, PreviousGamemode, WalkSpeed,
};
use crate::{ClientId, Server};
pub fn register(systems: &mut SystemExecutor<Game>) {
systems
.group::<Server>()
.add_system(remove_disconnected_clients);
}
fn remove_disconnected_clients(game: &mut Game, server: &mut Server) -> SysResult {
let mut entities_to_remove = Vec::new();
for (
player,
(
&client_id,
name,
position,
gamemode,
previous_gamemode,
health,
walk_speed,
fly_speed,
can_fly,
is_flying,
can_build,
instabreak,
invulnerable,
hotbar_slot,
inventory,
),
) in game
.ecs
.query::<(
&ClientId,
&Name,
&Position,
&Gamemode,
&PreviousGamemode,
&Health,
&WalkSpeed,
&CreativeFlyingSpeed,
&CanCreativeFly,
&CreativeFlying,
&CanBuild,
&Instabreak,
&Invulnerable,
&HotbarSlot,
&Inventory,
)>()
.iter()
{
let client = server.clients.get(client_id).unwrap();
if client.is_disconnected() {
entities_to_remove.push(player);
broadcast_player_leave(game, name);
game.world
.save_player_data(
client.uuid(),
&create_player_data(
*position,
*gamemode,
*previous_gamemode,
*health,
PlayerAbilities {
walk_speed: *walk_speed,
fly_speed: *fly_speed,
may_fly: *can_fly,
is_flying: *is_flying,
may_build: *can_build,
instabreak: *instabreak,
invulnerable: *invulnerable,
},
*hotbar_slot,
inventory,
),
)
.unwrap_or_else(|e| panic!("Couldn't save data for {}: {}", client.username(), e));
server.remove_client(client_id);
}
}
for player in entities_to_remove {
game.remove_entity(player)?;
}
Ok(())
}
fn broadcast_player_leave(game: &Game, username: &Name) {
let message = Text::translate_with("multiplayer.player.left", vec![username.to_string()]);
game.broadcast_chat(ChatKind::System, message);
}
fn create_player_data(
position: Position,
gamemode: Gamemode,
previous_gamemode: PreviousGamemode,
health: Health,
abilities: PlayerAbilities,
hotbar_slot: HotbarSlot,
inventory: &Inventory,
) -> PlayerData {
PlayerData {
animal: AnimalData {
base: BaseEntityData {
position: [position.x, position.y, position.z].into(),
rotation: [position.yaw, position.pitch].into(),
velocity: [0.0, 0.0, 0.0].into(),
},
health: *health,
},
gamemode: gamemode.to_i32().unwrap(),
previous_gamemode: previous_gamemode.id() as i32,
inventory: inventory
.to_vec()
.iter()
.enumerate()
// Here we filter out all empty slots.
.filter_map(|(slot, item)| {
match item {
libcraft_items::InventorySlot::Filled(item) => {
let res = InventorySlot::from_network_index(slot, item);
match res {
Some(i) => Some(i),
None => {
log::error!("Failed to convert the slot into anvil format.");
None
}
}
}
libcraft_items::InventorySlot::Empty => {
// Empty items are filtered out.
None
}
}
})
.collect(),
held_item: hotbar_slot.get() as i32,
abilities,
}
}