Botcraft 26.3
Loading...
Searching...
No Matches
ManagersClient.cpp
Go to the documentation of this file.
13
15#if USE_GUI
18#endif
19
20using namespace ProtocolCraft;
21
22namespace Botcraft
23{
25 {
27 is_hardcore = false;
28#if PROTOCOL_VERSION > 463 /* > 1.13.2 */
29 difficulty_locked = true;
30#endif
31
32 world = nullptr;
33 inventory_manager = nullptr;
34 entity_manager = nullptr;
35 physics_manager = nullptr;
36
37#if USE_GUI
38 rendering_manager = nullptr;
39#endif
40 auto_respawn = false;
41
42 // Ensure the assets are loaded
44 }
45
50
52 {
53 std::thread::id network_process_thread_id;
55 {
56 network_process_thread_id = network_manager->GetProcessingThreadId();
57 }
58
60 {
61 physics_manager->StopPhysics();
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
99
101 {
102 return day_time;
103 }
104
105 void ManagersClient::SetSharedWorld(const std::shared_ptr<World> world_)
106 {
107 world = world_;
108 }
109
111 {
112 return auto_respawn;
113 }
114
116 {
117 auto_respawn = b;
118 }
119
120 std::shared_ptr<World> ManagersClient::GetWorld() const
121 {
122 return world;
123 }
124
125 std::shared_ptr<EntityManager> ManagersClient::GetEntityManager() const
126 {
127 return entity_manager;
128 }
129
130 std::shared_ptr<LocalPlayer> ManagersClient::GetLocalPlayer() const
131 {
132 return entity_manager == nullptr ? nullptr : entity_manager->GetLocalPlayer();
133 }
134
135 std::shared_ptr<InventoryManager> ManagersClient::GetInventoryManager() const
136 {
137 return inventory_manager;
138 }
139
140 std::shared_ptr<PhysicsManager> ManagersClient::GetPhysicsManager() const
141 {
142 return physics_manager;
143 }
144
145 std::string ManagersClient::GetPlayerName(const UUID& uuid) const
146 {
147 std::shared_lock<std::shared_mutex> lock(player_names_mutex);
148 auto it = player_names.find(uuid);
149 if (it != player_names.end())
150 {
151 return it->second;
152 }
153 else if (entity_manager == nullptr || entity_manager->GetLocalPlayer() == nullptr)
154 {
155 return "";
156 }
157 else if (uuid == entity_manager->GetLocalPlayer()->GetUUID())
158 {
159 return network_manager->GetMyName();
160 }
161 return "";
162 }
163
164
165#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
166 void ManagersClient::Handle(ClientboundGameProfilePacket& packet)
167#else
169#endif
170 {
171 // Create all handlers
172 if (!world)
173 {
174 world = std::make_shared<World>(false);
175 }
176
177 inventory_manager = std::make_shared<InventoryManager>(network_manager);
178 entity_manager = std::make_shared<EntityManager>(network_manager);
179 // Subscribe them to the network manager
180 network_manager->AddHandler(world.get());
181 network_manager->AddHandler(inventory_manager.get());
182 network_manager->AddHandler(entity_manager.get());
183#if USE_GUI
185 {
186 rendering_manager = Renderer::RenderingManager::CreateRenderingManagerIfPossible(world, inventory_manager, entity_manager);
187 if (rendering_manager == nullptr)
188 {
189 static bool first_time = true;
190 if (first_time)
191 {
192 first_time = false;
194 "Having multiple clients with renderer enabled is not supported.\n" <<
195 "Make sure to destroy the rendered client or disable Botcraft::Renderer::Options before creating another one"
196 );
197 }
198 }
199 else
200 {
201 network_manager->AddHandler(rendering_manager.get());
202 }
203 }
204#endif
205 physics_manager = std::make_shared<PhysicsManager>(inventory_manager, entity_manager, network_manager, world);
206 network_manager->AddHandler(physics_manager.get());
207 // Start physics
208 physics_manager->StartPhysics();
209 }
210
212 {
213 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
214#if PROTOCOL_VERSION > 463 /* > 1.13.2 */
215 difficulty_locked = packet.GetLocked();
216#endif
217 }
218
220 {
221#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
223#endif
224
225#if PROTOCOL_VERSION > 737 /* > 1.16.1 */
226 is_hardcore = packet.GetHardcore();
227#else
228 is_hardcore = packet.GetGameType() & 0x08;
229#endif
230
231#if PROTOCOL_VERSION < 464 /* < 1.14 */
232 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
233#endif
234 }
235
237 {
238 if (packet.GetHealth() <= 0.0f && auto_respawn)
239 {
240 Respawn();
241 }
242 }
243
245 {
246 // Override the ConnectionClient Handle as the teleport confirmation is sent by the physics manager instead
247 }
248
250 {
251#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
253#endif
254
255#if PROTOCOL_VERSION < 464 /* < 1.14 */
256 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
257#endif
258 }
259
261 {
262 // abs because the server multiplies by -1 to indicate fixed daytime for versions < 1.21.2
263#if PROTOCOL_VERSION < 775 /* 26.1 */
264 day_time = std::abs(packet.GetDayTime()) % 24000;
265#else
266 // Per dimension updates
267 // TODO: match the dimension to ids using registry?
268 if (world != nullptr)
269 {
270 if (world->GetCurrentDimension() == "minecraft:overworld")
271 {
272 auto it = packet.GetClockUpdates().find(0);
273 if (it != packet.GetClockUpdates().end())
274 {
275 day_time = it->second.GetTotalTicks() % 24000;
276 }
277 }
278 else if (world->GetCurrentDimension() == "minecraft:the_end")
279 {
280 auto it = packet.GetClockUpdates().find(1);
281 if (it != packet.GetClockUpdates().end())
282 {
283 day_time = it->second.GetTotalTicks() % 24000;
284 }
285 }
286 }
287#endif
288 }
289
290#if PROTOCOL_VERSION < 761 /* < 1.19.3 */
291 void ManagersClient::Handle(ClientboundPlayerInfoPacket& packet)
292 {
293 if (packet.GetAction() == PlayerInfoAction::RemovePlayer)
294 {
295 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
296 for (const auto& [uuid, infos] : packet.GetEntries())
297 {
298 player_names.erase(uuid);
299 }
300 }
301 else if (packet.GetAction() == PlayerInfoAction::AddPlayer)
302 {
303 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
304 for (const auto& [uuid, infos] : packet.GetEntries())
305 {
306 if (infos.GetDisplayName().has_value())
307 {
308 player_names[uuid] = infos.GetDisplayName().value().GetText();
309 }
310 else
311 {
312 player_names[uuid] = infos.GetName();
313 }
314 }
315 }
316 else if (packet.GetAction() == PlayerInfoAction::UpdateDisplayName)
317 {
318 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
319 for (const auto& [uuid, infos] : packet.GetEntries())
320 {
321 if (infos.GetDisplayName().has_value())
322 {
323 player_names[uuid] = infos.GetDisplayName().value().GetText();
324 }
325 }
326 }
327 }
328#else
330 {
331 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
332 for (const auto& uuid : packet.GetProfileIds())
333 {
334 player_names.erase(uuid);
335 }
336 }
337
339 {
340 for (const auto& action : packet.GetActions())
341 {
342 if (action == PlayerInfoUpdateAction::AddPlayer)
343 {
344 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
345 for (const auto& [uuid, infos] : packet.GetEntries())
346 {
347 player_names[uuid] = infos.game_profile.GetName();
348 }
349 continue;
350 }
351 if (action == PlayerInfoUpdateAction::UpdateDisplayName)
352 {
353 std::scoped_lock<std::shared_mutex> lock(player_names_mutex);
354 for (const auto& [uuid, infos] : packet.GetEntries())
355 {
356 if (infos.display_name.has_value())
357 {
358 player_names[uuid] = infos.display_name.value().GetText();
359 }
360 }
361 }
362 }
363 }
364#endif
365} //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
Utilities::ScopeLockedWrapper< const std::map< ProtocolCraft::UUID, std::string >, std::shared_mutex, std::shared_lock > GetOnlinePlayers() const
Get the list of connected player (tab list)
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
std::shared_ptr< PhysicsManager > physics_manager
std::shared_ptr< InventoryManager > inventory_manager
std::shared_ptr< EntityManager > entity_manager
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 std::shared_ptr< RenderingManager > CreateRenderingManagerIfPossible(std::shared_ptr< World > world_, std::shared_ptr< InventoryManager > inventory_manager_, std::shared_ptr< EntityManager > entity_manager_)
Mutex protected reference, will be locked until destroyed.
std::array< unsigned char, 16 > UUID