13#if PROTOCOL_VERSION > 776
34 return it ==
entities.end() ? nullptr : it->second;
39 if (entity ==
nullptr)
45 entities[entity->GetEntityID()] = entity;
59#if PROTOCOL_VERSION < 764
62 local_player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
67#if PROTOCOL_VERSION < 755
71 auto it =
entities.find(packet.GetEntityId());
74 std::shared_ptr<Entity> entity = std::make_shared<UnknownEntity>();
75 entity->SetEntityID(packet.GetEntityId());
76 entities[packet.GetEntityId()] = entity;
83 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
85 if (entity !=
nullptr)
88#if PROTOCOL_VERSION < 777
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
95 if (packet.GetSingleDelta().has_value())
97 const std::array<short, 3>& delta = packet.GetSingleDelta().value();
98 for (
int i = 0; i < 3; ++i)
104 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
107 else if (packet.GetSteppedDeltas().has_value())
109 const std::vector<std::pair<int, std::array<short, 3>>>& steps = packet.GetSteppedDeltas().value();
110 for (
auto& [tick, delta] : steps)
112 for (
int i = 0; i < 3; ++i)
118 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
124 throw std::runtime_error(
"Invalid ClientboundMoveEntityPacket");
127 entity->SetPosition(entity_position);
134 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
136 if (entity !=
nullptr)
139#if PROTOCOL_VERSION < 777
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
146 if (packet.GetSingleDelta().has_value())
148 const std::array<short, 3>& delta = packet.GetSingleDelta().value();
149 for (
int i = 0; i < 3; ++i)
155 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
158 else if (packet.GetSteppedDeltas().has_value())
160 const std::vector<std::pair<int, std::array<short, 3>>>& steps = packet.GetSteppedDeltas().value();
161 for (
auto& [tick, delta] : steps)
163 for (
int i = 0; i < 3; ++i)
169 entity_position[i] = (std::round(entity_position[i] * 4096.0) + delta[i]) / 4096.0;
175 throw std::runtime_error(
"Invalid ClientboundMoveEntityPacketPosRot");
178 entity->SetPosition(entity_position);
179 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
180 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
187 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
189 if (entity !=
nullptr)
191 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
192 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
193 entity->SetOnGround(packet.GetOnGround());
199#if PROTOCOL_VERSION < 458
200 std::shared_ptr<Entity> entity = Entity::CreateObjectEntity(
static_cast<ObjectEntityType
>(packet.GetType()));
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
214 entity->SetSpeed(
Vector3<double>(packet.GetXa(), packet.GetYa(), packet.GetZa()) / 8000.0);
220 entities[packet.GetEntityId()] = entity;
227 if (entity ==
nullptr)
229 LOG_WARNING(
"Trying to apply knockback without a valid player");
233#if PROTOCOL_VERSION < 768
235 static_cast<double>(packet.GetKnockbackX()),
236 static_cast<double>(packet.GetKnockbackY()),
237 static_cast<double>(packet.GetKnockbackZ())
240 if (packet.GetPlayerKnockback().has_value())
242 entity->AddSpeed(packet.GetPlayerKnockback().value());
248#if PROTOCOL_VERSION < 759
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());
262 entities[packet.GetEntityId()] = entity;
266#if PROTOCOL_VERSION < 770
271 entity->SetEntityID(packet.GetEntityId());
272 entity->SetX(packet.GetX());
273 entity->SetY(packet.GetY());
274 entity->SetZ(packet.GetZ());
277 entities[packet.GetEntityId()] = entity;
281#if PROTOCOL_VERSION < 721
286 entity->SetEntityID(packet.GetEntityId());
287 entity->SetX(packet.GetX());
288 entity->SetY(packet.GetY());
289 entity->SetZ(packet.GetZ());
292 entities[packet.GetEntityId()] = entity;
296#if PROTOCOL_VERSION < 764
299 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
301 if (entity ==
nullptr)
304 entities[packet.GetEntityId()] = entity;
307 entity->SetEntityID(packet.GetEntityId());
308 entity->SetPosition(Vector3<double>(
313 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
314 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
315 entity->SetUUID(packet.GetPlayerId());
322 player->SetHealth(packet.GetHealth());
323 player->SetFood(packet.GetFood());
324 player->SetFoodSaturation(packet.GetFoodSaturation());
329 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
331 if (entity !=
nullptr)
333#if PROTOCOL_VERSION < 768
339 entity->SetYaw(360.0f * packet.GetYRot() / 256.0f);
340 entity->SetPitch(360.0f * packet.GetXRot() / 256.0f);
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]
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());
350 entity->SetOnGround(packet.GetOnGround());
357 player->SetAbilitiesFlags(packet.GetFlags());
358 player->SetFlyingSpeed(packet.GetFlyingSpeed());
359 player->SetWalkingSpeed(packet.GetWalkingSpeed());
362#if PROTOCOL_VERSION == 755
366 entities.erase(packet.GetEntityId());
372 for (
int i = 0; i < packet.GetEntityIds().size(); ++i)
374 entities.erase(packet.GetEntityIds()[i]);
382#if PROTOCOL_VERSION < 764
383 player->SetGameMode(
static_cast<GameType>(packet.GetPlayerGameType()));
385 player->SetGameMode(
static_cast<GameType>(packet.GetCommonPlayerSpawnInfo().GetGameType()));
391 switch (packet.GetType())
403 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
405 if (entity ==
nullptr)
407 LOG_WARNING(
"Trying to load metadata in unexisting entity");
411 entity->LoadMetadataFromRawArray(packet.GetPackedItems());
417 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
419 if (entity ==
nullptr)
421 LOG_WARNING(
"Trying to set speed of an unexisting entity");
425#if PROTOCOL_VERSION < 773
427 entity->SetSpeed(
Vector3<double>(packet.GetXA(), packet.GetYA(), packet.GetZA()) / 8000.0);
436 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
438 if (entity ==
nullptr)
440 LOG_WARNING(
"Trying to set equipment of an unexisting entity");
444#if PROTOCOL_VERSION > 730
445 for (
auto& p : packet.GetSlots())
447 entity->SetEquipment(
static_cast<EquipmentSlot>(p.first), p.second);
450 entity->SetEquipment(
static_cast<EquipmentSlot>(packet.GetSlot().first), packet.GetSlot().second);
457 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
459 if (entity ==
nullptr)
461 LOG_WARNING(
"Trying to set attributes of an unexisting entity");
463 else if (!entity->IsLivingEntity())
465 LOG_WARNING(
"Trying to set attributes of a non LivingEntity");
469 std::shared_ptr<LivingEntity> living_entity = std::dynamic_pointer_cast<LivingEntity>(entity);
470 for (
const auto& a : packet.GetAttributes())
472#if PROTOCOL_VERSION > 765
474#elif PROTOCOL_VERSION > 709
480 for (
const auto& m : a.GetModifiers())
483#
if PROTOCOL_VERSION < 767
491 living_entity->AddAttribute(attribute);
498 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
500 if (entity ==
nullptr)
502 LOG_WARNING(
"Trying to set effect of an unexisting entity");
508 static_cast<unsigned char>(packet.GetEffectAmplifier()),
509 std::chrono::steady_clock::now() + std::chrono::milliseconds(50 * packet.GetEffectDurationTicks())
516 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
518 if (entity ==
nullptr)
520 LOG_WARNING(
"Trying to remove effect of an unexisting entity");
528#if PROTOCOL_VERSION > 767
531 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
533 if (entity !=
nullptr)
536#if PROTOCOL_VERSION < 777
537 entity->SetPosition(packet.GetValues().GetPosition());
538 if (entity == player)
542 entity->SetYaw(packet.GetValues().GetYRot());
543 entity->SetPitch(packet.GetValues().GetXRot());
545 switch (packet.GetPosition().GetType())
548 entity->SetPosition(std::static_pointer_cast<ProtocolCraft::PositionPathLinear>(packet.GetPosition().GetData())->GetEndPosition());
551 entity->SetPosition(std::static_pointer_cast<ProtocolCraft::PositionPathStepped>(packet.GetPosition().GetData())->GetSteps().back().GetPosition());
554 throw std::runtime_error(
"Unknown PositionPathDataType: " + std::to_string(
static_cast<int>(packet.GetPosition().GetType())));
557 if (entity == player)
561 entity->SetYaw(packet.GetYRot());
562 entity->SetPitch(packet.GetXRot());
564 entity->SetOnGround(packet.GetOnGround());
570 std::shared_ptr<Entity> entity =
GetEntity(packet.GetEntityId());
572 if (entity ==
nullptr || !entity->IsAbstractMinecart())
580 entity->SetPosition(step.GetPosition());
581 entity->SetYaw(360.0f * step.GetYRot() / 256.0f);
582 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.