Botcraft 1.21.5
Loading...
Searching...
No Matches
Entity.cpp
Go to the documentation of this file.
2
7#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
9#endif
10#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
12#endif
13
14#if USE_GUI
17#endif
19
20#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
23#endif
24#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
26#endif
28#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
30#endif
33#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
35#endif
36#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
39#endif
41#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
43#endif
46#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
48#endif
49#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
51#endif
52#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
54#endif
55#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
57#endif
58#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
60#endif
61#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
63#endif
66#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
68#endif
70#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
72#endif
73#if PROTOCOL_VERSION > 767 /* > 1.21.1 */ && PROTOCOL_VERSION < 769 /* < 1.21.4 */
75#endif
77#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
79#endif
82#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
84#endif
85#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
90#endif
102#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
104#endif
105#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
107#endif
110#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
114#endif
116#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
118#endif
122#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
124#endif
128#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
130#endif
137#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
139#endif
151#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
153#endif
155#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
157#endif
159#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
161#else
163#endif
164#if PROTOCOL_VERSION > 736 /* > 1.16.1 */
166#endif
167#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
169#endif
171#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
174#endif
176#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
178#endif
180#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
182#endif
183#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
185#endif
186#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
188#else
190#endif
198#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
200#endif
207#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
209#endif
210#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
212#endif
217#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
219#endif
220#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
222#endif
223
224#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
226#endif
227#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
229#endif
233#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
235#endif
236#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
238#endif
239#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
241#endif
246#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
248#else
250#endif
251#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
253#endif
257#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
259#endif
262
263#include <mutex>
264
265namespace Botcraft
266{
267 const std::array<std::string, Entity::metadata_count> Entity::metadata_names{ {
268 "data_shared_flags_id",
269 "data_air_supply_id",
270 "data_custom_name",
271 "data_custom_name_visible",
272 "data_silent",
273 "data_no_gravity",
274#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
275 "data_pose",
276#endif
277#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
278 "data_ticks_frozen",
279#endif
280 } };
281
283 {
284 {
285 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
286 // Initialize base stuff
287 entity_id = 0;
288 position = Vector3<double>(0.0, 0.0, 0.0);
289 yaw = 0.0f;
290 pitch = 0.0f;
291 speed = Vector3<double>(0.0, 0.0, 0.0);
292 on_ground = false;
293 equipments = {
300#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
302#endif
303 };
304 }
305
306 // Initialize all metadata with default values
309#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
310 SetDataCustomName(std::optional<ProtocolCraft::Chat>());
311#else
313#endif
315 SetDataSilent(false);
316 SetDataNoGravity(false);
317#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
319#endif
320#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
322#endif
323 }
324
326 {
327
328 }
329
330
332 {
333 std::shared_lock<std::shared_mutex> lock(entity_mutex);
334 return GetColliderImpl();
335 }
336
337 double Entity::GetWidth() const
338 {
339 std::shared_lock<std::shared_mutex> lock(entity_mutex);
340 return GetWidthImpl();
341 }
342
343 double Entity::GetHeight() const
344 {
345 std::shared_lock<std::shared_mutex> lock(entity_mutex);
346 return GetHeightImpl();
347 }
348
349
350 void Entity::LoadMetadataFromRawArray(const std::vector<unsigned char>& data)
351 {
352 if (data.size() == 0)
353 {
354 return;
355 }
356
357 ProtocolCraft::ReadIterator iter = data.begin();
358 size_t length = data.size();
359
360 while (true)
361 {
362 const unsigned char index = ProtocolCraft::ReadData<unsigned char>(iter, length);
363 if (index == 0xFF)
364 {
365 break;
366 }
367
368 const int type = ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length);
369 std::any value;
370
371 enum EntityMetadataTypes
372 {
373 Char = 0,
374 Int,
375#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
376 Long,
377#endif
378 Float,
379 String,
380 Chat,
381#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
382 OptionalChat,
383#endif
384 Slot,
385 Bool,
386 Rotations,
387 BlockPosition,
388 OptionalBlockPosition,
389 DirectionType,
390#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
391 OptionalLivingEntityReference, // Fancy new name for 1.21.5, but it's still an optional UUID
392#else
393 OptionalUUID,
394#endif
395 BlockstateType,
396#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
397 OptionalBlockstate,
398#endif
399 NBT,
400#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
401 Particle,
402#endif
403#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
404 Particles,
405#endif
406#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
407 VillagerDataType,
408 OptionalUint,
409 PoseType,
410#endif
411#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
412 CatVariant,
413#endif
414#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
415 CowVariant,
416#endif
417#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
418 WolfVariant,
419#endif
420#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
421 WolfSoundVariant,
422#endif
423#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
424 FrogVariant,
425#endif
426#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
427 PigVariant,
428 ChickenVariant,
429#endif
430#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
431 OptionalGlobalPos,
432 PaintingVariant,
433#endif
434#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
435 SnifferState,
436#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
437 ArmadilloState,
438#endif
439 Vec3,
440 Quaternion,
441#endif
442 };
443
444 switch (type)
445 {
446 case EntityMetadataTypes::Char:
447 value = ProtocolCraft::ReadData<char>(iter, length);
448 break;
449 case EntityMetadataTypes::Int:
450 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
451 break;
452#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
453 case EntityMetadataTypes::Long:
454 value = static_cast<long long int>(ProtocolCraft::ReadData<ProtocolCraft::VarLong>(iter, length));
455 break;
456#endif
457 case EntityMetadataTypes::Float:
458 value = ProtocolCraft::ReadData<float>(iter, length);
459 break;
460 case EntityMetadataTypes::String:
461 value = ProtocolCraft::ReadData<std::string>(iter, length);
462 break;
463 case EntityMetadataTypes::Chat:
464 value = ProtocolCraft::Chat();
465 std::any_cast<ProtocolCraft::Chat&>(value).Read(iter, length);
466 break;
467#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
468 case EntityMetadataTypes::OptionalChat:
469 if (ProtocolCraft::ReadData<bool>(iter, length))
470 {
471 value = std::optional<ProtocolCraft::Chat>(ProtocolCraft::Chat());
472 std::any_cast<std::optional<ProtocolCraft::Chat>&>(value).value().Read(iter, length);
473 }
474 else
475 {
476 value = std::optional<ProtocolCraft::Chat>();
477 }
478 break;
479#endif
480 case EntityMetadataTypes::Slot:
481 value = ProtocolCraft::Slot();
482 std::any_cast<ProtocolCraft::Slot&>(value).Read(iter, length);
483 break;
484 case EntityMetadataTypes::Bool:
485 value = ProtocolCraft::ReadData<bool>(iter, length);
486 break;
487 case EntityMetadataTypes::Rotations:
488 {
489 Vector3<float> rotation;
490 for (int i = 0; i < 3; ++i)
491 {
492 rotation[i] = ProtocolCraft::ReadData<float>(iter, length);
493 }
494 value = rotation;
495 break;
496 }
497 case EntityMetadataTypes::BlockPosition:
498 {
500 position.Read(iter, length);
501 value = Position(position);
502 break;
503 }
504 case EntityMetadataTypes::OptionalBlockPosition:
505 if (ProtocolCraft::ReadData<bool>(iter, length))
506 {
508 position.Read(iter, length);
509 value = std::optional<Position>(position);
510 }
511 else
512 {
513 value = std::optional<Position>();
514 }
515 break;
516 case EntityMetadataTypes::DirectionType:
517 value = static_cast<Direction>(static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length)));
518 break;
519#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
520 case EntityMetadataTypes::OptionalLivingEntityReference:
521#else
522 case EntityMetadataTypes::OptionalUUID:
523#endif
524 if (ProtocolCraft::ReadData<bool>(iter, length))
525 {
526 value = std::optional<ProtocolCraft::UUID>(ProtocolCraft::ReadData<ProtocolCraft::UUID>(iter, length));
527 }
528 else
529 {
530 value = std::optional<ProtocolCraft::UUID>();
531 }
532 break;
533 case EntityMetadataTypes::BlockstateType:
534 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
535 break;
536#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
537 case EntityMetadataTypes::OptionalBlockstate:
538 if (const int n = ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length))
539 {
540 value = std::optional<int>(n);
541 }
542 else
543 {
544 value = std::optional<int>();
545 }
546 break;
547#endif
548 case EntityMetadataTypes::NBT:
549 {
551 unnamed_value.Read(iter, length);
552 value = ProtocolCraft::NBT::Value(unnamed_value);
553 break;
554 }
555#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
556 case EntityMetadataTypes::Particle:
557 {
559 particle.Read(iter, length);
560 value = particle;
561 break;
562 }
563#endif
564#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
565 case EntityMetadataTypes::Particles:
566 value = ProtocolCraft::ReadData<std::vector<ProtocolCraft::Particle>>(iter, length);
567 break;
568#endif
569#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
570 case EntityMetadataTypes::VillagerDataType:
571 value = VillagerData{
572 ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length), // villager_type
573 ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length), // villager_profession
574 ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length) // level
575 };
576 break;
577 case EntityMetadataTypes::OptionalUint:
578 {
579 const int val = ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length);
580 value = val > 0 ? std::optional<int>(val - 1) : std::optional<int>();
581 break;
582 }
583 case EntityMetadataTypes::PoseType:
584 value = static_cast<Pose>(static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length)));
585 break;
586#endif
587#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
588 case EntityMetadataTypes::CatVariant:
589 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
590 break;
591#endif
592#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
593 case EntityMetadataTypes::CowVariant:
594 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
595 break;
596#endif
597#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
598 case EntityMetadataTypes::WolfVariant:
599 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
600 break;
601#endif
602#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
603 case EntityMetadataTypes::WolfSoundVariant:
604 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
605 break;
606#endif
607#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
608 case EntityMetadataTypes::FrogVariant:
609 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
610 break;
611#endif
612#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
613 case EntityMetadataTypes::PigVariant:
614 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
615 break;
616 case EntityMetadataTypes::ChickenVariant:
617 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
618 break;
619#endif
620#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
621 case EntityMetadataTypes::OptionalGlobalPos:
622 if (ProtocolCraft::ReadData<bool>(iter, length))
623 {
625 dimension.Read(iter, length);
627 pos.Read(iter, length);
628
629 value = std::optional<GlobalPos>({
630 dimension,
631 pos
632 });
633 }
634 else
635 {
636 value = std::optional<GlobalPos>();
637 }
638 break;
639 case EntityMetadataTypes::PaintingVariant:
640 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
641 break;
642#endif
643#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
644 case EntityMetadataTypes::SnifferState:
645 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
646 break;
647#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
648 case EntityMetadataTypes::ArmadilloState:
649 value = static_cast<int>(ProtocolCraft::ReadData<ProtocolCraft::VarInt>(iter, length));
650 break;
651#endif
652 case EntityMetadataTypes::Vec3:
653 {
654 const float x = ProtocolCraft::ReadData<float>(iter, length);
655 const float y = ProtocolCraft::ReadData<float>(iter, length);
656 const float z = ProtocolCraft::ReadData<float>(iter, length);
657 value = Vector3<float>(x, y, z);
658 }
659 break;
660 case EntityMetadataTypes::Quaternion:
661 {
662 const float x = ProtocolCraft::ReadData<float>(iter, length);
663 const float y = ProtocolCraft::ReadData<float>(iter, length);
664 const float z = ProtocolCraft::ReadData<float>(iter, length);
665 const float w = ProtocolCraft::ReadData<float>(iter, length);
666 value = std::array<float, 4>{x, y, z, w};
667 }
668 break;
669#endif
670 default:
671 LOG_ERROR("Unknown type in entity metadata : " << type << ".Stopping current metadata parsing.");
672 return;
673 }
674 SetMetadataValue(index, value);
675 }
676 }
677
678 void Entity::SetMetadataValue(const int index, const std::any& value)
679 {
680 assert(index >= 0 && index < metadata_count);
681 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
682 const std::string& metadata_name = metadata_names[index];
683 metadata[metadata_name] = value;
684#if USE_GUI && PROTOCOL_VERSION > 404 /* > 1.13.2 */
685 if (metadata_name == "data_pose")
686 {
688 }
689#endif
690 }
691
692
694 {
695 std::shared_lock<std::shared_mutex> lock(entity_mutex);
697 }
698
700 {
701 std::shared_lock<std::shared_mutex> lock(entity_mutex);
702 return GetDataSharedFlagsIdImpl(id);
703 }
704
706 {
707 std::shared_lock<std::shared_mutex> lock(entity_mutex);
708 return std::any_cast<int>(metadata.at("data_air_supply_id"));
709 }
710
711#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
712 std::optional<ProtocolCraft::Chat> Entity::GetDataCustomName() const
713 {
714 std::shared_lock<std::shared_mutex> lock(entity_mutex);
715 return std::any_cast<std::optional<ProtocolCraft::Chat>>(metadata.at("data_custom_name"));
716 }
717#else
718 std::string Entity::GetDataCustomName() const
719 {
720 std::shared_lock<std::shared_mutex> lock(entity_mutex);
721 return std::any_cast<std::string>(metadata.at("data_custom_name"));
722 }
723#endif
724
726 {
727 std::shared_lock<std::shared_mutex> lock(entity_mutex);
728 return std::any_cast<bool>(metadata.at("data_custom_name_visible"));
729 }
730
732 {
733 std::shared_lock<std::shared_mutex> lock(entity_mutex);
734 return std::any_cast<bool>(metadata.at("data_silent"));
735 }
736
738 {
739 std::shared_lock<std::shared_mutex> lock(entity_mutex);
740 return std::any_cast<bool>(metadata.at("data_no_gravity"));
741 }
742
743#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
745 {
746 std::shared_lock<std::shared_mutex> lock(entity_mutex);
747 return GetDataPoseImpl();
748 }
749#endif
750
751#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
753 {
754 std::shared_lock<std::shared_mutex> lock(entity_mutex);
755 return std::any_cast<int>(metadata.at("data_ticks_frozen"));
756 }
757#endif
758
759
760 void Entity::SetDataSharedFlagsId(const char data_shared_flags_id)
761 {
762 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
763 SetDataSharedFlagsIdImpl(data_shared_flags_id);
764 }
765
767 {
768 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
770 }
771
772 void Entity::SetDataAirSupplyId(const int data_air_supply_id)
773 {
774 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
775 metadata["data_air_supply_id"] = data_air_supply_id;
776 }
777
778#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
779 void Entity::SetDataCustomName(const std::optional<ProtocolCraft::Chat>& data_custom_name)
780 {
781 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
782 metadata["data_custom_name"] = data_custom_name;
783 }
784#else
785 void Entity::SetDataCustomName(const std::string& data_custom_name)
786 {
787 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
788 metadata["data_custom_name"] = data_custom_name;
789 }
790#endif
791
792 void Entity::SetDataCustomNameVisible(const bool data_custom_name_visible)
793 {
794 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
795 metadata["data_custom_name_visible"] = data_custom_name_visible;
796 }
797
798 void Entity::SetDataSilent(const bool data_silent)
799 {
800 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
801 metadata["data_silent"] = data_silent;
802 }
803
804 void Entity::SetDataNoGravity(const bool data_no_gravity)
805 {
806 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
807 metadata["data_no_gravity"] = data_no_gravity;
808 }
809
810#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
811 void Entity::SetDataPose(const Pose data_pose)
812 {
813 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
814 SetDataPoseImpl(data_pose);
815 }
816#endif
817
818#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
819 void Entity::SetDataTicksFrozen(const int data_ticks_frozen)
820 {
821 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
822 metadata["data_ticks_frozen"] = data_ticks_frozen;
823 }
824#endif
825
826
828 {
829 std::shared_lock<std::shared_mutex> lock(entity_mutex);
830 return entity_id;
831 }
832
834 {
835 std::shared_lock<std::shared_mutex> lock(entity_mutex);
836 return uuid;
837 }
838
840 {
841 std::shared_lock<std::shared_mutex> lock(entity_mutex);
842 return position;
843 }
844
845 double Entity::GetX() const
846 {
847 std::shared_lock<std::shared_mutex> lock(entity_mutex);
848 return position.x;
849 }
850
851 double Entity::GetY() const
852 {
853 std::shared_lock<std::shared_mutex> lock(entity_mutex);
854 return position.y;
855 }
856
857 double Entity::GetZ() const
858 {
859 std::shared_lock<std::shared_mutex> lock(entity_mutex);
860 return position.z;
861 }
862
863 float Entity::GetYaw() const
864 {
865 std::shared_lock<std::shared_mutex> lock(entity_mutex);
866 return yaw;
867 }
868
869 float Entity::GetPitch() const
870 {
871 std::shared_lock<std::shared_mutex> lock(entity_mutex);
872 return pitch;
873 }
874
876 {
877 std::shared_lock<std::shared_mutex> lock(entity_mutex);
878 return speed;
879 }
880
881 double Entity::GetSpeedX() const
882 {
883 std::shared_lock<std::shared_mutex> lock(entity_mutex);
884 return speed.x;
885 }
886
887 double Entity::GetSpeedY() const
888 {
889 std::shared_lock<std::shared_mutex> lock(entity_mutex);
890 return speed.y;
891 }
892
893 double Entity::GetSpeedZ() const
894 {
895 std::shared_lock<std::shared_mutex> lock(entity_mutex);
896 return speed.z;
897 }
898
900 {
901 std::shared_lock<std::shared_mutex> lock(entity_mutex);
902 return on_ground;
903 }
904
905 std::map<EquipmentSlot, ProtocolCraft::Slot> Entity::GetEquipments() const
906 {
907 std::shared_lock<std::shared_mutex> lock(entity_mutex);
908 return equipments;
909 }
910
912 {
913 std::shared_lock<std::shared_mutex> lock(entity_mutex);
914 return equipments.at(slot);
915 }
916
917 std::vector<EntityEffect> Entity::GetEffects() const
918 {
919 std::shared_lock<std::shared_mutex> lock(entity_mutex);
920 return effects;
921 }
922
923#if USE_GUI
924 std::vector<Renderer::Face> Entity::GetFaces(const bool reset_uptodate_status)
925 {
926 if (faces.size() == 0)
927 {
929 }
930 std::shared_lock<std::shared_mutex> lock(entity_mutex);
932 {
933 for (size_t i = 0; i < faces.size(); ++i)
934 {
935 faces[i].UpdateMatrix(face_descriptors[i].transformations, face_descriptors[i].orientation);
936 }
937 }
938 if (reset_uptodate_status)
939 {
941 }
942 return faces;
943 }
944
946 {
947 std::shared_lock<std::shared_mutex> lock(entity_mutex);
949 }
950#endif
951
952
953 void Entity::SetEntityID(const int entity_id_)
954 {
955 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
956 entity_id = entity_id_;
957 }
958
960 {
961 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
962 uuid = uuid_;
963 }
964
966 {
967 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
968#if USE_GUI
969 if (position_ != position)
970 {
972 for (size_t i = 0; i < faces.size(); ++i)
973 {
974 std::shared_ptr<Renderer::Translation> f = std::static_pointer_cast<Renderer::Translation>(face_descriptors[i].transformations.translations.back());
975 f->x = static_cast<float>(position_.x);
976 f->y = static_cast<float>(position_.y);
977 f->z = static_cast<float>(position_.z);
978 }
979 }
980#endif
981 position = position_;
982 }
983
984 void Entity::SetX(const double x_)
985 {
986 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
987#if USE_GUI
988 if (x_ != position.x)
989 {
991 for (size_t i = 0; i < faces.size(); ++i)
992 {
993 std::static_pointer_cast<Renderer::Translation>(face_descriptors[i].transformations.translations.back())->x = static_cast<float>(x_);
994 }
995 }
996#endif
997 position.x = x_;
998 }
999
1000 void Entity::SetY(const double y_)
1001 {
1002 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1003#if USE_GUI
1004 if (y_ != position.y)
1005 {
1007 for (size_t i = 0; i < faces.size(); ++i)
1008 {
1009 std::static_pointer_cast<Renderer::Translation>(face_descriptors[i].transformations.translations.back())->y = static_cast<float>(y_);
1010 }
1011 }
1012#endif
1013 position.y = y_;
1014 }
1015
1016 void Entity::SetZ(const double z_)
1017 {
1018 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1019#if USE_GUI
1020 if (z_ != position.z)
1021 {
1023 for (size_t i = 0; i < faces.size(); ++i)
1024 {
1025 std::static_pointer_cast<Renderer::Translation>(face_descriptors[i].transformations.translations.back())->z = static_cast<float>(z_);
1026 }
1027 }
1028#endif
1029 position.z = z_;
1030 }
1031
1032 void Entity::SetYaw(const float yaw_)
1033 {
1034 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1035#if USE_GUI
1036 if (yaw_ != yaw)
1037 {
1039 for (size_t i = 0; i < faces.size(); ++i)
1040 {
1041 std::static_pointer_cast<Renderer::Rotation>(face_descriptors[i].transformations.rotations.front())->deg_angle = yaw_;
1042 }
1043 }
1044#endif
1045 yaw = yaw_;
1046 }
1047
1048 void Entity::SetPitch(const float pitch_)
1049 {
1050 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1051#if USE_GUI
1052 if (pitch_ != pitch)
1053 {
1055 for (size_t i = 0; i < faces.size(); ++i)
1056 {
1057 std::static_pointer_cast<Renderer::Rotation>(face_descriptors[i].transformations.rotations.back())->deg_angle = pitch_;
1058 }
1059 }
1060#endif
1061 pitch = pitch_;
1062 }
1063
1065 {
1066 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1067 speed = speed_;
1068 }
1069
1070 void Entity::SetSpeedX(const double speed_x_)
1071 {
1072 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1073 speed.x = speed_x_;
1074 }
1075
1076 void Entity::SetSpeedY(const double speed_y_)
1077 {
1078 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1079 speed.y = speed_y_;
1080 }
1081
1082 void Entity::SetSpeedZ(const double speed_z_)
1083 {
1084 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1085 speed.z = speed_z_;
1086 }
1087
1088 void Entity::SetOnGround(const bool on_ground_)
1089 {
1090 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1091 on_ground = on_ground_;
1092 }
1093
1095 {
1096 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1097 equipments.at(slot) = item;
1098 }
1099
1100 void Entity::SetEffects(const std::vector<EntityEffect>& effects_)
1101 {
1102 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1103 effects = effects_;
1104 }
1105
1107 {
1108 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1109 for (auto it = effects.begin(); it != effects.end();)
1110 {
1111 if (it->type == type)
1112 {
1113 it = effects.erase(it);
1114 }
1115 else
1116 {
1117 ++it;
1118 }
1119 }
1120 }
1121
1123 {
1124 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1125 // First, remove any instance of this type of effect on the entity
1126 for (auto it = effects.begin(); it != effects.end();)
1127 {
1128 if (it->type == effect.type)
1129 {
1130 it = effects.erase(it);
1131 }
1132 else
1133 {
1134 ++it;
1135 }
1136 }
1137
1138 // Then add the new one
1139 effects.push_back(effect);
1140 }
1141
1142#if USE_GUI
1143 void Entity::SetAreRenderedFacesUpToDate(const bool should_be_updated_)
1144 {
1145 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1146 are_rendered_faces_up_to_date = should_be_updated_;
1147 }
1148#endif
1149
1150
1152 {
1154
1155 {
1156 std::shared_lock<std::shared_mutex> lock(entity_mutex);
1157 output["id"] = entity_id;
1158 output["position"] = position.Serialize();
1159 output["yaw"] = yaw;
1160 output["pitch"] = pitch;
1161 output["speed"] = speed.Serialize();
1162 output["on_ground"] = on_ground;
1163 output["equipment"] = ProtocolCraft::Json::Value();
1164 for (auto& p : equipments)
1165 {
1166 output["equipment"][std::to_string(static_cast<int>(p.first))] = p.second.Serialize();
1167 }
1168 }
1169
1170 output["metadata"] = ProtocolCraft::Json::Value();
1171
1172 output["metadata"]["data_shared_flags_id"] = GetDataSharedFlagsId();
1173 output["metadata"]["data_air_supply_id"] = GetDataAirSupplyId();
1174#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1175 output["metadata"]["data_custom_name"] = GetDataCustomName() ? GetDataCustomName().value().Serialize() : ProtocolCraft::Json::Value();
1176#else
1177 output["metadata"]["data_custom_name"] = GetDataCustomName();
1178#endif
1179 output["metadata"]["data_custom_name_visible"] = GetDataCustomNameVisible();
1180 output["metadata"]["data_silent"] = GetDataSilent();
1181 output["metadata"]["data_no_gravity"] = GetDataNoGravity();
1182#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1183 output["metadata"]["data_pose"] = static_cast<int>(GetDataPose());
1184#endif
1185#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
1186 output["metadata"]["data_ticks_frozen"] = GetDataTicksFrozen();
1187#endif
1188
1189 return output;
1190 }
1191
1193 {
1194 return false;
1195 }
1196
1198 {
1199 return false;
1200 }
1201
1203 {
1204 return false;
1205 }
1206
1208 {
1209 return false;
1210 }
1211
1212 bool Entity::IsAnimal() const
1213 {
1214 return false;
1215 }
1216
1218 {
1219 return false;
1220 }
1221
1223 {
1224 return false;
1225 }
1226
1227#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
1229 {
1230 return false;
1231 }
1232#endif
1233
1234#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
1236 {
1237 return false;
1238 }
1239#endif
1240
1242 {
1243 return false;
1244 }
1245
1247 {
1248 return false;
1249 }
1250
1252 {
1253 return false;
1254 }
1255
1257 {
1258 return false;
1259 }
1260
1262 {
1263 return false;
1264 }
1265
1266 bool Entity::IsMob() const
1267 {
1268 return false;
1269 }
1270
1272 {
1273 return false;
1274 }
1275
1276#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1278 {
1279 return false;
1280 }
1281#endif
1282
1284 {
1285 return false;
1286 }
1287
1289 {
1290 return false;
1291 }
1292
1294 {
1295 return false;
1296 }
1297
1299 {
1300 return false;
1301 }
1302
1304 {
1305 return false;
1306 }
1307
1309 {
1310 return false;
1311 }
1312
1313#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
1315 {
1316 return false;
1317 }
1318#endif
1319
1321 {
1322 return false;
1323 }
1324
1326 {
1327 return false;
1328 }
1329
1330#if PROTOCOL_VERSION > 736 /* > 1.16.1 */
1332 {
1333 return false;
1334 }
1335#endif
1336
1338 {
1339 return false;
1340 }
1341
1342#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
1344 {
1345 return false;
1346 }
1347#endif
1348
1350 {
1351 return false;
1352 }
1353
1354#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1355 bool Entity::IsRaider() const
1356 {
1357 return false;
1358 }
1359#endif
1360
1362 {
1363 return false;
1364 }
1365
1367 {
1368 return false;
1369 }
1370
1371#if PROTOCOL_VERSION > 477 /* > 1.14 */
1373 {
1374 return false;
1375 }
1376#endif
1377
1379 {
1380 return false;
1381 }
1382
1384 {
1385 return false;
1386 }
1387
1388#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1390 {
1391 return false;
1392 }
1393#endif
1394
1396 {
1397 return false;
1398 }
1399
1400#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1402 {
1403 return false;
1404 }
1405#endif
1406
1407#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
1409 {
1410 return false;
1411 }
1412#endif
1413
1414#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
1416 {
1417 return false;
1418 }
1419
1421 {
1422 return false;
1423 }
1424
1426 {
1427 return false;
1428 }
1429
1430 bool Entity::IsBoat() const
1431 {
1432 return false;
1433 }
1434
1436 {
1437 return false;
1438 }
1439
1441 {
1442 return false;
1443 }
1444
1445 bool Entity::IsRaft() const
1446 {
1447 return false;
1448 }
1449#endif
1450
1451
1452 std::shared_ptr<Entity> Entity::CreateEntity(const EntityType type)
1453 {
1454 switch (type)
1455 {
1456 case EntityType::None:
1457 return nullptr;
1458#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
1459 case EntityType::Allay:
1460 return std::make_shared<AllayEntity>();
1461#endif
1463 return std::make_shared<AreaEffectCloudEntity>();
1464#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1466 return std::make_shared<ArmadilloEntity>();
1467#endif
1469 return std::make_shared<ArmorStandEntity>();
1470 case EntityType::Arrow:
1471 return std::make_shared<ArrowEntity>();
1472#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
1474 return std::make_shared<AxolotlEntity>();
1475#endif
1476 case EntityType::Bat:
1477 return std::make_shared<BatEntity>();
1478#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
1479 case EntityType::Bee:
1480 return std::make_shared<BeeEntity>();
1481#endif
1482 case EntityType::Blaze:
1483 return std::make_shared<BlazeEntity>();
1484#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
1485 case EntityType::Boat:
1486 return std::make_shared<BoatEntity>();
1487#else
1497 return std::make_shared<BoatEntity>(type);
1498#endif
1499#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1500 case EntityType::Bogged:
1501 return std::make_shared<BoggedEntity>();
1502#endif
1503#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
1504 case EntityType::Breeze:
1505 return std::make_shared<BreezeEntity>();
1506#endif
1507#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1509 return std::make_shared<BreezeWindChargeEntity>();
1510#endif
1511#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
1512#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
1513 case EntityType::ChestBoat:
1514 return std::make_shared<ChestBoatEntity>();
1515#else
1525 return std::make_shared<ChestBoatEntity>(type);
1526#endif
1527#endif
1528#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
1530 return std::make_shared<ChestRaftEntity>(type);
1531#endif
1532#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1533 case EntityType::Cat:
1534 return std::make_shared<CatEntity>();
1535#endif
1536#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
1537 case EntityType::Camel:
1538 return std::make_shared<CamelEntity>();
1539#endif
1541 return std::make_shared<CaveSpiderEntity>();
1543 return std::make_shared<ChickenEntity>();
1544#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1545 case EntityType::Cod:
1546 return std::make_shared<CodEntity>();
1547#endif
1548 case EntityType::Cow:
1549 return std::make_shared<CowEntity>();
1550#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
1552 return std::make_shared<CreakingEntity>();
1553#endif
1554#if PROTOCOL_VERSION > 767 /* > 1.21.1 */ && PROTOCOL_VERSION < 769 /* < 1.21.4 */
1555 case EntityType::CreakingTransient:
1556 return std::make_shared<CreakingTransientEntity>();
1557#endif
1559 return std::make_shared<CreeperEntity>();
1560#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
1562 return std::make_shared<DisplayBlockDisplayEntity>();
1564 return std::make_shared<DisplayItemDisplayEntity>();
1566 return std::make_shared<DisplayTextDisplayEntity>();
1567#endif
1568#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1570 return std::make_shared<DolphinEntity>();
1571#endif
1572 case EntityType::Donkey:
1573 return std::make_shared<DonkeyEntity>();
1575 return std::make_shared<DragonFireballEntity>();
1576#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1578 return std::make_shared<DrownedEntity>();
1579#endif
1581 return std::make_shared<ElderGuardianEntity>();
1583 return std::make_shared<EndCrystalEntity>();
1585 return std::make_shared<EnderDragonEntity>();
1587 return std::make_shared<EnderManEntity>();
1589 return std::make_shared<EndermiteEntity>();
1590 case EntityType::Evoker:
1591 return std::make_shared<EvokerEntity>();
1593 return std::make_shared<EvokerFangsEntity>();
1595 return std::make_shared<ExperienceOrbEntity>();
1597 return std::make_shared<EyeOfEnderEntity>();
1599 return std::make_shared<FallingBlockEntity>();
1601 return std::make_shared<FireworkRocketEntity>();
1602#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1603 case EntityType::Fox:
1604 return std::make_shared<FoxEntity>();
1605#endif
1606#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
1607 case EntityType::Frog:
1608 return std::make_shared<FrogEntity>();
1609#endif
1610 case EntityType::Ghast:
1611 return std::make_shared<GhastEntity>();
1612 case EntityType::Giant:
1613 return std::make_shared<GiantEntity>();
1614#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
1616 return std::make_shared<GlowItemFrameEntity>();
1618 return std::make_shared<GlowSquidEntity>();
1619 case EntityType::Goat:
1620 return std::make_shared<GoatEntity>();
1621#endif
1623 return std::make_shared<GuardianEntity>();
1624#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1625 case EntityType::Hoglin:
1626 return std::make_shared<HoglinEntity>();
1627#endif
1628 case EntityType::Horse:
1629 return std::make_shared<HorseEntity>();
1630 case EntityType::Husk:
1631 return std::make_shared<HuskEntity>();
1633 return std::make_shared<IllusionerEntity>();
1634#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
1636 return std::make_shared<InteractionEntity>();
1637#endif
1639 return std::make_shared<IronGolemEntity>();
1641 return std::make_shared<ItemEntity>();
1643 return std::make_shared<ItemFrameEntity>();
1644#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
1646 return std::make_shared<OminousItemSpawnerEntity>();
1647#endif
1649 return std::make_shared<LargeFireballEntity>();
1651 return std::make_shared<LeashFenceKnotEntity>();
1652#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1654 return std::make_shared<LightningBoltEntity>();
1655#endif
1656 case EntityType::Llama:
1657 return std::make_shared<LlamaEntity>();
1659 return std::make_shared<LlamaSpitEntity>();
1661 return std::make_shared<MagmaCubeEntity>();
1662#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
1663 case EntityType::Marker:
1664 return std::make_shared<MarkerEntity>();
1665#endif
1667 return std::make_shared<MinecartEntity>();
1669 return std::make_shared<MinecartChestEntity>();
1671 return std::make_shared<MinecartCommandBlockEntity>();
1673 return std::make_shared<MinecartFurnaceEntity>();
1675 return std::make_shared<MinecartHopperEntity>();
1677 return std::make_shared<MinecartSpawnerEntity>();
1679 return std::make_shared<MinecartTNTEntity>();
1680 case EntityType::Mule:
1681 return std::make_shared<MuleEntity>();
1683 return std::make_shared<MushroomCowEntity>();
1684 case EntityType::Ocelot:
1685 return std::make_shared<OcelotEntity>();
1687 return std::make_shared<PaintingEntity>();
1688#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1689 case EntityType::Panda:
1690 return std::make_shared<PandaEntity>();
1691#endif
1692 case EntityType::Parrot:
1693 return std::make_shared<ParrotEntity>();
1694#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1696 return std::make_shared<PhantomEntity>();
1697#endif
1698 case EntityType::Pig:
1699 return std::make_shared<PigEntity>();
1700#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1701 case EntityType::Piglin:
1702 return std::make_shared<PiglinEntity>();
1703#endif
1704#if PROTOCOL_VERSION > 736 /* > 1.16.1 */
1706 return std::make_shared<PiglinBruteEntity>();
1707#endif
1708#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1710 return std::make_shared<PillagerEntity>();
1711#endif
1713 return std::make_shared<PolarBearEntity>();
1715 return std::make_shared<PrimedTntEntity>();
1716#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1718 return std::make_shared<PufferfishEntity>();
1719#endif
1720 case EntityType::Rabbit:
1721 return std::make_shared<RabbitEntity>();
1722#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
1723 case EntityType::Raft:
1724 return std::make_shared<RaftEntity>(type);
1725#endif
1726#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1728 return std::make_shared<RavagerEntity>();
1729#endif
1730#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1731 case EntityType::Salmon:
1732 return std::make_shared<SalmonEntity>();
1733#endif
1734 case EntityType::Sheep:
1735 return std::make_shared<SheepEntity>();
1737 return std::make_shared<ShulkerEntity>();
1739 return std::make_shared<ShulkerBulletEntity>();
1741 return std::make_shared<SilverfishEntity>();
1743 return std::make_shared<SkeletonEntity>();
1745 return std::make_shared<SkeletonHorseEntity>();
1746 case EntityType::Slime:
1747 return std::make_shared<SlimeEntity>();
1749 return std::make_shared<SmallFireballEntity>();
1750#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
1752 return std::make_shared<SnifferEntity>();
1753#endif
1755 return std::make_shared<SnowGolemEntity>();
1757 return std::make_shared<SnowballEntity>();
1759 return std::make_shared<SpectralArrowEntity>();
1760 case EntityType::Spider:
1761 return std::make_shared<SpiderEntity>();
1762 case EntityType::Squid:
1763 return std::make_shared<SquidEntity>();
1764 case EntityType::Stray:
1765 return std::make_shared<StrayEntity>();
1766#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1768 return std::make_shared<StriderEntity>();
1769#endif
1770#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
1772 return std::make_shared<TadpoleEntity>();
1773#endif
1775 return std::make_shared<ThrownEggEntity>();
1777 return std::make_shared<ThrownEnderpearlEntity>();
1779 return std::make_shared<ThrownExperienceBottleEntity>();
1780#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
1781 case EntityType::ThrownPotion:
1782 return std::make_shared<ThrownPotionEntity>();
1783#else
1785 return std::make_shared<ThrownSplashPotionEntity>();
1787 return std::make_shared<ThrownLingeringPotionEntity>();
1788#endif
1789#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1791 return std::make_shared<ThrownTridentEntity>();
1792#endif
1793#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1795 return std::make_shared<TraderLlamaEntity>();
1796#endif
1797#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1799 return std::make_shared<TropicalFishEntity>();
1800#endif
1801#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1802 case EntityType::Turtle:
1803 return std::make_shared<TurtleEntity>();
1804#endif
1805 case EntityType::Vex:
1806 return std::make_shared<VexEntity>();
1808 return std::make_shared<VillagerEntity>();
1810 return std::make_shared<VindicatorEntity>();
1811#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
1813 return std::make_shared<WanderingTraderEntity>();
1814#endif
1815#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
1816 case EntityType::Warden:
1817 return std::make_shared<WardenEntity>();
1818#endif
1819#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
1821 return std::make_shared<WindChargeEntity>();
1822#endif
1823 case EntityType::Witch:
1824 return std::make_shared<WitchEntity>();
1826 return std::make_shared<WitherBossEntity>();
1828 return std::make_shared<WitherSkeletonEntity>();
1830 return std::make_shared<WitherSkullEntity>();
1831 case EntityType::Wolf:
1832 return std::make_shared<WolfEntity>();
1833#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1834 case EntityType::Zoglin:
1835 return std::make_shared<ZoglinEntity>();
1836#endif
1837 case EntityType::Zombie:
1838 return std::make_shared<ZombieEntity>();
1840 return std::make_shared<ZombieHorseEntity>();
1842 return std::make_shared<ZombieVillagerEntity>();
1843#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
1845 return std::make_shared<ZombifiedPiglinEntity>();
1846#else
1847 case EntityType::PigZombie:
1848 return std::make_shared<PigZombieEntity>();
1849#endif
1850 case EntityType::Player:
1851 return std::make_shared<PlayerEntity>();
1852#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1854 return std::make_shared<FishingHookEntity>();
1855#endif
1856 default:
1857 return nullptr;
1858 }
1859 }
1860
1861#if PROTOCOL_VERSION < 458 /* < 1.14 */
1862 std::shared_ptr<Entity> Entity::CreateObjectEntity(const ObjectEntityType type)
1863 {
1864 switch (type)
1865 {
1866 case ObjectEntityType::None:
1867 return nullptr;
1868 case ObjectEntityType::Boat:
1869 return std::make_shared<BoatEntity>();
1870 case ObjectEntityType::ItemEntity:
1871 return std::make_shared<ItemEntity>();
1872 case ObjectEntityType::AreaEffectCloud:
1873 return std::make_shared<AreaEffectCloudEntity>();
1874 case ObjectEntityType::PrimedTnt:
1875 return std::make_shared<PrimedTntEntity>();
1876 case ObjectEntityType::EndCrystal:
1877 return std::make_shared<EndCrystalEntity>();
1878 case ObjectEntityType::Arrow:
1879 return std::make_shared<ArrowEntity>();
1880 case ObjectEntityType::Snowball:
1881 return std::make_shared<SnowballEntity>();
1882 case ObjectEntityType::ThrownEgg:
1883 return std::make_shared<ThrownEggEntity>();
1884 case ObjectEntityType::LargeFireball:
1885 return std::make_shared<LargeFireballEntity>();
1886 case ObjectEntityType::SmallFireball:
1887 return std::make_shared<SmallFireballEntity>();
1888 case ObjectEntityType::ThrownEnderpearl:
1889 return std::make_shared<ThrownEnderpearlEntity>();
1890 case ObjectEntityType::WitherSkull:
1891 return std::make_shared<WitherSkullEntity>();
1892 case ObjectEntityType::ShulkerBullet:
1893 return std::make_shared<ShulkerBulletEntity>();
1894 case ObjectEntityType::LlamaSpit:
1895 return std::make_shared<LlamaSpitEntity>();
1896 case ObjectEntityType::FallingBlockEntity:
1897 return std::make_shared<FallingBlockEntity>();
1898 case ObjectEntityType::ItemFrame:
1899 return std::make_shared<ItemFrameEntity>();
1900 case ObjectEntityType::EyeOfEnder:
1901 return std::make_shared<EyeOfEnderEntity>();
1902 case ObjectEntityType::ThrownPotion:
1903 return std::make_shared<ThrownPotionEntity>();
1904 case ObjectEntityType::ThrownExperienceBottle:
1905 return std::make_shared<ThrownExperienceBottleEntity>();
1906 case ObjectEntityType::FireworkRocketEntity:
1907 return std::make_shared<FireworkRocketEntity>();
1908 case ObjectEntityType::LeashFenceKnotEntity:
1909 return std::make_shared<LeashFenceKnotEntity>();
1910 case ObjectEntityType::ArmorStand:
1911 return std::make_shared<ArmorStandEntity>();
1912 case ObjectEntityType::EvokerFangs:
1913 return std::make_shared<EvokerFangsEntity>();
1914 case ObjectEntityType::FishingHook:
1915 return std::make_shared<FishingHookEntity>();
1916 case ObjectEntityType::SpectralArrow:
1917 return std::make_shared<SpectralArrowEntity>();
1918 case ObjectEntityType::DragonFireball:
1919 return std::make_shared<DragonFireballEntity>();
1920#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
1921 case ObjectEntityType::ThrownTrident:
1922 return std::make_shared<ThrownTridentEntity>();
1923#endif
1924 default:
1925 return nullptr;
1926 }
1927 }
1928#endif
1929
1930#if USE_GUI
1932 {
1933 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
1935
1936 // Generate default faces
1937 face_descriptors = std::vector<FaceDescriptor>(6);
1938 faces = std::vector<Renderer::Face>(6);
1939 const double half_height = GetHeightImpl() / 2.0;
1940 const double half_width = GetWidthImpl() / 2.0;
1941 for (int i = 0; i < 6; ++i)
1942 {
1943 face_descriptors[i].orientation = static_cast<Orientation>(i);
1944 face_descriptors[i].texture_names = { "" };
1945 face_descriptors[i].cullface_direction = Orientation::None;
1946 face_descriptors[i].use_tintindexes = { false };
1947
1948 // Base entity scale
1949 face_descriptors[i].transformations.scales.push_back(std::make_shared<Renderer::Scale>(
1950 static_cast<float>(half_width),
1951 static_cast<float>(half_height),
1952 static_cast<float>(half_width)
1953 ));
1954 // Base translation
1955 face_descriptors[i].transformations.translations.push_back(std::make_shared<Renderer::Translation>(0.0f, static_cast<float>(half_height), 0.0f));
1956 // Entity pos translation
1957 face_descriptors[i].transformations.translations.push_back(std::make_shared<Renderer::Translation>(
1958 static_cast<float>(position.x),
1959 static_cast<float>(position.y),
1960 static_cast<float>(position.z))
1961 );
1962 // Entity yaw/pitch rotation
1963 face_descriptors[i].transformations.rotations.push_back(std::make_shared<Renderer::Rotation>(0.0f, 1.0f, 0.0f, yaw));
1964 face_descriptors[i].transformations.rotations.push_back(std::make_shared<Renderer::Rotation>(1.0f, 0.0f, 0.0f, pitch));
1965
1966 face_descriptors[i].transformations.rotation = 0;
1967
1968 faces[i] = Renderer::Face(face_descriptors[i].transformations, face_descriptors[i].orientation);
1969 faces[i].SetDisplayBackface(false);
1970 faces[i].SetTextureMultipliers({ 0xFFFFFFFF, 0xFFFFFFFF });
1971
1972 // Extract texture info from the atlas
1973 std::array<unsigned short, 4> texture_pos = { 0, 0, 0, 0 };
1974 std::array<unsigned short, 4> texture_size = { 0, 0, 0, 0 };
1975 std::array<Renderer::Transparency, 2> transparencies = { Renderer::Transparency::Opaque, Renderer::Transparency::Opaque };
1976 std::array<Renderer::Animation, 2> animated = { Renderer::Animation::Static, Renderer::Animation::Static };
1977
1978 for (int j = 0; j < std::min(2, static_cast<int>(face_descriptors[i].texture_names.size())); ++j)
1979 {
1980 const Renderer::TextureData& texture_data = atlas->GetData(face_descriptors[i].texture_names[j]);
1981 std::tie(texture_pos[2 * j + 0], texture_pos[2 * j + 1]) = texture_data.position;
1982 std::tie(texture_size[2 * j + 0], texture_size[2 * j + 1]) = texture_data.size;
1983 transparencies[j] = texture_data.transparency;
1984 animated[j] = texture_data.animation;
1985 }
1986
1987 // Main texture coords in the atlas
1988 std::array<float, 4> coords = faces[i].GetTextureCoords(false);
1989 unsigned short height_normalizer = animated[0] == Renderer::Animation::Animated ? texture_size[0] : texture_size[1];
1990 coords[0] = (texture_pos[0] + coords[0] / 16.0f * texture_size[0]) / atlas->GetWidth();
1991 coords[1] = (texture_pos[1] + coords[1] / 16.0f * height_normalizer) / atlas->GetHeight();
1992 coords[2] = (texture_pos[0] + coords[2] / 16.0f * texture_size[0]) / atlas->GetWidth();
1993 coords[3] = (texture_pos[1] + coords[3] / 16.0f * height_normalizer) / atlas->GetHeight();
1994 faces[i].SetTextureCoords(coords, false);
1995
1996 // Overlay texture coords in the atlas if existing
1997 if (face_descriptors[i].texture_names.size() > 1)
1998 {
1999 coords = faces[i].GetTextureCoords(true);
2000 height_normalizer = animated[1] == Renderer::Animation::Animated ? texture_size[2] : texture_size[3];
2001 coords[0] = (texture_pos[2] + coords[0] / 16.0f * texture_size[2]) / atlas->GetWidth();
2002 coords[1] = (texture_pos[3] + coords[1] / 16.0f * height_normalizer) / atlas->GetHeight();
2003 coords[2] = (texture_pos[2] + coords[2] / 16.0f * texture_size[2]) / atlas->GetWidth();
2004 coords[3] = (texture_pos[3] + coords[3] / 16.0f * height_normalizer) / atlas->GetHeight();
2005 faces[i].SetTextureCoords(coords, true);
2006 }
2007
2008 faces[i].SetTransparencyData(transparencies[0]);
2009 }
2011 }
2012
2014 {
2016 const double half_width = GetWidthImpl() / 2.0;
2017 const double half_height = GetHeightImpl() / 2.0;
2018 for (size_t i = 0; i < faces.size(); ++i)
2019 {
2020 std::static_pointer_cast<Renderer::Scale>(face_descriptors[i].transformations.scales.back())->axis_x = static_cast<float>(half_width);
2021 std::static_pointer_cast<Renderer::Scale>(face_descriptors[i].transformations.scales.back())->axis_y = static_cast<float>(half_height);
2022 std::static_pointer_cast<Renderer::Scale>(face_descriptors[i].transformations.scales.back())->axis_z = static_cast<float>(half_width);
2023 std::static_pointer_cast<Renderer::Translation>(face_descriptors[i].transformations.translations.front())->y = static_cast<float>(half_height);
2024 }
2025 }
2026#endif
2027
2029 {
2030 return std::any_cast<char>(metadata.at("data_shared_flags_id"));
2031 }
2032
2034 {
2035 return (GetDataSharedFlagsIdImpl() >> static_cast<char>(id)) & 0x01;
2036 }
2037
2038 void Entity::SetDataSharedFlagsIdImpl(const char data_shared_flags_id)
2039 {
2040 metadata["data_shared_flags_id"] = data_shared_flags_id;
2041 }
2042
2044 {
2045 char current_value = GetDataSharedFlagsIdImpl();
2046 // Set the bit to 0
2047 const char mask = 0x01 << static_cast<char>(id);
2048 current_value &= ~mask;
2049 // Set the bit to b
2050 current_value |= b << static_cast<char>(id);
2051 SetDataSharedFlagsIdImpl(current_value);
2052 }
2053
2054#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
2056 {
2057 return std::any_cast<Pose>(metadata.at("data_pose"));
2058 }
2059
2060 void Entity::SetDataPoseImpl(const Pose data_pose)
2061 {
2062 metadata["data_pose"] = data_pose;
2063#if USE_GUI
2064 OnSizeUpdated();
2065#endif
2066 }
2067#endif
2068
2070 {
2071 const double half_width = GetWidthImpl() / 2.0;
2072 const double half_height = GetHeightImpl() / 2.0;
2073 return AABB(Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
2074 }
2075
2077 {
2078 return -1.0;
2079 }
2080
2082 {
2083 return -1.0;
2084 }
2085}
#define LOG_ERROR(osstream)
Definition Logger.hpp:45
const Renderer::Atlas * GetAtlas() const
static AssetsManager & getInstance()
int GetDataTicksFrozen() const
Definition Entity.cpp:752
void OnSizeUpdated()
Definition Entity.cpp:2013
bool GetOnGround() const
Definition Entity.cpp:899
ProtocolCraft::UUID uuid
Definition Entity.hpp:262
virtual double GetHeightImpl() const
Definition Entity.cpp:2081
void SetDataSilent(const bool data_silent)
Definition Entity.cpp:798
virtual bool IsFlyingMob() const
Definition Entity.cpp:1283
virtual ProtocolCraft::Json::Value Serialize() const
Definition Entity.cpp:1151
void SetDataPose(const Pose data_pose)
Definition Entity.cpp:811
std::map< EquipmentSlot, ProtocolCraft::Slot > equipments
Items on this entity.
Definition Entity.hpp:271
virtual void SetY(const double y_)
Definition Entity.cpp:1000
double GetZ() const
Definition Entity.cpp:857
virtual bool IsAbstractChestedHorse() const
Definition Entity.cpp:1256
virtual bool IsWaterAnimal() const
Definition Entity.cpp:1251
void SetEffects(const std::vector< EntityEffect > &effects_)
Definition Entity.cpp:1100
void SetDataAirSupplyId(const int data_air_supply_id)
Definition Entity.cpp:772
void SetDataSharedFlagsIdImpl(const char data_shared_flags_id)
Definition Entity.cpp:2038
Vector3< double > GetSpeed() const
Definition Entity.cpp:875
void SetUUID(const ProtocolCraft::UUID &uuid_)
Definition Entity.cpp:959
std::map< EquipmentSlot, ProtocolCraft::Slot > GetEquipments() const
Definition Entity.cpp:905
Vector3< double > speed
Definition Entity.hpp:266
virtual bool IsAbstractArrow() const
Definition Entity.cpp:1207
virtual bool IsAbstractSkeleton() const
Definition Entity.cpp:1361
virtual bool IsAbstractPiglin() const
Definition Entity.cpp:1331
int GetEntityID() const
Definition Entity.cpp:827
virtual bool IsLocalPlayer() const
Definition Entity.cpp:1192
double GetHeight() const
Definition Entity.cpp:343
std::vector< EntityEffect > effects
Definition Entity.hpp:272
char GetDataSharedFlagsIdImpl() const
Definition Entity.cpp:2028
virtual bool IsRemotePlayer() const
Definition Entity.cpp:1197
bool GetDataSilent() const
Definition Entity.cpp:731
void SetDataPoseImpl(const Pose data_pose)
Definition Entity.cpp:2060
double GetSpeedX() const
Definition Entity.cpp:881
virtual bool IsAbstractBoat() const
Definition Entity.cpp:1415
virtual bool IsAbstractHurtingProjectile() const
Definition Entity.cpp:1261
ProtocolCraft::UUID GetUUID() const
Definition Entity.cpp:833
double GetY() const
Definition Entity.cpp:851
virtual bool IsAbstractSchoolingFish() const
Definition Entity.cpp:1246
void SetSpeed(const Vector3< double > &speed_)
Definition Entity.cpp:1064
virtual bool IsHangingEntity() const
Definition Entity.cpp:1298
bool GetAreRenderedFacesUpToDate() const
Definition Entity.cpp:945
virtual void SetMetadataValue(const int index, const std::any &value)
Definition Entity.cpp:678
virtual bool IsSpellcasterIllager() const
Definition Entity.cpp:1271
virtual bool IsBlockAttachedEntity() const
Definition Entity.cpp:1408
static std::shared_ptr< Entity > CreateEntity(const EntityType type)
Definition Entity.cpp:1452
virtual bool IsAbstractThrownPotion() const
Definition Entity.cpp:1343
AABB GetCollider() const
Definition Entity.cpp:331
static const std::array< std::string, metadata_count > metadata_names
Definition Entity.hpp:61
bool are_rendered_faces_up_to_date
Definition Entity.hpp:281
virtual double GetWidthImpl() const
Definition Entity.cpp:2076
virtual void SetZ(const double z_)
Definition Entity.cpp:1016
void SetEquipment(const EquipmentSlot slot, const ProtocolCraft::Slot &item)
Definition Entity.cpp:1094
std::vector< Renderer::Face > faces
Definition Entity.hpp:279
virtual bool IsLivingEntity() const
Definition Entity.cpp:1202
virtual bool IsChestBoat() const
Definition Entity.cpp:1435
double GetWidth() const
Definition Entity.cpp:337
virtual ~Entity()
Definition Entity.cpp:325
virtual bool IsPatrollingMonster() const
Definition Entity.cpp:1389
virtual bool IsProjectile() const
Definition Entity.cpp:1277
int GetDataAirSupplyId() const
Definition Entity.cpp:705
std::vector< EntityEffect > GetEffects() const
Definition Entity.cpp:917
virtual bool IsAbstractMinecart() const
Definition Entity.cpp:1308
virtual bool IsDisplay() const
Definition Entity.cpp:1228
void SetSpeedY(const double speed_y_)
Definition Entity.cpp:1076
virtual bool IsAbstractMinecartContainer() const
Definition Entity.cpp:1320
void SetDataCustomName(const std::optional< ProtocolCraft::Chat > &data_custom_name)
Definition Entity.cpp:779
virtual bool IsAmbientCreature() const
Definition Entity.cpp:1217
virtual bool IsFireball() const
Definition Entity.cpp:1303
Pose GetDataPose() const
Definition Entity.cpp:744
std::shared_mutex entity_mutex
Definition Entity.hpp:259
void SetOnGround(const bool on_ground_)
Definition Entity.cpp:1088
virtual bool IsAnimal() const
Definition Entity.cpp:1212
std::vector< Renderer::Face > GetFaces(const bool reset_uptodate_status)
Definition Entity.cpp:924
void SetSpeedX(const double speed_x_)
Definition Entity.cpp:1070
virtual bool IsAbstractFish() const
Definition Entity.cpp:1349
virtual bool IsAgeableMob() const
Definition Entity.cpp:1378
virtual void SetX(const double x_)
Definition Entity.cpp:984
virtual bool IsAbstractVillager() const
Definition Entity.cpp:1372
virtual void InitializeFaces()
Definition Entity.cpp:1931
virtual bool IsThrowableProjectile() const
Definition Entity.cpp:1395
virtual bool IsAbstractWindCharge() const
Definition Entity.cpp:1401
AABB GetColliderImpl() const
Definition Entity.cpp:2069
void SetDataSharedFlagsId(const char data_shared_flags_id)
Definition Entity.cpp:760
char GetDataSharedFlagsId() const
Definition Entity.cpp:693
virtual bool IsMob() const
Definition Entity.cpp:1266
virtual void SetYaw(const float yaw_)
Definition Entity.cpp:1032
virtual bool IsPathfinderMob() const
Definition Entity.cpp:1383
ProtocolCraft::Slot GetEquipment(const EquipmentSlot slot) const
Definition Entity.cpp:911
void SetDataCustomNameVisible(const bool data_custom_name_visible)
Definition Entity.cpp:792
virtual bool IsVehicle() const
Definition Entity.cpp:1235
double GetSpeedY() const
Definition Entity.cpp:887
double GetSpeedZ() const
Definition Entity.cpp:893
Pose GetDataPoseImpl() const
Definition Entity.cpp:2055
virtual bool IsRaider() const
Definition Entity.cpp:1355
virtual void SetPitch(const float pitch_)
Definition Entity.cpp:1048
virtual bool IsThrowableItemProjectile() const
Definition Entity.cpp:1366
void SetEntityID(const int entity_id_)
Definition Entity.cpp:953
virtual bool IsMonster() const
Definition Entity.cpp:1222
virtual bool IsTamableAnimal() const
Definition Entity.cpp:1241
void SetDataNoGravity(const bool data_no_gravity)
Definition Entity.cpp:804
bool GetDataCustomNameVisible() const
Definition Entity.cpp:725
virtual bool IsAbstractIllager() const
Definition Entity.cpp:1337
virtual bool IsAbstractHorse() const
Definition Entity.cpp:1288
std::map< std::string, std::any > metadata
Definition Entity.hpp:274
Vector3< double > position
Definition Entity.hpp:263
virtual bool IsAbstractCow() const
Definition Entity.cpp:1314
virtual bool IsAbstractChestBoat() const
Definition Entity.cpp:1420
void AddEffect(const EntityEffect &effect)
Definition Entity.cpp:1122
virtual bool IsChestRaft() const
Definition Entity.cpp:1440
virtual bool IsAbstractGolem() const
Definition Entity.cpp:1293
float GetYaw() const
Definition Entity.cpp:863
virtual bool IsAgeableWaterCreature() const
Definition Entity.cpp:1425
double GetX() const
Definition Entity.cpp:845
bool GetDataNoGravity() const
Definition Entity.cpp:737
void SetAreRenderedFacesUpToDate(const bool are_rendered_faces_up_to_date_)
Definition Entity.cpp:1143
Vector3< double > GetPosition() const
Definition Entity.cpp:839
void SetSpeedZ(const double speed_z_)
Definition Entity.cpp:1082
static constexpr int metadata_count
Definition Entity.hpp:55
std::vector< FaceDescriptor > face_descriptors
Definition Entity.hpp:278
std::optional< ProtocolCraft::Chat > GetDataCustomName() const
Definition Entity.cpp:712
virtual bool IsBoat() const
Definition Entity.cpp:1430
void LoadMetadataFromRawArray(const std::vector< unsigned char > &data)
Definition Entity.cpp:350
virtual bool IsRaft() const
Definition Entity.cpp:1445
virtual bool IsShoulderRidingEntity() const
Definition Entity.cpp:1325
float GetPitch() const
Definition Entity.cpp:869
void RemoveEffect(const EntityEffectType type)
Definition Entity.cpp:1106
virtual void SetPosition(const Vector3< double > &position_)
Definition Entity.cpp:965
void SetDataTicksFrozen(const int data_ticks_frozen)
Definition Entity.cpp:819
const TextureData & GetData(const std::string &name) const
Definition Atlas.cpp:237
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
virtual void Read(ReadIterator &iter, size_t &length)
Vector3< int > Position
Definition Vector3.hpp:282
EquipmentSlot
Definition Enums.hpp:276
EntityEffectType
Definition Enums.hpp:315
EntitySharedFlagsId
Definition Entity.hpp:40
std::array< unsigned char, 16 > UUID
std::vector< unsigned char >::const_iterator ReadIterator
EntityEffectType type
Definition Entity.hpp:34
std::pair< int, int > position
<Col, Row>
Definition Atlas.hpp:16
std::pair< int, int > size
<Width, Height>
Definition Atlas.hpp:14
ProtocolCraft::Json::Value Serialize() const
Definition Vector3.hpp:267