Botcraft 1.21.11
Loading...
Searching...
No Matches
ManagersClient.cpp
Go to the documentation of this file.
13
15#if USE_GUI
17#endif
18
19using namespace ProtocolCraft;
20
21namespace Botcraft
22{
23 ManagersClient::ManagersClient(const bool use_renderer_)
24 {
26 is_hardcore = false;
27#if PROTOCOL_VERSION > 463 /* > 1.13.2 */
28 difficulty_locked = true;
29#endif
30
31 world = nullptr;
32 inventory_manager = nullptr;
33 entity_manager = nullptr;
34 physics_manager = nullptr;
35
36#if USE_GUI
37 use_renderer = use_renderer_;
38 rendering_manager = nullptr;
39#else
40 if (use_renderer_)
41 {
42 LOG_WARNING("Your version of botcraft hasn't been compiled with GUI enabled, setting use_renderer_ to false");
43 }
44#endif
45 auto_respawn = false;
46
47 // Ensure the assets are loaded
49 }
50
55
57 {
58 std::thread::id network_process_thread_id;
60 {
61 network_process_thread_id = network_manager->GetProcessingThreadId();
62 }
63
65
67#if PROTOCOL_VERSION > 463 /* > 1.13.2 */
68 difficulty_locked = true;
69#endif
70 is_hardcore = false;
71
72 physics_manager.reset();
73 entity_manager.reset();
74 inventory_manager.reset();
75#if USE_GUI
77 {
78 rendering_manager->Close();
79 }
80 rendering_manager.reset();
81#endif
82
83 if (world)
84 {
85 world->UnloadAllChunks(network_process_thread_id);
86 world.reset();
87 }
88
89 {
90 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
91 player_names.clear();
92 }
93 }
94
96 {
97 return day_time;
98 }
99
100 int ManagersClient::SendInventoryTransaction(const std::shared_ptr<ServerboundContainerClickPacket>& transaction)
101 {
102 InventoryTransaction inventory_transaction = inventory_manager->PrepareTransaction(transaction);
103#if PROTOCOL_VERSION < 755 /* < 1.17 */
104 inventory_manager->AddPendingTransaction(inventory_transaction);
105 network_manager->Send(transaction);
106 return transaction->GetUid();
107#else
108 network_manager->Send(transaction);
109 // In 1.17+ there is no server confirmation so apply it directly
110 inventory_manager->ApplyTransaction(inventory_transaction);
111 return 1;
112#endif
113 }
114
115 void ManagersClient::SetSharedWorld(const std::shared_ptr<World> world_)
116 {
117 world = world_;
118 }
119
121 {
122 return auto_respawn;
123 }
124
126 {
127 auto_respawn = b;
128 }
129
130 std::shared_ptr<World> ManagersClient::GetWorld() const
131 {
132 return world;
133 }
134
135 std::shared_ptr<EntityManager> ManagersClient::GetEntityManager() const
136 {
137 return entity_manager;
138 }
139
140 std::shared_ptr<LocalPlayer> ManagersClient::GetLocalPlayer() const
141 {
142 return entity_manager == nullptr ? nullptr : entity_manager->GetLocalPlayer();
143 }
144
145 std::shared_ptr<InventoryManager> ManagersClient::GetInventoryManager() const
146 {
147 return inventory_manager;
148 }
149
150 std::shared_ptr<PhysicsManager> ManagersClient::GetPhysicsManager() const
151 {
152 return physics_manager;
153 }
154
155 std::string ManagersClient::GetPlayerName(const UUID& uuid) const
156 {
157 std::shared_lock<std::shared_mutex> lock(player_names_mutex);
158 auto it = player_names.find(uuid);
159 if (it != player_names.end())
160 {
161 return it->second;
162 }
163 else if (entity_manager == nullptr || entity_manager->GetLocalPlayer() == nullptr)
164 {
165 return "";
166 }
167 else if (uuid == entity_manager->GetLocalPlayer()->GetUUID())
168 {
169 return network_manager->GetMyName();
170 }
171 return "";
172 }
173
174
175#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
176 void ManagersClient::Handle(ClientboundGameProfilePacket& packet)
177#else
179#endif
180 {
181 // Create all handlers
182 if (!world)
183 {
184 world = std::make_shared<World>(false);
185 }
186
187 inventory_manager = std::make_shared<InventoryManager>();
188 entity_manager = std::make_shared<EntityManager>(network_manager);
189 // Subscribe them to the network manager
190 network_manager->AddHandler(world.get());
191 network_manager->AddHandler(inventory_manager.get());
192 network_manager->AddHandler(entity_manager.get());
193#if USE_GUI
194 if (use_renderer)
195 {
196 rendering_manager = std::make_shared<Renderer::RenderingManager>(world, inventory_manager, entity_manager, 800, 600, CHUNK_WIDTH, false);
197 network_manager->AddHandler(rendering_manager.get());
198 }
199 physics_manager = std::make_shared<PhysicsManager>(rendering_manager, inventory_manager, entity_manager, network_manager, world);
200#else
201 physics_manager = std::make_shared<PhysicsManager>(inventory_manager, entity_manager, network_manager, world);
202#endif
203 network_manager->AddHandler(physics_manager.get());
204 // Start physics
205 physics_manager->StartPhysics();
206 }
207
209 {
210 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
211#if PROTOCOL_VERSION > 463 /* > 1.13.2 */
212 difficulty_locked = packet.GetLocked();
213#endif
214 }
215
217 {
218#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
220#endif
221
222#if PROTOCOL_VERSION > 737 /* > 1.16.1 */
223 is_hardcore = packet.GetHardcore();
224#else
225 is_hardcore = packet.GetGameType() & 0x08;
226#endif
227
228#if PROTOCOL_VERSION < 464 /* < 1.14 */
229 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
230#endif
231 }
232
234 {
235 if (packet.GetHealth() <= 0.0f && auto_respawn)
236 {
237 Respawn();
238 }
239 }
240
242 {
243 // Override the ConnectionClient Handle as the teleport confirmation is sent by the physics manager instead
244 }
245
247 {
248#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
250#endif
251
252#if PROTOCOL_VERSION < 464 /* < 1.14 */
253 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
254#endif
255 }
256
258 {
259 // abs because the server multiplies by -1 to indicate fixed daytime for versions < 1.21.2
260 day_time = std::abs(packet.GetDayTime()) % 24000;
261 }
262
263#if PROTOCOL_VERSION < 761 /* < 1.19.3 */
264 void ManagersClient::Handle(ClientboundPlayerInfoPacket& packet)
265 {
266 if (packet.GetAction() == PlayerInfoAction::RemovePlayer)
267 {
268 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
269 for (const auto& [uuid, infos] : packet.GetEntries())
270 {
271 player_names.erase(uuid);
272 }
273 }
274 else if (packet.GetAction() == PlayerInfoAction::AddPlayer)
275 {
276 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
277 for (const auto& [uuid, infos] : packet.GetEntries())
278 {
279 if (infos.GetDisplayName().has_value())
280 {
281 player_names[uuid] = infos.GetDisplayName().value().GetText();
282 }
283 else
284 {
285 player_names[uuid] = infos.GetName();
286 }
287 }
288 }
289 else if (packet.GetAction() == PlayerInfoAction::UpdateDisplayName)
290 {
291 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
292 for (const auto& [uuid, infos] : packet.GetEntries())
293 {
294 if (infos.GetDisplayName().has_value())
295 {
296 player_names[uuid] = infos.GetDisplayName().value().GetText();
297 }
298 }
299 }
300 }
301#else
303 {
304 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
305 for (const auto& uuid : packet.GetProfileIds())
306 {
307 player_names.erase(uuid);
308 }
309 }
310
312 {
313 for (const auto& action : packet.GetActions())
314 {
315 if (action == PlayerInfoUpdateAction::AddPlayer)
316 {
317 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
318 for (const auto& [uuid, infos] : packet.GetEntries())
319 {
320 player_names[uuid] = infos.game_profile.GetName();
321 }
322 continue;
323 }
324 if (action == PlayerInfoUpdateAction::UpdateDisplayName)
325 {
326 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
327 for (const auto& [uuid, infos] : packet.GetEntries())
328 {
329 if (infos.display_name.has_value())
330 {
331 player_names[uuid] = infos.display_name.value().GetText();
332 }
333 }
334 }
335 }
336 }
337#endif
338} //Botcraft
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
static AssetsManager & getInstance()
void Respawn()
Ask to respawn when dead.
virtual void Handle(ProtocolCraft::ClientboundLoginDisconnectPacket &packet) override
std::shared_ptr< NetworkManager > network_manager
std::atomic< int > day_time
std::map< ProtocolCraft::UUID, std::string > player_names
Names of all connected players.
std::shared_ptr< EntityManager > GetEntityManager() const
std::shared_ptr< PhysicsManager > GetPhysicsManager() const
void SetSharedWorld(const std::shared_ptr< World > world_)
std::shared_ptr< Renderer::RenderingManager > rendering_manager
std::shared_ptr< LocalPlayer > GetLocalPlayer() const
void SetAutoRespawn(const bool b)
std::shared_ptr< InventoryManager > GetInventoryManager() const
virtual void Disconnect() override
ManagersClient(const bool use_renderer_)
std::shared_ptr< PhysicsManager > physics_manager
std::shared_ptr< InventoryManager > inventory_manager
std::shared_ptr< EntityManager > entity_manager
int SendInventoryTransaction(const std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > &transaction)
std::shared_mutex player_names_mutex
std::shared_ptr< World > world
virtual void Handle(ProtocolCraft::ClientboundLoginFinishedPacket &packet) override
int GetDayTime() const
Get the current tick.
std::string GetPlayerName(const ProtocolCraft::UUID &uuid) const
Get the name of a connected player.
std::shared_ptr< World > GetWorld() const
static constexpr int CHUNK_WIDTH
Definition Chunk.hpp:21
std::array< unsigned char, 16 > UUID