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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
mod clients;
mod components;
mod config;
mod entities;
mod events;
mod generators;
mod messages;
mod physics;
mod registry;
mod search;
mod stats;
mod systems;
mod types;
mod utils;
mod voxels;

use actix::Recipient;
use hashbrown::HashMap;
use log::{info, warn};
use nanoid::nanoid;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use specs::{
    shred::{Fetch, FetchMut, Resource},
    Builder, Component, DispatcherBuilder, Entity, EntityBuilder, Join, ReadStorage, SystemData,
    World as ECSWorld, WorldExt, WriteStorage,
};
use std::fs::{self, File};
use std::path::PathBuf;
use std::{env, hash::Hash};

use crate::{
    encode_message,
    protocols::Peer,
    server::{Message, MessageType},
    EncodedMessage, EntityProtocol, PeerProtocol, Vec2, Vec3,
};

use super::common::ClientFilter;

pub use clients::*;
pub use components::*;
pub use config::*;
pub use entities::*;
pub use events::*;
pub use generators::*;
pub use messages::*;
pub use physics::*;
pub use registry::*;
pub use search::*;
pub use stats::*;
pub use systems::*;
pub use types::*;
pub use utils::*;
pub use voxels::*;

pub type ModifyDispatch = fn() -> DispatcherBuilder<'static, 'static>;

pub type CustomFunction<T> = fn(T, &mut World);

pub type ClientParser = fn(&str, Entity, &mut World);

pub type Transports = HashMap<String, Recipient<EncodedMessage>>;

