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;
171#if PROTOCOL_VERSION < 759
176 entity->SetEntityID(packet.GetEntityId());
177 entity->SetX(packet.GetX());
178 entity->SetY(packet.GetY());
179 entity->SetZ(packet.GetZ());
180 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
181 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
182 entity->SetUUID(packet.GetUuid());
185 entities[packet.GetEntityId()] = entity;
189#if PROTOCOL_VERSION < 770
194 entity->SetEntityID(packet.GetEntityId());
195 entity->SetX(packet.GetX());
196 entity->SetY(packet.GetY());
197 entity->SetZ(packet.GetZ());
200 entities[packet.GetEntityId()] = entity;
204#if PROTOCOL_VERSION < 721
209 entity->SetEntityID(packet.GetEntityId());
210 entity->SetX(packet.GetX());
211 entity->SetY(packet.GetY());
212 entity->SetZ(packet.GetZ());
215 entities[packet.GetEntityId()] = entity;
219#if PROTOCOL_VERSION < 764
222 std::shared_ptr<Entity> entity =
nullptr;
226 auto it =
entities.find(packet.GetEntityId());
230 entities[packet.GetEntityId()] = entity;
238 entity->SetEntityID(packet.GetEntityId());
239 entity->SetPosition(Vector3<double>(
244 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
245 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
246 entity->SetUUID(packet.GetPlayerId());
254 local_player->SetFoodSaturation(packet.GetFoodSaturation());
259 std::shared_ptr<Entity> entity =
nullptr;
263 auto it =
entities.find(packet.GetEntityId());
270 if (entity !=
nullptr)
272#if PROTOCOL_VERSION < 768
278 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
279 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
282 packet.GetRelatives() & (1 << 0) ? entity->GetX() + packet.GetChange().GetPosition()[0] : packet.GetChange().GetPosition()[0],
283 packet.GetRelatives() & (1 << 1) ? entity->GetY() + packet.GetChange().GetPosition()[1] : packet.GetChange().GetPosition()[1],
284 packet.GetRelatives() & (1 << 2) ? entity->GetZ() + packet.GetChange().GetPosition()[2] : packet.GetChange().GetPosition()[2]
286 entity->SetYaw(packet.GetRelatives() & (1 << 3) ? entity->GetYaw() + packet.GetChange().GetYRot() : packet.GetChange().GetYRot());
287 entity->SetPitch(packet.GetRelatives() & (1 << 4) ? entity->GetPitch() + packet.GetChange().GetXRot() : packet.GetChange().GetXRot());
289 entity->SetOnGround(packet.GetOnGround());
297 local_player->SetWalkingSpeed(packet.GetWalkingSpeed());
300#if PROTOCOL_VERSION == 755
304 entities.erase(packet.GetEntityId());
310 for (
int i = 0; i < packet.GetEntityIds().size(); ++i)
312 entities.erase(packet.GetEntityIds()[i]);
319#if PROTOCOL_VERSION < 764
322 local_player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
328 switch (packet.GetType())
340 std::shared_ptr<Entity> entity =
nullptr;
344 auto it =
entities.find(packet.GetEntityId());
351 if (entity ==
nullptr)
353 LOG_WARNING(
"Trying to load metadata in unexisting entity");
357 entity->LoadMetadataFromRawArray(packet.GetPackedItems());
363 std::shared_ptr<Entity> entity =
nullptr;
367 auto it =
entities.find(packet.GetEntityId());
374 if (entity ==
nullptr)
376 LOG_WARNING(
"Trying to set speed of an unexisting entity");
380#if PROTOCOL_VERSION < 773
382 entity->SetSpeed(
Vector3<double>(packet.GetXA(), packet.GetYA(), packet.GetZA()) / 8000.0);
391 std::shared_ptr<Entity> entity =
nullptr;
395 auto it =
entities.find(packet.GetEntityId());
402 if (entity ==
nullptr)
404 LOG_WARNING(
"Trying to set equipment of an unexisting entity");
408#if PROTOCOL_VERSION > 730
409 for (
auto& p : packet.GetSlots())
411 entity->SetEquipment(
static_cast<EquipmentSlot>(p.first), p.second);
414 entity->SetEquipment(
static_cast<EquipmentSlot>(packet.GetSlot().first), packet.GetSlot().second);
421 std::shared_ptr<Entity> entity =
nullptr;
425 auto it =
entities.find(packet.GetEntityId());
432 if (entity ==
nullptr)
434 LOG_WARNING(
"Trying to set attributes of an unexisting entity");
436 else if (!entity->IsLivingEntity())
438 LOG_WARNING(
"Trying to set attributes of a non LivingEntity");
442 std::shared_ptr<LivingEntity> living_entity = std::dynamic_pointer_cast<LivingEntity>(entity);
443 for (
const auto& a : packet.GetAttributes())
445#if PROTOCOL_VERSION > 765
447#elif PROTOCOL_VERSION > 709
453 for (
const auto& m : a.GetModifiers())
456#
if PROTOCOL_VERSION < 767
464 living_entity->AddAttribute(attribute);
471 std::shared_ptr<Entity> entity =
nullptr;
475 auto it =
entities.find(packet.GetEntityId());
482 if (entity ==
nullptr)
484 LOG_WARNING(
"Trying to set effect of an unexisting entity");
490 static_cast<unsigned char>(packet.GetEffectAmplifier()),
491 std::chrono::steady_clock::now() + std::chrono::milliseconds(50 * packet.GetEffectDurationTicks())
499 std::shared_ptr<Entity> entity =
nullptr;
503 auto it =
entities.find(packet.GetEntityId());
510 if (entity ==
nullptr)
512 LOG_WARNING(
"Trying to remove effect of an unexisting entity");
520#if PROTOCOL_VERSION > 767
523 std::shared_ptr<Entity> entity =
nullptr;
527 auto it =
entities.find(packet.GetEntityId());
534 if (entity !=
nullptr)
536 entity->SetPosition(packet.GetValues().GetPosition());
541 entity->SetYaw(packet.GetValues().GetYRot());
542 entity->SetPitch(packet.GetValues().GetXRot());
543 entity->SetOnGround(packet.GetOnGround());
549 std::shared_ptr<Entity> entity =
nullptr;
553 auto it =
entities.find(packet.GetEntityId());
560 if (entity ==
nullptr || !entity->IsAbstractMinecart())
568 entity->SetPosition(step.GetPosition());
569 entity->SetYaw(360.0f * step.GetYRot() / 256.0f);
570 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.