25 return it ==
entities.end() ? nullptr : it->second;
30 if (entity ==
nullptr)
36 entities[entity->GetEntityID()] = entity;
49#if PROTOCOL_VERSION < 764
52 local_player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
58#if PROTOCOL_VERSION < 755
62 auto it =
entities.find(packet.GetEntityId());
65 std::shared_ptr<Entity> entity = std::make_shared<UnknownEntity>();
66 entity->SetEntityID(packet.GetEntityId());
67 entities[packet.GetEntityId()] = entity;
74 std::shared_ptr<Entity> entity =
nullptr;
78 auto it =
entities.find(packet.GetEntityId());
85 if (entity !=
nullptr)
89 (packet.GetXA() / 128.0f + entity_position.
x * 32.0f) / 32.0f,
90 (packet.GetYA() / 128.0f + entity_position.
y * 32.0f) / 32.0f,
91 (packet.GetZA() / 128.0f + entity_position.
z * 32.0f) / 32.0f
93 entity->SetOnGround(packet.GetOnGround());
99 std::shared_ptr<Entity> entity =
nullptr;
103 auto it =
entities.find(packet.GetEntityId());
110 if (entity !=
nullptr)
114 (packet.GetXA() / 128.0f + entity_position.
x * 32.0f) / 32.0f,
115 (packet.GetYA() / 128.0f + entity_position.
y * 32.0f) / 32.0f,
116 (packet.GetZA() / 128.0f + entity_position.
z * 32.0f) / 32.0f
118 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
119 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
120 entity->SetOnGround(packet.GetOnGround());
126 std::shared_ptr<Entity> entity =
nullptr;
130 auto it =
entities.find(packet.GetEntityId());
137 if (entity !=
nullptr)
139 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
140 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
141 entity->SetOnGround(packet.GetOnGround());
147#if PROTOCOL_VERSION < 458
148 std::shared_ptr<Entity> entity = Entity::CreateObjectEntity(
static_cast<ObjectEntityType
>(packet.GetType()));
153 entity->SetEntityID(packet.GetEntityId());
154 entity->SetX(packet.GetX());
155 entity->SetY(packet.GetY());
156 entity->SetZ(packet.GetZ());
157 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
158 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
159 entity->SetUUID(packet.GetUuid());
160#if PROTOCOL_VERSION < 773
162 entity->SetSpeed(
Vector3<double>(packet.GetXa(), packet.GetYa(), packet.GetZa()) / 8000.0);
168 entities[packet.GetEntityId()] = entity;
176 if (entity ==
nullptr)
178 LOG_WARNING(
"Trying to apply knockback without a valid player");
182#if PROTOCOL_VERSION < 768
184 static_cast<double>(packet.GetKnockbackX()),
185 static_cast<double>(packet.GetKnockbackY()),
186 static_cast<double>(packet.GetKnockbackZ())
189 if (packet.GetPlayerKnockback().has_value())
191 entity->SetSpeed(entity->GetSpeed() + packet.GetPlayerKnockback().value());
197#if PROTOCOL_VERSION < 759
202 entity->SetEntityID(packet.GetEntityId());
203 entity->SetX(packet.GetX());
204 entity->SetY(packet.GetY());
205 entity->SetZ(packet.GetZ());
206 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
207 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
208 entity->SetUUID(packet.GetUuid());
211 entities[packet.GetEntityId()] = entity;
215#if PROTOCOL_VERSION < 770
220 entity->SetEntityID(packet.GetEntityId());
221 entity->SetX(packet.GetX());
222 entity->SetY(packet.GetY());
223 entity->SetZ(packet.GetZ());
226 entities[packet.GetEntityId()] = entity;
230#if PROTOCOL_VERSION < 721
235 entity->SetEntityID(packet.GetEntityId());
236 entity->SetX(packet.GetX());
237 entity->SetY(packet.GetY());
238 entity->SetZ(packet.GetZ());
241 entities[packet.GetEntityId()] = entity;
245#if PROTOCOL_VERSION < 764
248 std::shared_ptr<Entity> entity =
nullptr;
252 auto it =
entities.find(packet.GetEntityId());
256 entities[packet.GetEntityId()] = entity;
264 entity->SetEntityID(packet.GetEntityId());
265 entity->SetPosition(Vector3<double>(
270 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
271 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
272 entity->SetUUID(packet.GetPlayerId());
280 local_player->SetFoodSaturation(packet.GetFoodSaturation());
285 std::shared_ptr<Entity> entity =
nullptr;
289 auto it =
entities.find(packet.GetEntityId());
296 if (entity !=
nullptr)
298#if PROTOCOL_VERSION < 768
304 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
305 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
308 packet.GetRelatives() & (1 << 0) ? entity->GetX() + packet.GetChange().GetPosition()[0] : packet.GetChange().GetPosition()[0],
309 packet.GetRelatives() & (1 << 1) ? entity->GetY() + packet.GetChange().GetPosition()[1] : packet.GetChange().GetPosition()[1],
310 packet.GetRelatives() & (1 << 2) ? entity->GetZ() + packet.GetChange().GetPosition()[2] : packet.GetChange().GetPosition()[2]
312 entity->SetYaw(packet.GetRelatives() & (1 << 3) ? entity->GetYaw() + packet.GetChange().GetYRot() : packet.GetChange().GetYRot());
313 entity->SetPitch(packet.GetRelatives() & (1 << 4) ? entity->GetPitch() + packet.GetChange().GetXRot() : packet.GetChange().GetXRot());
315 entity->SetOnGround(packet.GetOnGround());
323 local_player->SetWalkingSpeed(packet.GetWalkingSpeed());
326#if PROTOCOL_VERSION == 755
330 entities.erase(packet.GetEntityId());
336 for (
int i = 0; i < packet.GetEntityIds().size(); ++i)
338 entities.erase(packet.GetEntityIds()[i]);
345#if PROTOCOL_VERSION < 764
348 local_player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
354 switch (packet.GetType())
366 std::shared_ptr<Entity> entity =
nullptr;
370 auto it =
entities.find(packet.GetEntityId());
377 if (entity ==
nullptr)
379 LOG_WARNING(
"Trying to load metadata in unexisting entity");
383 entity->LoadMetadataFromRawArray(packet.GetPackedItems());
389 std::shared_ptr<Entity> entity =
nullptr;
393 auto it =
entities.find(packet.GetEntityId());
400 if (entity ==
nullptr)
402 LOG_WARNING(
"Trying to set speed of an unexisting entity");
406#if PROTOCOL_VERSION < 773
408 entity->SetSpeed(
Vector3<double>(packet.GetXA(), packet.GetYA(), packet.GetZA()) / 8000.0);
417 std::shared_ptr<Entity> entity =
nullptr;
421 auto it =
entities.find(packet.GetEntityId());
428 if (entity ==
nullptr)
430 LOG_WARNING(
"Trying to set equipment of an unexisting entity");
434#if PROTOCOL_VERSION > 730
435 for (
auto& p : packet.GetSlots())
437 entity->SetEquipment(
static_cast<EquipmentSlot>(p.first), p.second);
440 entity->SetEquipment(
static_cast<EquipmentSlot>(packet.GetSlot().first), packet.GetSlot().second);
447 std::shared_ptr<Entity> entity =
nullptr;
451 auto it =
entities.find(packet.GetEntityId());
458 if (entity ==
nullptr)
460 LOG_WARNING(
"Trying to set attributes of an unexisting entity");
462 else if (!entity->IsLivingEntity())
464 LOG_WARNING(
"Trying to set attributes of a non LivingEntity");
468 std::shared_ptr<LivingEntity> living_entity = std::dynamic_pointer_cast<LivingEntity>(entity);
469 for (
const auto& a : packet.GetAttributes())
471#if PROTOCOL_VERSION > 765
473#elif PROTOCOL_VERSION > 709
479 for (
const auto& m : a.GetModifiers())
482#
if PROTOCOL_VERSION < 767
490 living_entity->AddAttribute(attribute);
497 std::shared_ptr<Entity> entity =
nullptr;
501 auto it =
entities.find(packet.GetEntityId());
508 if (entity ==
nullptr)
510 LOG_WARNING(
"Trying to set effect of an unexisting entity");
516 static_cast<unsigned char>(packet.GetEffectAmplifier()),
517 std::chrono::steady_clock::now() + std::chrono::milliseconds(50 * packet.GetEffectDurationTicks())
525 std::shared_ptr<Entity> entity =
nullptr;
529 auto it =
entities.find(packet.GetEntityId());
536 if (entity ==
nullptr)
538 LOG_WARNING(
"Trying to remove effect of an unexisting entity");
546#if PROTOCOL_VERSION > 767
549 std::shared_ptr<Entity> entity =
nullptr;
553 auto it =
entities.find(packet.GetEntityId());
560 if (entity !=
nullptr)
562 entity->SetPosition(packet.GetValues().GetPosition());
567 entity->SetYaw(packet.GetValues().GetYRot());
568 entity->SetPitch(packet.GetValues().GetXRot());
569 entity->SetOnGround(packet.GetOnGround());
575 std::shared_ptr<Entity> entity =
nullptr;
579 auto it =
entities.find(packet.GetEntityId());
586 if (entity ==
nullptr || !entity->IsAbstractMinecart())
594 entity->SetPosition(step.GetPosition());
595 entity->SetYaw(360.0f * step.GetYRot() / 256.0f);
596 entity->SetPitch(360.0f * step.GetXRot() / 256.0f);
#define LOG_WARNING(osstream)
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)
Mutex protected reference, will be locked until destroyed.