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