forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.rs
More file actions
59 lines (54 loc) · 1.86 KB
/
load.rs
File metadata and controls
59 lines (54 loc) · 1.86 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
use ahash::AHashMap;
use feather_core::anvil::{
block_entity::{BlockEntityData, BlockEntityVariant},
entity::{EntityData, EntityDataKind},
};
use feather_server_types::{
BlockEntityLoaderFn, BlockEntityLoaderRegistration, EntityLoaderFn, EntityLoaderRegistration,
};
use fecs::EntityBuilder;
/// Stores state for loading entities.
pub struct EntityLoader {
/// Map from `EntityDataKind` to functions
/// to load entities of those kinds.
loaders: AHashMap<EntityDataKind, &'static dyn EntityLoaderFn>,
block_loaders: AHashMap<BlockEntityVariant, &'static dyn BlockEntityLoaderFn>,
}
impl Default for EntityLoader {
fn default() -> Self {
Self::new()
}
}
impl EntityLoader {
/// Initializes a new entity loader state. This function allocates.
pub fn new() -> Self {
let loaders = inventory::iter::<EntityLoaderRegistration>
.into_iter()
.map(|registration| (registration.kind, registration.f))
.collect();
let block_loaders = inventory::iter::<BlockEntityLoaderRegistration>
.into_iter()
.map(|registration| (registration.kind, registration.f))
.collect();
Self {
loaders,
block_loaders,
}
}
}
impl EntityLoader {
/// Converts an `EntityData` into an `EntityBuilder`
/// ready for spawning in a `World`.
pub fn load(&self, data: EntityData) -> Option<anyhow::Result<EntityBuilder>> {
self.loaders
.get(&EntityDataKind::from(&data))
.map(|loader| loader(data))
}
/// Converts a `BlockEntityData` into an `EntityBuilder`
/// ready for spawning in a `World`.
pub fn load_block(&self, data: BlockEntityData) -> Option<anyhow::Result<EntityBuilder>> {
self.block_loaders
.get(&data.kind.variant())
.map(|loader| loader(data))
}
}