/// The default client metadata parser, parses PositionComp and DirectionComp, and updates RigidBodyComp.
pub fn default_client_parser(metadata: &str, client_ent: Entity, world: &mut World) {
    let metadata: PeerUpdate =
        serde_json::from_str(metadata).expect("Could not parse peer update.");

    if let Some(position) = metadata.position {
        {
            let mut positions = world.write_component::<PositionComp>();
            if let Some(p) = positions.get_mut(client_ent) {
                p.0.set(position.0, position.1, position.2);
            }
        }

        {
            let mut bodies = world.write_component::<RigidBodyComp>();
            if let Some(b) = bodies.get_mut(client_ent) {
                b.0.set_position(position.0, position.1, position.2);
            }
        }
    }

    if let Some(direction) = metadata.direction {
        let mut directions = world.write_component::<DirectionComp>();
        if let Some(d) = directions.get_mut(client_ent) {
            d.0.set(direction.0, direction.1, direction.2);
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PeerUpdate {
    position: Option<Vec3<f32>>,
    direction: Option<Vec3<f32>>,
}

/// A voxelize world.
#[derive(Default)]
pub struct World {
    /// ID of the world, generated from `nanoid!()`.
    pub id: String,

    /// Name of the world, used for connection.
    pub name: String,

    /// Whether if the world has started.
    pub started: bool,

    /// Entity component system world.
    ecs: ECSWorld,

    /// The modifier of the ECS dispatcher.
    dispatcher: Option<ModifyDispatch>,

    /// The modifier of any new client.
    client_modifier: Option<CustomFunction<Entity>>,

    /// The metadata parser for clients.
    client_parser: Option<ClientParser>,

    /// The handler for `Method`s.
    method_handles: HashMap<String, CustomFunction<Value>>,

    /// The handler for `Transport`s.
    transport_handle: Option<CustomFunction<Value>>,
}

fn dispatcher() -> DispatcherBuilder<'static, 'static> {
    DispatcherBuilder::new()
        .with(UpdateStatsSystem, "update-stats", &[])
        .with(EntityMetaSystem, "entity-meta", &[])
        .with(PeersMetaSystem, "peers-meta", &[])
        .with(CurrentChunkSystem, "current-chunking", &[])
        .with(ChunkUpdatingSystem, "chunk-updating", &["current-chunking"])
        .with(ChunkRequestsSystem, "chunk-requests", &["current-chunking"])
        .with(
            ChunkPipeliningSystem,
            "chunk-pipelining",
            &["chunk-requests"],
        )
        .with(ChunkMeshingSystem, "chunk-meshing", &["chunk-pipelining"])
        .with(ChunkSendingSystem, "chunk-sending", &["chunk-meshing"])
        .with(ChunkSavingSystem, "chunk-saving", &["chunk-pipelining"])
        .with(PhysicsSystem, "physics", &["update-stats"])
        .with(EntitiesSavingSystem, "entities-saving", &["entity-meta"])
        .with(
            EntitiesSendingSystem,
            "entities-sending",
            &["entities-saving"],
        )
        .with(PeersSendingSystem, "peers-sending", &["peers-meta"])
        .with(
            BroadcastSystem,
            "broadcast",
            &["entities-sending", "peers-sending", "chunk-sending"],
        )
        .with(
            ClearCollisionsSystem,
            "clear-collisions",
            &["entities-sending"],
        )
        .with(
            EventsBroadcastSystem,
            "events-broadcasting",
            &["chunk-requests", "broadcast"],
        )
}

#[derive(Serialize, Deserialize)]
struct OnLoadRequest {
    chunks: Vec<Vec2<i32>>,
}

#[derive(Serialize, Deserialize)]
struct OnUnloadRequest {
    chunks: Vec<Vec2<i32>>,
}

#[derive(Serialize, Deserialize)]
struct OnMethodRequest {
    method: String,
    data: Value,
}

impl World {
    /// Create a new voxelize world.
    pub fn new(name: &str, config: &WorldConfig) -> Self {
        let id = nanoid!();

        if config.saving {
            let folder = PathBuf::from(&config.save_dir);

            if !folder.exists() {
                panic!(
                    "World folder not created at: '{}'",
                    if folder.is_absolute() {
                        folder.to_path_buf()
                    } else {
                        if let Ok(curr_dir) = env::current_dir() {
                            curr_dir.join(folder)
                        } else {
                            folder
                        }
                    }
                    .to_string_lossy()
                );
            }

            info!("Storage for world '{}' is at '{}'", name, config.save_dir);
        } else {
            info!("World '{}' is temporarily saved in memory.", name);
        }

        let mut ecs = ECSWorld::new();

        ecs.register::<ChunkRequestsComp>();
        ecs.register::<CurrentChunkComp>();
        ecs.register::<IDComp>();
        ecs.register::<NameComp>();
        ecs.register::<PositionComp>();
        ecs.register::<DirectionComp>();
        ecs.register::<ClientFlag>();
        ecs.register::<EntityFlag>();
        ecs.register::<ETypeComp>();
        ecs.register::<MetadataComp>();
        ecs.register::<RigidBodyComp>();
        ecs.register::<AddrComp>();
        ecs.register::<InteractorComp>();
        ecs.register::<CollisionsComp>();
        ecs.register::<AnimationComp>();

        ecs.insert(name.to_owned());
        ecs.insert(config.clone());

        ecs.insert(Chunks::new(config));
        ecs.insert(SeededNoise::new(config.seed));
        ecs.insert(SeededTerrain::new(config.seed, &config.terrain));
        ecs.insert(Entities::new(config.saving, &config.save_dir));
        ecs.insert(Search::new());

        ecs.insert(Mesher::new());
        ecs.insert(Pipeline::new());
        ecs.insert(Clients::new());
        ecs.insert(MessageQueue::new());
        ecs.insert(Stats::new());
        ecs.insert(Physics::new());
        ecs.insert(Events::new());
        ecs.insert(Transports::new());

        Self {
            id,
            name: name.to_owned(),
            started: false,

            ecs,

            dispatcher: Some(dispatcher),
            method_handles: HashMap::default(),
            client_parser: Some(default_client_parser),
            client_modifier: None,
            transport_handle: None,
        }
    }

    /// Get a reference to the ECS world..
    pub fn ecs(&self) -> &ECSWorld {
        &self.ecs
    }

    /// Get a mutable reference to the ECS world.
    pub fn ecs_mut(&mut self) -> &mut ECSWorld {
        &mut self.ecs
    }

    /// Insert a component into an entity.
    pub fn add<T: Component>(&mut self, e: Entity, c: T) {
        let mut storage: WriteStorage<T> = SystemData::fetch(self.ecs());
        storage.insert(e, c).unwrap();
    }

    /// Remove a component type from an entity.
    pub fn remove<T: Component>(&mut self, e: Entity) {
        let mut storage: WriteStorage<T> = SystemData::fetch(self.ecs());
        storage.remove(e);
    }

    /// Read an ECS resource generically.
    pub fn read_resource<T: Resource>(&self) -> Fetch<T> {
        self.ecs.read_resource::<T>()
    }

    /// Write an ECS resource generically.
    pub fn write_resource<T: Resource>(&mut self) -> FetchMut<T> {
        self.ecs.write_resource::<T>()
    }

    /// Read an ECS component storage.
    pub fn read_component<T: Component>(&self) -> ReadStorage<T> {
        self.ecs.read_component::<T>()
    }

    /// Write an ECS component storage.
    pub fn write_component<T: Component>(&mut self) -> WriteStorage<T> {
        self.ecs.write_component::<T>()
    }

    /// Get an ID from IDComp from an entity
    pub fn get_id(&self, entity: Entity) -> String {
        if let Some(id) = self.read_component::<IDComp>().get(entity) {
            id.0.to_owned()
        } else {
            panic!("Something went wrong! An entity does not have an `IDComp` attached!");
        }
    }

    /// Add a transport address to this world.
    pub fn add_transport(&mut self, id: &str, addr: &Recipient<EncodedMessage>) {
        let init_message = self.generate_init_message(id);
        self.send(addr, &init_message);
        self.write_resource::<Transports>()
            .insert(id.to_owned(), addr.to_owned());
    }

    /// Remove a transport address from this world.
    pub fn remove_transport(&mut self, id: &str) {
        self.write_resource::<Transports>().remove(id);
    }

    /// Add a client to the world by an ID and an Actix actor address.
    pub fn add_client(&mut self, id: &str, username: &str, addr: &Recipient<EncodedMessage>) {
        let init_message = self.generate_init_message(id);

        let body =
            RigidBody::new(&AABB::new().scale_x(0.8).scale_y(1.8).scale_z(0.8).build()).build();

        let interactor = self.physics_mut().register(&body);

        let ent = self
            .ecs
            .create_entity()
            .with(ClientFlag::default())
            .with(IDComp::new(id))
            .with(NameComp::new(username))
            .with(AddrComp::new(addr))
            .with(ChunkRequestsComp::default())
            .with(CurrentChunkComp::default())
            .with(MetadataComp::default())
            .with(PositionComp::default())
            .with(DirectionComp::default())
            .with(RigidBodyComp::new(&body))
            .with(InteractorComp::new(interactor))
            .with(CollisionsComp::new())
            .build();

        if let Some(modifier) = self.client_modifier.to_owned() {
            modifier(ent, self);
        }

        self.clients_mut().insert(
            id.to_owned(),
            Client {
                id: id.to_owned(),
                entity: ent,
                username: username.to_owned(),
                addr: addr.to_owned(),
            },
        );

        self.send(addr, &init_message);

        let join_message = Message::new(&MessageType::Join).text(id).build();
        self.broadcast(join_message, ClientFilter::All);

        info!("Client at {} joined the server to world: {}", id, self.name);
    }

    /// Remove a client from the world by endpoint.
    pub fn remove_client(&mut self, id: &str) {
        let removed = self.clients_mut().remove(id);

        if let Some(client) = removed {
            {
                let entities = self.ecs.entities();

                entities.delete(client.entity).unwrap_or_else(|_| {
                    panic!(
                        "Something went wrong with deleting this client: {}",
                        client.id
                    )
                });
            }

            self.ecs.maintain();

            let leave_message = Message::new(&MessageType::Leave).text(&client.id).build();
            self.broadcast(leave_message, ClientFilter::All);
            info!("Client at {} left the world: {}", id, self.name);
        }
    }

    pub fn set_dispatcher(&mut self, dispatch: ModifyDispatch) {
        self.dispatcher = Some(dispatch);
    }

    pub fn set_client_modifier(&mut self, modifier: CustomFunction<Entity>) {
        self.client_modifier = Some(modifier);
    }

    pub fn set_client_parser(&mut self, parser: ClientParser) {
        self.client_parser = Some(parser);
    }

    pub fn set_method_handle(&mut self, method: &str, handle: CustomFunction<Value>) {
        self.method_handles.insert(method.to_lowercase(), handle);
    }

    pub fn set_transport_handle(&mut self, handle: CustomFunction<Value>) {
        self.transport_handle = Some(handle);
    }

    pub fn generate_init_message(&self, id: &str) -> Message {
        let config = self.config().get_init_config();
        let mut json = HashMap::new();

        json.insert("id".to_owned(), json!(id));
        json.insert("blocks".to_owned(), json!(self.registry().blocks_by_name));
        json.insert("ranges".to_owned(), json!(self.registry().ranges));
        json.insert("params".to_owned(), json!(config));

        /* ------------------------ Loading other the clients ----------------------- */
        let ids = self.read_component::<IDComp>();
        let flags = self.read_component::<ClientFlag>();
        let names = self.read_component::<NameComp>();
        let metadatas = self.read_component::<MetadataComp>();

        let mut peers = vec![];

        for (pid, name, metadata, _) in (&ids, &names, &metadatas, &flags).join() {
            peers.push(PeerProtocol {
                id: pid.0.to_owned(),
                username: name.0.to_owned(),
                metadata: metadata.to_string(),
            })
        }

        /* -------------------------- Loading all entities -------------------------- */
        let etypes = self.read_component::<ETypeComp>();
        let metadatas = self.read_component::<MetadataComp>();

        let mut entities = vec![];

        for (id, etype, metadata) in (&ids, &etypes, &metadatas).join() {
            if metadata.is_empty() {
                continue;
            }

            let j_str = metadata.to_string();

            entities.push(EntityProtocol {
                id: id.0.to_owned(),
                r#type: etype.0.to_owned(),
                metadata: Some(j_str),
            });
        }

        drop(ids);
        drop(etypes);
        drop(metadatas);

        Message::new(&MessageType::Init)
            .json(&serde_json::to_string(&json).unwrap())
            .peers(&peers)
            .entities(&entities)
            .build()
    }

    /// Handler for protobuf requests from clients.
    pub fn on_request(&mut self, client_id: &str, data: Message) {
        let msg_type = MessageType::from_i32(data.r#type).unwrap();

        match msg_type {
            MessageType::Peer => self.on_peer(client_id, data),
            MessageType::Load => self.on_load(client_id, data),
            MessageType::Unload => self.on_unload(client_id, data),
            MessageType::Method => self.on_method(client_id, data),
            MessageType::Chat => self.on_chat(client_id, data),
            MessageType::Update => self.on_update(client_id, data),
            MessageType::Event => self
                .write_resource::<MessageQueue>()
                .push((data, ClientFilter::All)),
            MessageType::Transport => {
                if let Some(transport_handle) = self.transport_handle {
                    transport_handle(
                        serde_json::from_str(&data.json)
                            .expect("Something went wrong with the transport JSON value."),
                        self,
                    );
                } else {
                    warn!("Transport calls are being called, but no transport handlers set!");
                }
            }
            _ => {
                info!("Received message of unknown type: {:?}", msg_type);
            }
        }
    }

    /// Broadcast a protobuf message to a subset or all of the clients in the world.
    pub fn broadcast(&mut self, data: Message, filter: ClientFilter) {
        self.write_resource::<MessageQueue>().push((data, filter));
    }

    /// Send a direct message to an endpoint
    pub fn send(&self, addr: &Recipient<EncodedMessage>, data: &Message) {
        addr.do_send(EncodedMessage(encode_message(data)));
    }

    /// Access to the world's config.
    pub fn config(&self) -> Fetch<WorldConfig> {
        self.read_resource::<WorldConfig>()
    }

    /// Access all clients in the ECS world.
    pub fn clients(&self) -> Fetch<Clients> {
        self.read_resource::<Clients>()
    }

    /// Access a mutable clients map in the ECS world.
    pub fn clients_mut(&mut self) -> FetchMut<Clients> {
        self.write_resource::<Clients>()
    }

    /// Access all entities metadata save-load manager.
    pub fn entities(&self) -> Fetch<Entities> {
        self.read_resource::<Entities>()
    }

    /// Access a mutable entities metadata save-load manager.
    pub fn entities_mut(&mut self) -> FetchMut<Entities> {
        self.write_resource::<Entities>()
    }

    /// Access the registry in the ECS world.
    pub fn registry(&self) -> Fetch<Registry> {
        self.read_resource::<Registry>()
    }

    /// Access chunks management in the ECS world.
    pub fn chunks(&self) -> Fetch<Chunks> {
        self.read_resource::<Chunks>()
    }

    /// Access a mutable chunk manager in the ECS world.
    pub fn chunks_mut(&mut self) -> FetchMut<Chunks> {
        self.write_resource::<Chunks>()
    }

    /// Access physics management in the ECS world.
    pub fn physics(&self) -> Fetch<Physics> {
        self.read_resource::<Physics>()
    }

    /// Access a mutable physics manager in the ECS world.
    pub fn physics_mut(&mut self) -> FetchMut<Physics> {
        self.write_resource::<Physics>()
    }

    /// Access the event queue in the ECS world.
    pub fn events(&self) -> Fetch<Events> {
        self.read_resource::<Events>()
    }

    /// Access the mutable events queue in the ECS world.
    pub fn events_mut(&mut self) -> FetchMut<Events> {
        self.write_resource::<Events>()
    }

    /// Access the search tree in the ECS world.
    pub fn search(&self) -> Fetch<Search> {
        self.read_resource::<Search>()
    }

    /// Access the mutable search tree in the ECS world.
    pub fn search_mut(&mut self) -> FetchMut<Search> {
        self.write_resource::<Search>()
    }

    /// Access the terrain of the ECS world.
    pub fn terrain(&self) -> Fetch<SeededTerrain> {
        self.read_resource::<SeededTerrain>()
    }

    /// Access a mutable terrain of the ECS world.
    pub fn terrain_mut(&mut self) -> FetchMut<SeededTerrain> {
        assert!(
            !self.started,
            "Cannot change terrain after world has started."
        );
        self.write_resource::<SeededTerrain>()
    }

    /// Access pipeline management in the ECS world.
    pub fn pipeline(&self) -> Fetch<Pipeline> {
        self.read_resource::<Pipeline>()
    }

    /// Access a mutable pipeline management in the ECS world.
    pub fn pipeline_mut(&mut self) -> FetchMut<Pipeline> {
        assert!(
            !self.started,
            "Cannot change pipeline after world has started."
        );
        self.write_resource::<Pipeline>()
    }

    /// Create a basic entity ready to be added more.
    pub fn create_entity(&mut self, id: &str, etype: &str) -> EntityBuilder {
        self.ecs_mut()
            .create_entity()
            .with(IDComp::new(id))
            .with(EntityFlag::default())
            .with(ETypeComp::new(etype))
            .with(MetadataComp::new())
            .with(CurrentChunkComp::default())
            .with(CollisionsComp::new())
    }

    /// Spawn an entity of type at a location.
    pub fn spawn_entity(&mut self, etype: &str, position: &Vec3<f32>) -> Option<Entity> {
        let loader = if let Some(loader) = self.entities_mut().get_loader(&etype.to_lowercase()) {
            loader
        } else {
            return None;
        };

        let ent = loader(nanoid!(), etype.to_owned(), MetadataComp::default(), self).build();
        set_position(self.ecs_mut(), ent, position.0, position.1, position.2);

        Some(ent)
    }

    /// Check if this world is empty.
    pub fn is_empty(&self) -> bool {
        self.read_resource::<Clients>().is_empty() && self.read_resource::<Transports>().is_empty()
    }

    /// Prepare to start.
    pub fn prepare(&mut self) {
        for (position, body) in (
            &self.ecs.read_storage::<PositionComp>(),
            &mut self.ecs.write_storage::<RigidBodyComp>(),
        )
            .join()
        {
            body.0
                .set_position(position.0 .0, position.0 .1, position.0 .2);
        }

        self.load_entities();
    }

    /// Tick of the world, run every 16ms.
    pub fn tick(&mut self) {
        if !self.started {
            self.started = true;
        }

        if self.is_empty() {
            return;
        }

        let mut dispatcher = self.dispatcher.unwrap()().build();
        dispatcher.dispatch(&self.ecs);

        self.ecs.maintain();
    }

    /// Handler for `Peer` type messages.
    fn on_peer(&mut self, client_id: &str, data: Message) {
        let client_ent = if let Some(client) = self.clients().get(client_id) {
            client.entity.to_owned()
        } else {
            return;
        };

        data.peers.into_iter().for_each(|peer| {
            let Peer {
                metadata, username, ..
            } = peer;

            {
                let mut names = self.write_component::<NameComp>();
                if let Some(n) = names.get_mut(client_ent) {
                    n.0 = username.to_owned();
                }
            }

            self.client_parser.unwrap()(&metadata, client_ent, self);

            if let Some(client) = self.clients_mut().get_mut(client_id) {
                client.username = username;
            }
        })
    }

    /// Handler for `Load` type messages.
    fn on_load(&mut self, client_id: &str, data: Message) {
        let client_ent = if let Some(client) = self.clients().get(client_id) {
            client.entity.to_owned()
        } else {
            return;
        };

        let json: OnLoadRequest =
            serde_json::from_str(&data.json).expect("`on_load` error. Could not read JSON string.");

        let chunks = json.chunks;
        if chunks.is_empty() {
            return;
        }

        let mut storage = self.write_component::<ChunkRequestsComp>();

        if let Some(requests) = storage.get_mut(client_ent) {
            chunks.into_iter().for_each(|coords| {
                requests.add(&coords);
            });
        }
    }

    /// Handler for `Unload` type messages.
    fn on_unload(&mut self, client_id: &str, data: Message) {
        let client_ent = if let Some(client) = self.clients().get(client_id) {
            client.entity.to_owned()
        } else {
            return;
        };

        let json: OnUnloadRequest = serde_json::from_str(&data.json)
            .expect("`on_unload` error. Could not read JSON string.");

        let chunks = json.chunks;
        if chunks.is_empty() {
            return;
        }

        let mut storage = self.write_component::<ChunkRequestsComp>();

        if let Some(requests) = storage.get_mut(client_ent) {
            chunks.into_iter().for_each(|coords| {
                requests.unload(&coords);
            });
        }
    }

    /// Handler for `Update` type messages.
    fn on_update(&mut self, _: &str, data: Message) {
        let chunk_size = self.config().chunk_size;
        let mut chunks = self.chunks_mut();

        data.updates.into_iter().for_each(|update| {
            let coords =
                ChunkUtils::map_voxel_to_chunk(update.vx, update.vy, update.vz, chunk_size);

            if !chunks.is_within_world(&coords) {
                return;
            }

            chunks
                .to_update
                .push_front((Vec3(update.vx, update.vy, update.vz), update.voxel));
        });
    }

    /// Handler for `Method` type messages.
    fn on_method(&mut self, _: &str, data: Message) {
        let json: OnMethodRequest = serde_json::from_str(&data.json)
            .expect("`on_method` error. Could not read JSON string.");
        let method = json.method.to_lowercase();

        if !self.method_handles.contains_key(&method) {
            warn!("`Method` type messages received, but no method handler set.");
            return;
        }

        let handle = self.method_handles.get(&method).unwrap().to_owned();

        handle(json.data, self);
    }

    /// Handler for `Chat` type messages.
    fn on_chat(&mut self, _: &str, data: Message) {
        if let Some(chat) = data.chat.clone() {
            let sender = chat.sender;
            let body = chat.body;

            info!("{}: {}", sender, body);

            if body.starts_with('/') {
                let body = body
                    .strip_prefix('/')
                    .unwrap()
                    .split_whitespace()
                    .collect::<Vec<_>>();

                // let mut msgs = vec![];
            } else {
                self.broadcast(data, ClientFilter::All);
            }
        }
    }

    /// Load existing entities.
    fn load_entities(&mut self) {
        if self.config().saving {
            // TODO: THIS FEELS HACKY

            let paths = fs::read_dir(self.entities().folder.clone()).unwrap();
            let loaders = self.entities().loaders.to_owned();

            for path in paths {
                let path = path.unwrap().path();

                if let Ok(entity_data) = File::open(&path) {
                    let id = path.file_stem().unwrap().to_str().unwrap().to_owned();
                    let mut data: HashMap<String, Value> = serde_json::from_reader(entity_data)
                        .unwrap_or_else(|_| panic!("Could not load entity file: {:?}", path));
                    let etype: String = serde_json::from_value(data.remove("etype").unwrap())
                        .unwrap_or_else(|_| {
                            panic!("EType filed does not exist on file: {:?}", path)
                        });
                    let metadata: MetadataComp =
                        serde_json::from_value(data.remove("metadata").unwrap()).unwrap_or_else(
                            |_| panic!("Metadata filed does not exist on file: {:?}", path),
                        );

                    if let Some(loader) = loaders.get(&etype) {
                        loader(id, etype, metadata, self).build();
                    } else {
                        fs::remove_file(path).expect("Unable to remove entity file...");
                    }
                }
            }
        }
    }
}