forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
118 lines (104 loc) · 3.69 KB
/
mod.rs
File metadata and controls
118 lines (104 loc) · 3.69 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
//! Systems and components specific to player entities.
use crate::broadcasters::LastKnownPositions;
use crate::chunk_logic::ChunkHolder;
use crate::entity;
use crate::entity::{CreationPacketCreator, EntityId, Name, PreviousPosition, SpawnPacketCreator};
use crate::io::NewClientInfo;
use crate::network::Network;
use crate::p_inventory::EntityInventory;
use crate::util::degrees_to_stops;
use feather_core::network::packet::implementation::{PlayerInfo, PlayerInfoAction, SpawnPlayer};
use feather_core::{Gamemode, Packet, Position};
use fecs::{Entity, EntityRef, World};
use mojang_api::ProfileProperty;
use uuid::Uuid;
pub const PLAYER_EYE_HEIGHT: f64 = 1.62;
/// Profile properties of a player.
#[derive(Debug, Clone)]
pub struct ProfileProperties(pub Vec<ProfileProperty>);
/// Zero-sized component used to mark players.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Player;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ItemTimedUse {
pub tick_start: u64,
}
/// Creates a new player from the given `NewClientInfo`.
///
/// This function also triggers the `PlayerJoinEvent` for this player.
pub fn create(world: &mut World, info: NewClientInfo) -> Entity {
// TODO: blocked on https://github.com/TomGillen/legion/issues/36
let entity = info.entity;
world.add(entity, EntityId(entity::new_id())).unwrap();
world.add(entity, info.position).unwrap();
world.add(entity, PreviousPosition(info.position)).unwrap();
world.add(entity, info.uuid).unwrap();
world.add(entity, info.uuid).unwrap();
world
.add(
entity,
Network {
tx: info.sender,
rx: info.receiver,
},
)
.unwrap();
world.add(entity, info.ip).unwrap();
world.add(entity, ProfileProperties(info.profile)).unwrap();
world.add(entity, Name(info.username)).unwrap();
world.add(entity, ChunkHolder::default()).unwrap();
world.add(entity, LastKnownPositions::default()).unwrap();
world
.add(entity, SpawnPacketCreator(&create_spawn_packet))
.unwrap();
world
.add(entity, CreationPacketCreator(&create_initialization_packet))
.unwrap();
world.add(entity, Gamemode::Creative).unwrap(); // TODO: proper gamemode handling
world.add(entity, EntityInventory::default()).unwrap();
world.add(entity, Player).unwrap();
entity
}
/// Function to create a `SpawnPlayer` packet to spawn the player.
fn create_spawn_packet(accessor: &EntityRef) -> Box<dyn Packet> {
let entity_id = accessor.get::<EntityId>().0;
let player_uuid = *accessor.get::<Uuid>();
let pos = *accessor.get::<Position>();
// TODO: metadata
let packet = SpawnPlayer {
entity_id,
player_uuid,
x: pos.x,
y: pos.y,
z: pos.z,
yaw: degrees_to_stops(pos.yaw),
pitch: degrees_to_stops(pos.pitch),
metadata: Default::default(),
};
Box::new(packet)
}
/// Function to create a `PlayerInfo` packet to broadcast when the player joins.
fn create_initialization_packet(accessor: &EntityRef) -> Box<dyn Packet> {
let name = accessor.get::<Name>();
let props = accessor.get::<ProfileProperties>();
let uuid = *accessor.get::<Uuid>();
let props = props
.0
.iter()
.map(|prop| {
(
prop.name.clone(),
prop.value.clone(),
prop.signature.clone(),
)
})
.collect::<Vec<_>>();
let display_name = json!({
"text": name.0
})
.to_string();
let action =
PlayerInfoAction::AddPlayer(name.0.clone(), props, Gamemode::Creative, 50, display_name);
let packet = PlayerInfo { action, uuid };
Box::new(packet)
}