Botcraft 1.21.4
Loading...
Searching...
No Matches
EntityManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <shared_mutex>
5#include <unordered_map>
6
8
10
11namespace Botcraft
12{
13 class Entity;
14 class LocalPlayer;
15 class NetworkManager;
16
18 {
19 public:
20 EntityManager(const std::shared_ptr<NetworkManager>& network_manager);
21
22 std::shared_ptr<LocalPlayer> GetLocalPlayer();
23
24 std::shared_ptr<Entity> GetEntity(const int id) const;
25 void AddEntity(const std::shared_ptr<Entity>& entity);
26
27 /// @brief Get a read-only locked version of all the loaded entities (including local player)
28 /// @return Basically an object you can use as a std::unordered_map<int, std::shared_ptr<Entity>>*.
29 /// **ALL ENTITIES UPDATE WILL BE BLOCKED WHILE THIS OBJECT IS ALIVE**, make sure it goes out of scope
30 /// as soon as you don't need it.
32
33 protected:
34 virtual void Handle(ProtocolCraft::ClientboundLoginPacket& msg) override;
35 virtual void Handle(ProtocolCraft::ClientboundAddEntityPacket& msg) override;
36#if PROTOCOL_VERSION < 759 /* < 1.19 */
37 virtual void Handle(ProtocolCraft::ClientboundAddMobPacket& msg) override;
38#endif
40#if PROTOCOL_VERSION < 721 /* < 1.16 */
41 virtual void Handle(ProtocolCraft::ClientboundAddGlobalEntityPacket& msg) override;
42#endif
43#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
44 virtual void Handle(ProtocolCraft::ClientboundAddPlayerPacket& msg) override;
45#endif
46 virtual void Handle(ProtocolCraft::ClientboundSetHealthPacket& msg) override;
49#if PROTOCOL_VERSION < 755 /* < 1.17 */
50 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacket& msg) override;
51#endif
52 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketPos& msg) override;
54 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketRot& msg) override;
55#if PROTOCOL_VERSION == 755 /* 1.17 */
56 virtual void Handle(ProtocolCraft::ClientboundRemoveEntityPacket& msg) override;
57#else
59#endif
60 virtual void Handle(ProtocolCraft::ClientboundRespawnPacket& msg) override;
61 virtual void Handle(ProtocolCraft::ClientboundGameEventPacket& msg) override;
62 virtual void Handle(ProtocolCraft::ClientboundSetEntityDataPacket& msg) override;
64 virtual void Handle(ProtocolCraft::ClientboundSetEquipmentPacket& msg) override;
68#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
70 virtual void Handle(ProtocolCraft::ClientboundMoveMinecartPacket& msg) override;
71#endif
72
73
74 private:
75 std::unordered_map<int, std::shared_ptr<Entity> > entities;
76 // The current player is stored independently
77 std::shared_ptr<LocalPlayer> local_player;
78
79 mutable std::shared_mutex entity_manager_mutex;
80
81 std::shared_ptr<NetworkManager> network_manager;
82 };
83} // Botcraft
std::shared_ptr< LocalPlayer > GetLocalPlayer()
std::shared_ptr< NetworkManager > network_manager
std::shared_ptr< LocalPlayer > local_player
std::shared_mutex entity_manager_mutex
Utilities::ScopeLockedWrapper< const std::unordered_map< int, std::shared_ptr< Entity > >, std::shared_mutex, std::shared_lock > GetEntities() const
Get a read-only locked version of all the loaded entities (including local player)
std::unordered_map< int, std::shared_ptr< Entity > > entities
void AddEntity(const std::shared_ptr< Entity > &entity)
virtual void Handle(ProtocolCraft::ClientboundLoginPacket &msg) override
std::shared_ptr< Entity > GetEntity(const int id) const
Mutex protected reference, will be locked until destroyed.