Botcraft 26.3
Loading...
Searching...
No Matches
EntityManager.cpp
Go to the documentation of this file.
6
8
12
13#if PROTOCOL_VERSION > 776 /* > 26.2 */
14#include <stdexcept>
15#endif
16
17namespace Botcraft
18{
19 EntityManager::EntityManager(const std::shared_ptr<NetworkManager>& network_manager) : network_manager(network_manager)
20 {
21 local_player = nullptr;
22 }
23
24 std::shared_ptr<LocalPlayer> EntityManager::GetLocalPlayer()
25 {
26 std::shared_lock<std::shared_mutex> lock(entity_manager_mutex);
27 return local_player;
28 }
29
30 std::shared_ptr<Entity> EntityManager::GetEntity(const int id) const
31 {
32 std::shared_lock<std::shared_mutex> lock(entity_manager_mutex);
33 auto it = entities.find(id);
34 return it == entities.end() ? nullptr : it->second;
35 }
36
37 void EntityManager::AddEntity(const std::shared_ptr<Entity>& entity)
38 {
39 if (entity == nullptr)
40 {
41 return;
42 }
43
44 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
45 entities[entity->GetEntityID()] = entity;
46 }
47
52
53
55 {
56 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
57 local_player = std::make_shared<LocalPlayer>();
58 local_player->SetEntityID(packet.GetPlayerId());
59#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
60 local_player->SetGameMode(static_cast<GameType>(packet.GetGameType() & 0x03));
61#else
62 local_player->SetGameMode(static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
63#endif
64 entities[packet.GetPlayerId()] = local_player;
65 }
66
67#if PROTOCOL_VERSION < 755 /* < 1.17 */
68 void EntityManager::Handle(ProtocolCraft::ClientboundMoveEntityPacket& packet)
69 {
70 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
71 auto it = entities.find(packet.GetEntityId());
72 if (it == entities.end())
73 {
74 std::shared_ptr<Entity> entity = std::make_shared<UnknownEntity>();
75 entity->SetEntityID(packet.GetEntityId());
76 entities[packet.GetEntityId()] = entity;
77 }
78 }
79#endif
80
82 {
83 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
84
85 if (entity != nullptr)
86 {
87 Vector3<double> entity_position = entity->GetPosition();
88#if PROTOCOL_VERSION < 777 /* < 26.3 */
89 entity_position = Vector3<double>(
90 (packet.GetXA() / 128.0f + entity_position.x * 32.0f) / 32.0f,
91 (packet.GetYA() / 128.0f + entity_position.y * 32.0f) / 32.0f,
92 (packet.GetZA() / 128.0f + entity_position.z * 32.0f) / 32.0f
93 );
94#else
95 if (packet.GetSingleDelta().has_value())
96 {
97 const std::array<short, 3>& delta = packet.GetSingleDelta().value();
98 for (int i = 0; i < 3; ++i)
99 {
100 if (delta[i] == 0)
101 {
102 continue;
103 }
104 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
105 }
106 }
107 else if (packet.GetSteppedDeltas().has_value())
108 {
109 const std::vector<std::pair<int, std::array<short, 3>>>& steps = packet.GetSteppedDeltas().value();
110 for (auto& [tick, delta] : steps)
111 {
112 for (int i = 0; i < 3; ++i)
113 {
114 if (delta[i] == 0)
115 {
116 continue;
117 }
118 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
119 }
120 }
121 }
122 else
123 {
124 throw std::runtime_error("Invalid ClientboundMoveEntityPacket");
125 }
126#endif
127 entity->SetPosition(entity_position);
128 entity->SetOnGround(packet.GetOnGround());
129 }
130 }
131
133 {
134 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
135
136 if (entity != nullptr)
137 {
138 Vector3<double> entity_position = entity->GetPosition();
139#if PROTOCOL_VERSION < 777 /* < 26.3 */
140 entity_position = Vector3<double>(
141 (packet.GetXA() / 128.0f + entity_position.x * 32.0f) / 32.0f,
142 (packet.GetYA() / 128.0f + entity_position.y * 32.0f) / 32.0f,
143 (packet.GetZA() / 128.0f + entity_position.z * 32.0f) / 32.0f
144 );
145#else
146 if (packet.GetSingleDelta().has_value())
147 {
148 const std::array<short, 3>& delta = packet.GetSingleDelta().value();
149 for (int i = 0; i < 3; ++i)
150 {
151 if (delta[i] == 0)
152 {
153 continue;
154 }
155 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
156 }
157 }
158 else if (packet.GetSteppedDeltas().has_value())
159 {
160 const std::vector<std::pair<int, std::array<short, 3>>>& steps = packet.GetSteppedDeltas().value();
161 for (auto& [tick, delta] : steps)
162 {
163 for (int i = 0; i < 3; ++i)
164 {
165 if (delta[i] == 0)
166 {
167 continue;
168 }
169 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
170 }
171 }
172 }
173 else
174 {
175 throw std::runtime_error("Invalid ClientboundMoveEntityPacketPosRot");
176 }
177#endif
178 entity->SetPosition(entity_position);
179 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
180 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
181 entity->SetOnGround(packet.GetOnGround());
182 }
183 }
184
186 {
187 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
188
189 if (entity != nullptr)
190 {
191 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
192 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
193 entity->SetOnGround(packet.GetOnGround());
194 }
195 }
196
198 {
199#if PROTOCOL_VERSION < 458 /* < 1.14 */
200 std::shared_ptr<Entity> entity = Entity::CreateObjectEntity(static_cast<ObjectEntityType>(packet.GetType()));
201#else
202 std::shared_ptr<Entity> entity = Entity::CreateEntity(static_cast<EntityType>(packet.GetType()));
203#endif
204
205 entity->SetEntityID(packet.GetEntityId());
206 entity->SetX(packet.GetX());
207 entity->SetY(packet.GetY());
208 entity->SetZ(packet.GetZ());
209 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
210 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
211 entity->SetUUID(packet.GetUuid());
212#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
213 // Packet data is in 1/8000 of block per tick, so convert it back to block/tick
214 entity->SetSpeed(Vector3<double>(packet.GetXa(), packet.GetYa(), packet.GetZa()) / 8000.0);
215#else
216 entity->SetSpeed(Vector3<double>(packet.GetMovement()));
217#endif
218
219 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
220 entities[packet.GetEntityId()] = entity;
221 }
222
224 {
225 std::shared_ptr<Entity> entity = GetLocalPlayer();
226
227 if (entity == nullptr)
228 {
229 LOG_WARNING("Trying to apply knockback without a valid player");
230 }
231 else
232 {
233#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
234 entity->AddSpeed(Vector3<double>(
235 static_cast<double>(packet.GetKnockbackX()),
236 static_cast<double>(packet.GetKnockbackY()),
237 static_cast<double>(packet.GetKnockbackZ())
238 ));
239#else
240 if (packet.GetPlayerKnockback().has_value())
241 {
242 entity->AddSpeed(packet.GetPlayerKnockback().value());
243 }
244#endif
245 }
246 }
247
248#if PROTOCOL_VERSION < 759 /* < 1.19 */
249 void EntityManager::Handle(ProtocolCraft::ClientboundAddMobPacket& packet)
250 {
251 std::shared_ptr<Entity> entity = Entity::CreateEntity(static_cast<EntityType>(packet.GetType()));
252
253 entity->SetEntityID(packet.GetEntityId());
254 entity->SetX(packet.GetX());
255 entity->SetY(packet.GetY());
256 entity->SetZ(packet.GetZ());
257 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
258 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
259 entity->SetUUID(packet.GetUuid());
260
261 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
262 entities[packet.GetEntityId()] = entity;
263 }
264#endif
265
266#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
267 void EntityManager::Handle(ProtocolCraft::ClientboundAddExperienceOrbPacket& packet)
268 {
269 std::shared_ptr<Entity> entity = Entity::CreateEntity(EntityType::ExperienceOrb);
270
271 entity->SetEntityID(packet.GetEntityId());
272 entity->SetX(packet.GetX());
273 entity->SetY(packet.GetY());
274 entity->SetZ(packet.GetZ());
275 // What do we do with the xp value?
276 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
277 entities[packet.GetEntityId()] = entity;
278 }
279#endif
280
281#if PROTOCOL_VERSION < 721 /* < 1.16 */
282 void EntityManager::Handle(ProtocolCraft::ClientboundAddGlobalEntityPacket& packet)
283 {
284 std::shared_ptr<Entity> entity = Entity::CreateEntity(static_cast<EntityType>(packet.GetType()));
285
286 entity->SetEntityID(packet.GetEntityId());
287 entity->SetX(packet.GetX());
288 entity->SetY(packet.GetY());
289 entity->SetZ(packet.GetZ());
290
291 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
292 entities[packet.GetEntityId()] = entity;
293 }
294#endif
295
296#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
297 void EntityManager::Handle(ProtocolCraft::ClientboundAddPlayerPacket& packet)
298 {
299 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
300
301 if (entity == nullptr)
302 {
304 entities[packet.GetEntityId()] = entity;
305 }
306
307 entity->SetEntityID(packet.GetEntityId());
308 entity->SetPosition(Vector3<double>(
309 packet.GetX(),
310 packet.GetY(),
311 packet.GetZ()
312 ));
313 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
314 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
315 entity->SetUUID(packet.GetPlayerId());
316 }
317#endif
318
320 {
321 std::shared_ptr<LocalPlayer> player = GetLocalPlayer();
322 player->SetHealth(packet.GetHealth());
323 player->SetFood(packet.GetFood());
324 player->SetFoodSaturation(packet.GetFoodSaturation());
325 }
326
328 {
329 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
330
331 if (entity != nullptr)
332 {
333#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
334 entity->SetPosition(Vector3<double>(
335 packet.GetX(),
336 packet.GetY(),
337 packet.GetZ()
338 ));
339 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
340 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
341#else
342 entity->SetPosition(Vector3<double>(
343 packet.GetRelatives() & (1 << 0) ? entity->GetX() + packet.GetChange().GetPosition()[0] : packet.GetChange().GetPosition()[0],
344 packet.GetRelatives() & (1 << 1) ? entity->GetY() + packet.GetChange().GetPosition()[1] : packet.GetChange().GetPosition()[1],
345 packet.GetRelatives() & (1 << 2) ? entity->GetZ() + packet.GetChange().GetPosition()[2] : packet.GetChange().GetPosition()[2]
346 ));
347 entity->SetYaw(packet.GetRelatives() & (1 << 3) ? entity->GetYaw() + packet.GetChange().GetYRot() : packet.GetChange().GetYRot());
348 entity->SetPitch(packet.GetRelatives() & (1 << 4) ? entity->GetPitch() + packet.GetChange().GetXRot() : packet.GetChange().GetXRot());
349#endif
350 entity->SetOnGround(packet.GetOnGround());
351 }
352 }
353
355 {
356 std::shared_ptr<LocalPlayer> player = GetLocalPlayer();
357 player->SetAbilitiesFlags(packet.GetFlags());
358 player->SetFlyingSpeed(packet.GetFlyingSpeed());
359 player->SetWalkingSpeed(packet.GetWalkingSpeed());
360 }
361
362#if PROTOCOL_VERSION == 755 /* 1.17 */
363 void EntityManager::Handle(ProtocolCraft::ClientboundRemoveEntityPacket& packet)
364 {
365 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
366 entities.erase(packet.GetEntityId());
367 }
368#else
370 {
371 std::scoped_lock<std::shared_mutex> lock(entity_manager_mutex);
372 for (int i = 0; i < packet.GetEntityIds().size(); ++i)
373 {
374 entities.erase(packet.GetEntityIds()[i]);
375 }
376 }
377#endif
378
380 {
381 std::shared_ptr<LocalPlayer> player = GetLocalPlayer();
382#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
383 player->SetGameMode(static_cast<GameType>(packet.GetPlayerGameType()));
384#else
385 player->SetGameMode(static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
386#endif
387 }
388
390 {
391 switch (packet.GetType())
392 {
393 case 3: // CHANGE_GAME_MODE
394 GetLocalPlayer()->SetGameMode(static_cast<GameType>(packet.GetParam()));
395 break;
396 default:
397 break;
398 }
399 }
400
402 {
403 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
404
405 if (entity == nullptr)
406 {
407 LOG_WARNING("Trying to load metadata in unexisting entity");
408 }
409 else
410 {
411 entity->LoadMetadataFromRawArray(packet.GetPackedItems());
412 }
413 }
414
416 {
417 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
418
419 if (entity == nullptr)
420 {
421 LOG_WARNING("Trying to set speed of an unexisting entity");
422 }
423 else
424 {
425#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
426 // Packet data is in 1/8000 of block per tick, so convert it back to block/tick
427 entity->SetSpeed(Vector3<double>(packet.GetXA(), packet.GetYA(), packet.GetZA()) / 8000.0);
428#else
429 entity->SetSpeed(Vector3<double>(packet.GetMovement()));
430#endif
431 }
432 }
433
435 {
436 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
437
438 if (entity == nullptr)
439 {
440 LOG_WARNING("Trying to set equipment of an unexisting entity");
441 }
442 else
443 {
444#if PROTOCOL_VERSION > 730 /* > 1.15.2 */
445 for (auto& p : packet.GetSlots())
446 {
447 entity->SetEquipment(static_cast<EquipmentSlot>(p.first), p.second);
448 }
449#else
450 entity->SetEquipment(static_cast<EquipmentSlot>(packet.GetSlot().first), packet.GetSlot().second);
451#endif
452 }
453 }
454
456 {
457 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
458
459 if (entity == nullptr)
460 {
461 LOG_WARNING("Trying to set attributes of an unexisting entity");
462 }
463 else if (!entity->IsLivingEntity())
464 {
465 LOG_WARNING("Trying to set attributes of a non LivingEntity");
466 }
467 else
468 {
469 std::shared_ptr<LivingEntity> living_entity = std::dynamic_pointer_cast<LivingEntity>(entity);
470 for (const auto& a : packet.GetAttributes())
471 {
472#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
473 const EntityAttribute::Type type = static_cast<EntityAttribute::Type>(a.GetKey());
474#elif PROTOCOL_VERSION > 709 /* > 1.15.2 */
475 const EntityAttribute::Type type = EntityAttribute::StringToType(a.GetKey().GetFull());
476#else
477 const EntityAttribute::Type type = EntityAttribute::StringToType(a.GetKey());
478#endif
479 EntityAttribute attribute(type, a.GetValue());
480 for (const auto& m : a.GetModifiers())
481 {
482 attribute.SetModifier(
483#if PROTOCOL_VERSION < 767 /* < 1.21 */
484 m.GetUuid(),
485#else
486 m.GetId().GetFull(),
487#endif
488 EntityAttribute::Modifier{ m.GetAmount(), static_cast<EntityAttribute::Modifier::Operation>(m.GetOperation()) }
489 );
490 }
491 living_entity->AddAttribute(attribute);
492 }
493 }
494 }
495
497 {
498 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
499
500 if (entity == nullptr)
501 {
502 LOG_WARNING("Trying to set effect of an unexisting entity");
503 }
504 else
505 {
506 entity->AddEffect(EntityEffect {
507 static_cast<EntityEffectType>(packet.GetEffectId()), // type
508 static_cast<unsigned char>(packet.GetEffectAmplifier()), //amplifier
509 std::chrono::steady_clock::now() + std::chrono::milliseconds(50 * packet.GetEffectDurationTicks()) // end
510 });
511 }
512 }
513
515 {
516 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
517
518 if (entity == nullptr)
519 {
520 LOG_WARNING("Trying to remove effect of an unexisting entity");
521 }
522 else
523 {
524 entity->RemoveEffect(static_cast<EntityEffectType>(packet.GetEffect()));
525 }
526 }
527
528#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
530 {
531 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
532
533 if (entity != nullptr)
534 {
535 std::shared_ptr<LocalPlayer> player = GetLocalPlayer();
536#if PROTOCOL_VERSION < 777 /* < 26.3 */
537 entity->SetPosition(packet.GetValues().GetPosition());
538 if (entity == player)
539 {
540 return;
541 }
542 entity->SetYaw(packet.GetValues().GetYRot());
543 entity->SetPitch(packet.GetValues().GetXRot());
544#else
545 switch (packet.GetPosition().GetType())
546 {
548 entity->SetPosition(std::static_pointer_cast<ProtocolCraft::PositionPathLinear>(packet.GetPosition().GetData())->GetEndPosition());
549 break;
551 entity->SetPosition(std::static_pointer_cast<ProtocolCraft::PositionPathStepped>(packet.GetPosition().GetData())->GetSteps().back().GetPosition());
552 break;
553 default:
554 throw std::runtime_error("Unknown PositionPathDataType: " + std::to_string(static_cast<int>(packet.GetPosition().GetType())));
555 break;
556 }
557 if (entity == player)
558 {
559 return;
560 }
561 entity->SetYaw(packet.GetYRot());
562 entity->SetPitch(packet.GetXRot());
563#endif
564 entity->SetOnGround(packet.GetOnGround());
565 }
566 }
567
569 {
570 std::shared_ptr<Entity> entity = GetEntity(packet.GetEntityId());
571
572 if (entity == nullptr || !entity->IsAbstractMinecart())
573 {
574 return;
575 }
576
577 // Don't lerp, directly set the position to the last lerp step
578
579 const ProtocolCraft::MinecartBehaviorMinecartStep& step = packet.GetLerpSteps().back();
580 entity->SetPosition(step.GetPosition());
581 entity->SetYaw(360.0f * step.GetYRot() / 256.0f);
582 entity->SetPitch(360.0f * step.GetXRot() / 256.0f);
583 }
584#endif
585}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
void SetModifier(const ModifierKey &key, const Modifier &modifier)
virtual void Handle(ProtocolCraft::ClientboundLoginPacket &packet) override
std::shared_ptr< LocalPlayer > GetLocalPlayer()
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)
EntityManager(const std::shared_ptr< NetworkManager > &network_manager)
std::shared_ptr< Entity > GetEntity(const int id) const
static std::shared_ptr< Entity > CreateEntity(const EntityType type)
Definition Entity.cpp:1571
Mutex protected reference, will be locked until destroyed.
EquipmentSlot
Definition Enums.hpp:285
EntityEffectType
Definition Enums.hpp:330