-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathjoinhandler.rs
More file actions
205 lines (180 loc) · 6.71 KB
/
joinhandler.rs
File metadata and controls
205 lines (180 loc) · 6.71 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! The join handler, in contrast to the initial handler,
//! takes over after the login sequence has completed.
//! It's responsible for asyncrhonously loading the player's
//! data (inventory, chunks, etc.) and then sending the necessary
//! packets to join the player. After completion, the component is
//! removed.
use std::sync::atomic::Ordering;
use std::sync::Arc;
use shrev::EventChannel;
use specs::{
Component, Entities, Entity, HashMapStorage, Join, LazyUpdate, Read, ReadStorage, System,
Write, WriteStorage,
};
use feather_core::network::packet::implementation::{
JoinGame, PlayerPositionAndLookClientbound, SpawnPosition,
};
use feather_core::world::{BlockPosition, ChunkMap, ChunkPosition, Position};
use feather_core::{Difficulty, Dimension, Gamemode};
use crate::chunk_logic::{ChunkHolderComponent, ChunkHolders, ChunkWorkerHandle};
use crate::config::Config;
use crate::network::NetworkComponent;
use crate::player::{ChunkPendingComponent, LoadedChunksComponent};
use crate::PlayerCount;
use feather_core::level::LevelData;
/// For now, we use a fixed spawn position.
/// In the future, the spawn position should
/// be loaded asynchronously from the world save.
pub const SPAWN_POSITION: Position = Position {
x: 0.0,
y: 64.0,
z: 0.0,
pitch: 0.0,
yaw: 0.0,
};
#[derive(Default)]
pub struct JoinHandlerComponent {
stage: Stage,
}
impl JoinHandlerComponent {
pub fn new() -> Self {
Self {
stage: Stage::Initial,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Stage {
Initial,
AwaitChunkSends,
}
impl Default for Stage {
fn default() -> Self {
Stage::Initial
}
}
impl Component for JoinHandlerComponent {
type Storage = HashMapStorage<Self>;
}
/// Event which is triggered when a player
/// completes the join process (i.e. when
/// all chunks have been sent).
pub struct PlayerJoinEvent {
pub player: Entity,
}
/// System for join handling.
pub struct JoinHandlerSystem;
impl<'a> System<'a> for JoinHandlerSystem {
type SystemData = (
WriteStorage<'a, JoinHandlerComponent>,
ReadStorage<'a, NetworkComponent>,
ReadStorage<'a, ChunkPendingComponent>,
Write<'a, EventChannel<PlayerJoinEvent>>,
Read<'a, ChunkWorkerHandle>,
Entities<'a>,
Read<'a, LazyUpdate>,
Read<'a, Arc<Config>>,
Read<'a, Arc<PlayerCount>>,
Read<'a, ChunkMap>,
Write<'a, ChunkHolders>,
Read<'a, LevelData>,
WriteStorage<'a, ChunkHolderComponent>,
WriteStorage<'a, LoadedChunksComponent>,
);
fn run(&mut self, data: Self::SystemData) {
let (
mut joincomps,
netcomps,
pending_chunks,
mut join_events,
worker_handle,
entities,
lazy,
config,
player_count,
chunk_map,
mut holders,
level,
mut holder_comps,
mut loaded_chunks_comps,
) = data;
let mut to_remove = vec![];
for (player, net, join_handler, pending_chunks) in
(&entities, &netcomps, &mut joincomps, &pending_chunks).join()
{
match join_handler.stage {
Stage::Initial => {
// Send Join Game, then queue chunks for loading + sending.
let join_game = JoinGame::new(
player.id() as i32,
Gamemode::Creative.get_id(),
Dimension::Overwold.get_id(),
Difficulty::Medium.get_id(),
0, // Max players - not used
"default".to_string(), // Level type
false, // Reduced debug info
);
crate::network::send_packet_to_player(net, join_game);
let mut holder_comp = ChunkHolderComponent::new();
let mut loaded_chunks_comp = LoadedChunksComponent::default();
// Queue chunks
let view_distance = i32::from(config.server.view_distance);
for x in -view_distance..=view_distance {
for y in -view_distance..=view_distance {
let pos = ChunkPosition::new(x, y);
crate::player::send_chunk_to_player(
pos,
net,
player,
&chunk_map,
&worker_handle,
&mut holders,
&mut holder_comp,
&mut loaded_chunks_comp,
&lazy,
);
}
}
holder_comps.insert(player, holder_comp).unwrap();
loaded_chunks_comps
.insert(player, loaded_chunks_comp)
.unwrap();
// Increment player count
player_count.0.fetch_add(1, Ordering::SeqCst);
join_handler.stage = Stage::AwaitChunkSends;
}
Stage::AwaitChunkSends => {
// If 0 chunks have yet to be sent, join the player by sending spawn position.
// See https://wiki.vg/Protocol_FAQ
if pending_chunks.len() != 0 {
continue;
}
let spawn_position = SpawnPosition::new(BlockPosition::new(
level.spawn_x,
level.spawn_y,
level.spawn_z,
));
crate::network::send_packet_to_player(net, spawn_position);
let position_and_look = PlayerPositionAndLookClientbound::new(
SPAWN_POSITION.x,
SPAWN_POSITION.y,
SPAWN_POSITION.z,
SPAWN_POSITION.yaw,
SPAWN_POSITION.pitch,
0, // Flags - unused by us
0, // Teleport ID - unused by us
);
crate::network::send_packet_to_player(net, position_and_look);
// Trigger event
let event = PlayerJoinEvent { player };
join_events.single_write(event);
// We're finished here.
to_remove.push(player);
}
}
}
to_remove.into_iter().for_each(|player| {
joincomps.remove(player);
});
}
}