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());
162 entities[packet.GetEntityId()] = entity;
165#if PROTOCOL_VERSION < 759
170 entity->SetEntityID(packet.GetEntityId());
171 entity->SetX(packet.GetX());
172 entity->SetY(packet.GetY());
173 entity->SetZ(packet.GetZ());
174 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
175 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
176 entity->SetUUID(packet.GetUuid());
179 entities[packet.GetEntityId()] = entity;
183#if PROTOCOL_VERSION < 770
188 entity->SetEntityID(packet.GetEntityId());
189 entity->SetX(packet.GetX());
190 entity->SetY(packet.GetY());
191 entity->SetZ(packet.GetZ());
194 entities[packet.GetEntityId()] = entity;
198#if PROTOCOL_VERSION < 721
203 entity->SetEntityID(packet.GetEntityId());
204 entity->SetX(packet.GetX());
205 entity->SetY(packet.GetY());
206 entity->SetZ(packet.GetZ());
209 entities[packet.GetEntityId()] = entity;
213#if PROTOCOL_VERSION < 764
216 std::shared_ptr<Entity> entity =
nullptr;
220 auto it =
entities.find(packet.GetEntityId());
224 entities[packet.GetEntityId()] = entity;
232 entity->SetEntityID(packet.GetEntityId());
233 entity->SetPosition(Vector3<double>(
238 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
239 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
240 entity->SetUUID(packet.GetPlayerId());
248 local_player->SetFoodSaturation(packet.GetFoodSaturation());
253 std::shared_ptr<Entity> entity =
nullptr;
257 auto it =
entities.find(packet.GetEntityId());
264 if (entity !=
nullptr)
266#if PROTOCOL_VERSION < 768
272 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
273 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
276 packet.GetRelatives() & (1 << 0) ? entity->GetX() + packet.GetChange().GetPosition()[0] : packet.GetChange().GetPosition()[0],
277 packet.GetRelatives() & (1 << 1) ? entity->GetY() + packet.GetChange().GetPosition()[1] : packet.GetChange().GetPosition()[1],
278 packet.GetRelatives() & (1 << 2) ? entity->GetZ() + packet.GetChange().GetPosition()[2] : packet.GetChange().GetPosition()[2]
280 entity->SetYaw(packet.GetRelatives() & (1 << 3) ? entity->GetYaw() + packet.GetChange().GetYRot() : packet.GetChange().GetYRot());
281 entity->SetPitch(packet.GetRelatives() & (1 << 4) ? entity->GetPitch() + packet.GetChange().GetXRot() : packet.GetChange().GetXRot());
283 entity->SetOnGround(packet.GetOnGround());
291 local_player->SetWalkingSpeed(packet.GetWalkingSpeed());
294#if PROTOCOL_VERSION == 755
298 entities.erase(packet.GetEntityId());
304 for (
int i = 0; i < packet.GetEntityIds().size(); ++i)
306 entities.erase(packet.GetEntityIds()[i]);
313#if PROTOCOL_VERSION < 764
316 local_player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
322 switch (packet.GetType())
334 std::shared_ptr<Entity> entity =
nullptr;
338 auto it =
entities.find(packet.GetEntityId());
345 if (entity ==
nullptr)
347 LOG_WARNING(
"Trying to load metadata in unexisting entity");
351 entity->LoadMetadataFromRawArray(packet.GetPackedItems());
357 std::shared_ptr<Entity> entity =
nullptr;
361 auto it =
entities.find(packet.GetEntityId());
368 if (entity ==
nullptr)
370 LOG_WARNING(
"Trying to set speed of an unexisting entity");
375 entity->SetSpeed(
Vector3<double>(packet.GetXA(), packet.GetYA(), packet.GetZA()) / 8000.0);
381 std::shared_ptr<Entity> entity =
nullptr;
385 auto it =
entities.find(packet.GetEntityId());
392 if (entity ==
nullptr)
394 LOG_WARNING(
"Trying to set equipment of an unexisting entity");
398#if PROTOCOL_VERSION > 730
399 for (
auto& p : packet.GetSlots())
401 entity->SetEquipment(
static_cast<EquipmentSlot>(p.first), p.second);
404 entity->SetEquipment(
static_cast<EquipmentSlot>(packet.GetSlot().first), packet.GetSlot().second);
411 std::shared_ptr<Entity> entity =
nullptr;
415 auto it =
entities.find(packet.GetEntityId());
422 if (entity ==
nullptr)
424 LOG_WARNING(
"Trying to set attributes of an unexisting entity");
426 else if (!entity->IsLivingEntity())
428 LOG_WARNING(
"Trying to set attributes of a non LivingEntity");
432 std::shared_ptr<LivingEntity> living_entity = std::dynamic_pointer_cast<LivingEntity>(entity);
433 for (
const auto& a : packet.GetAttributes())
435#if PROTOCOL_VERSION > 765
437#elif PROTOCOL_VERSION > 709
443 for (
const auto& m : a.GetModifiers())
446#
if PROTOCOL_VERSION < 767
454 living_entity->AddAttribute(attribute);
461 std::shared_ptr<Entity> entity =
nullptr;
465 auto it =
entities.find(packet.GetEntityId());
472 if (entity ==
nullptr)
474 LOG_WARNING(
"Trying to set effect of an unexisting entity");
480 static_cast<unsigned char>(packet.GetEffectAmplifier()),
481 std::chrono::steady_clock::now() + std::chrono::milliseconds(50 * packet.GetEffectDurationTicks())
489 std::shared_ptr<Entity> entity =
nullptr;
493 auto it =
entities.find(packet.GetEntityId());
500 if (entity ==
nullptr)
502 LOG_WARNING(
"Trying to remove effect of an unexisting entity");
510#if PROTOCOL_VERSION > 767
513 std::shared_ptr<Entity> entity =
nullptr;
517 auto it =
entities.find(packet.GetEntityId());
524 if (entity !=
nullptr)
526 entity->SetPosition(packet.GetValues().GetPosition());
531 entity->SetYaw(packet.GetValues().GetYRot());
532 entity->SetPitch(packet.GetValues().GetXRot());
533 entity->SetOnGround(packet.GetOnGround());
539 std::shared_ptr<Entity> entity =
nullptr;
543 auto it =
entities.find(packet.GetEntityId());
550 if (entity ==
nullptr || !entity->IsAbstractMinecart())
558 entity->SetPosition(step.GetPosition());
559 entity->SetYaw(360.0f * step.GetYRot() / 256.0f);
560 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.