Botcraft 1.21.5
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 std::shared_ptr<ServerboundClientInformationPacket> settings_packet = std::make_shared<ServerboundClientInformationPacket>();
244#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
245 settings_packet->SetLanguage("fr_FR");
246 settings_packet->SetViewDistance(10);
247 settings_packet->SetChatVisibility(static_cast<int>(ChatMode::Enabled));
248 settings_packet->SetChatColors(true);
249 settings_packet->SetModelCustomisation(0xFF);
250 settings_packet->SetMainHand(1); // 1 is right handed, 0 is left handed
251#else
253 info.SetLanguage("fr_FR");
254 info.SetViewDistance(10);
255 info.SetChatVisibility(static_cast<int>(ChatMode::Enabled));
256 info.SetChatColors(true);
257 info.SetModelCustomisation(0xFF);
258 info.SetMainHand(1); // 1 is right handed, 0 is left handed
259#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
260 info.SetParticleStatus(2); // 0 is "all", 1 is "decreased" and 2 is "minimal"
261#endif
262 settings_packet->SetClientInformation(info);
263#endif
264
265 network_manager->Send(settings_packet);
266 }
267
269 {
270 // Override the ConnectionClient Handle as the teleport confirmation is sent by the physics manager instead
271 }
272
274 {
275#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
277#endif
278
279#if PROTOCOL_VERSION < 464 /* < 1.14 */
280 difficulty = static_cast<Difficulty>(packet.GetDifficulty());
281#endif
282 }
283
285 {
286 // abs because the server multiplies by -1 to indicate fixed daytime for versions < 1.21.2
287 day_time = std::abs(packet.GetDayTime()) % 24000;
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
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