forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin.rs
More file actions
78 lines (66 loc) · 2.22 KB
/
join.rs
File metadata and controls
78 lines (66 loc) · 2.22 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
//! After chunks are sent to a client, we complete the login sequence
//! by sending Spawn Position, Player Position and Look, and inventory,
//! among others. This is handled by the event handler `join`.
use crate::entity::EntityId;
use crate::game::Game;
use crate::network::Network;
use feather_core::network::packet::implementation::{
JoinGame, PlayerPositionAndLookClientbound, SpawnPosition,
};
use feather_core::{BlockPosition, ChunkPosition, Difficulty, Dimension, Gamemode, Position};
use fecs::{Entity, World};
/// Component indicating that a player has completed the join sequence.
#[derive(Default, Debug)]
pub struct Joined;
/// System to run the join sequence. To determine when a player is ready to join,
/// we wait for the chunk that the player is in to be sent—this appears to work
/// well with the client.
pub fn on_chunk_send_join_player(
game: &Game,
world: &mut World,
chunk: ChunkPosition,
player: Entity,
) {
if world.try_get::<Joined>(player).is_some() {
return; // already joined
}
let pos = {
let pos = world.get::<Position>(player);
if pos.chunk() != chunk {
return;
}
*pos
};
// Run the join sequence.
world.add(player, Joined).unwrap();
let network = world.get::<Network>(player);
let packet = SpawnPosition {
location: BlockPosition::new(game.level.spawn_x, game.level.spawn_y, game.level.spawn_z),
};
network.send(packet);
let packet = PlayerPositionAndLookClientbound {
x: pos.x,
y: pos.y,
z: pos.z,
yaw: pos.yaw,
pitch: pos.pitch,
flags: 0,
teleport_id: 0,
};
network.send(packet);
}
pub fn on_player_join_send_join_game(game: &Game, world: &World, player: Entity) {
let network = world.get::<Network>(player);
let id = world.get::<EntityId>(player);
// TODO
let packet = JoinGame {
entity_id: id.0,
gamemode: Gamemode::Creative.id(),
dimension: Dimension::Overwold.id(),
difficulty: Difficulty::Medium.id(),
max_players: game.config.server.max_players as u8,
level_type: game.level.generator_name.clone(),
reduced_debug_info: false,
};
network.send(packet);
}