Botcraft 26.3
Loading...
Searching...
No Matches
DataComponents.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
4
30#if PROTOCOL_VERSION < 777 /* < 26.3 */
32#endif
38#if PROTOCOL_VERSION < 774 /* < 1.21.11 */
40#endif
43#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
45#endif
49#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
51#endif
52#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
60#endif
61#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
72#endif
73#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
75#endif
76#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
83#endif
84#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
86#endif
87#if PROTOCOL_VERSION > 776 /* > 26.2 */
93#endif
94
96
97#include <stdexcept>
98
99namespace ProtocolCraft
100{
101 namespace Components
102 {
110#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
113#endif
114#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
116#endif
118#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
120#endif
121#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
123#endif
124#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
126#endif
127#if PROTOCOL_VERSION > 769 /* > 1.21.4 */ && PROTOCOL_VERSION < 777 /* < 26.3 */
128 DEFINE_NETWORK_TYPE(AssetInfo);
129 DEFINE_NETWORK_TYPE(MaterialAssetGroup);
130#endif
133#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
136#endif
137#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
140#endif
141#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
142 DEFINE_NETWORK_TYPE(PossibleEffect);
143#endif
144#if PROTOCOL_VERSION > 776 /* > 26.2 */
147#endif
154
180#if PROTOCOL_VERSION < 777 /* < 26.3 */
181 DEFINE_NETWORK_TYPE(DataComponentTypeMapItemColor);
182#endif
188#if PROTOCOL_VERSION < 774 /* < 1.21.11 */
189 DEFINE_NETWORK_TYPE(DataComponentTypeResourceLocation);
190#endif
193#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
194 DEFINE_NETWORK_TYPE(DataComponentTypeUnbreakable);
195#endif
199#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
201#endif
202#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
210#endif
211#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
222#endif
223#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
225#endif
226#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
233#endif
234#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
236#endif
237#if PROTOCOL_VERSION > 776 /* > 26.2 */
243#endif
244
245 std::shared_ptr<DataComponentType> CreateComponentType(const DataComponentTypes type)
246 {
247 if (type <= DataComponentTypes::None || type >= DataComponentTypes::NUM_DATA_COMPONENT_TYPES)
248 {
249 throw std::runtime_error("Unable to create data component with id: " + std::to_string(static_cast<int>(type)) + ".");
250 }
251
252 switch (type)
253 {
256 return std::make_shared<DataComponentTypeAdventureModePredicate>();
258 return std::make_shared<DataComponentTypeArmorTrim>();
260 return std::make_shared<DataComponentTypeBannerPatternLayers>();
262 return std::make_shared<DataComponentTypeBlockItemStateProperties>();
264 return std::make_shared<DataComponentTypeBoolean>();
266 return std::make_shared<DataComponentTypeBundleContents>();
268 return std::make_shared<DataComponentTypeChargedProjectiles>();
271 return std::make_shared<DataComponentTypeComponent>();
273#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
276#endif
277 return std::make_shared<DataComponentTypeCustomData>();
279 return std::make_shared<DataComponentTypeCustomModelData>();
287 // Component with no network serializer specified fallback to NBT
288 return std::make_shared<DataComponentTypeDefault>();
290 return std::make_shared<DataComponentTypeDyeColor>();
292 return std::make_shared<DataComponentTypeDyedItemColor>();
294 return std::make_shared<DataComponentTypeFireworkExplosion>();
296 return std::make_shared<DataComponentTypeFireworks>();
298 return std::make_shared<DataComponentTypeFoodProperties>();
300 return std::make_shared<DataComponentTypeInstrument>();
306#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
308#endif
309 return std::make_shared<DataComponentTypeInteger>();
311 return std::make_shared<DataComponentTypeItemAttributeModifiers>();
313 return std::make_shared<DataComponentTypeItemContainerContents>();
316 return std::make_shared<DataComponentTypeItemEnchantments>();
318 return std::make_shared<DataComponentTypeItemLore>();
319#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
321 return std::make_shared<DataComponentTypeJukeboxPlayable>();
322#endif
324 return std::make_shared<DataComponentTypeListBeehiveBlockEntityOccupant>();
326 return std::make_shared<DataComponentTypeLodestoneTracker>();
328 return std::make_shared<DataComponentTypeMapId>();
329#if PROTOCOL_VERSION < 777 /* < 26.3 */
330 case DataComponentTypes::MapColor:
331 return std::make_shared<DataComponentTypeMapItemColor>();
332#endif
334 return std::make_shared<DataComponentTypeMapPostProcessing>();
336 return std::make_shared<DataComponentTypePotDecorations>();
338 return std::make_shared<DataComponentTypePotionContents>();
340 return std::make_shared<DataComponentTypeRarity>();
342 return std::make_shared<DataComponentTypeResolvableProfile>();
343#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
345 return std::make_shared<DataComponentTypeDamageResistant>();
348#endif
350#if PROTOCOL_VERSION < 774 /* < 1.21.11 */
351 return std::make_shared<DataComponentTypeResourceLocation>();
352#else
353 return std::make_shared<DataComponentTypeIdentifier>();
354#endif
356 return std::make_shared<DataComponentTypeSuspiciousStewEffects>();
358 return std::make_shared<DataComponentTypeTool>();
359#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
361 return std::make_shared<DataComponentTypeUnbreakable>();
362#else
363 // From custom Unbreakable to Unit in 1.21.5
365#endif
366#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
367 case DataComponentTypes::HideAdditionalTooltip:
368 case DataComponentTypes::HideTooltip:
369#endif
371#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
372 case DataComponentTypes::FireResistant:
373#endif
374#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
376#endif
377 return std::make_shared<DataComponentTypeUnit>();
379 return std::make_shared<DataComponentTypeWritableBookContent>();
381 return std::make_shared<DataComponentTypeWrittenBookContent>();
382#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
384 return std::make_shared<DataComponentTypeConsumable>();
386 return std::make_shared<DataComponentTypeDeathProtection>();
388 return std::make_shared<DataComponentTypeEquippable>();
390 return std::make_shared<DataComponentTypeRepairable>();
392 return std::make_shared<DataComponentTypeUseCooldown>();
394 return std::make_shared<DataComponentTypeUseRemainder>();
395#endif
396#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
398 return std::make_shared<DataComponentTypeBlocksAttacks>();
400 return std::make_shared<DataComponentTypeBreakSound>();
402 return std::make_shared<DataComponentTypePaintingVariant>();
404 return std::make_shared<DataComponentTypeFloat>();
406 return std::make_shared<DataComponentTypeProvidesBannerPattern>();
408 return std::make_shared<DataComponentTypeProvidesTrimMaterial>();
410 return std::make_shared<DataComponentTypeTooltipDisplay>();
412 return std::make_shared<DataComponentTypeWeapon>();
413#if PROTOCOL_VERSION < 775 /* < 26.1 */
415 return std::make_shared<DataComponentTypeEitherRegistryVarint>();
416#endif
433 return std::make_shared<DataComponentTypeVariantEnum>();
440 return std::make_shared<DataComponentTypeDyeColor>();
441#endif
442#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
445 return std::make_shared<DataComponentTypeTypedEntityData>();
446#endif
447#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
449 return std::make_shared<DataComponentTypeUseEffects>();
451 return std::make_shared<DataComponentTypeFloat>();
453 return std::make_shared<DataComponentTypeAttackRange>();
454#if PROTOCOL_VERSION < 775 /* < 26.1 */
457 return std::make_shared<DataComponentTypeEitherRegistryVarint>();
458#endif
460 return std::make_shared<DataComponentTypePiercingWeapon>();
462 return std::make_shared<DataComponentTypeKineticWeapon>();
463#endif
464#if PROTOCOL_VERSION > 773 /* > 1.21.10 */ && PROTOCOL_VERSION < 777 /* < 26.3 */
465 case DataComponentTypes::SwingAnimation:
466 return std::make_shared<DataComponentTypeSwingAnimation>();
467#endif
468#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
470 return std::make_shared<DataComponentTypeDyeColor>();
478 return std::make_shared<DataComponentTypeVariantEnum>();
480 return std::make_shared<DataComponentTypeInteger>();
481#endif
482#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
484 return std::make_shared<DataComponentTypeSulfurCubeContent>();
485#endif
486#if PROTOCOL_VERSION > 776 /* > 26.2 */
489 return std::make_shared<DataComponentTypeSwingAnimation>();
491 return std::make_shared<DataComponentTypeBrewingFuel>();
493 return std::make_shared<DataComponentTypeCompostable>();
495 return std::make_shared<DataComponentTypeCookingFuel>();
498 return std::make_shared<DataComponentTypeVariantEnum>();
500 return std::make_shared<DataComponentTypeMobVisibility>();
503 return std::make_shared<DataComponentTypeSignText>();
505 return std::make_shared<DataComponentTypeVariantEnum>();
507 return std::make_shared<DataComponentTypeUnit>();
509 return std::make_shared<DataComponentTypeDyeColor>();
510#endif
511 default:
512 // Should never happen but will make the compilers happy
513 throw std::runtime_error("Unable to create data component with id: " + std::to_string(static_cast<int>(type)));
514 return nullptr;
515 }
516
517 return nullptr;
518 }
519
524
525 const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& DataComponentPredicate::GetMap() const
526 {
527 return map;
528 }
529
530 DataComponentPredicate& DataComponentPredicate::SetMap(const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& map_)
531 {
532 map = map_;
533 return *this;
534 }
535
537 {
538 // special case, dynamic factory
539 const int map_size = ReadData<VarInt>(iter, length);
540 map.clear();
541 for (int i = 0; i < map_size; ++i)
542 {
543 const DataComponentTypes k = ReadData<DataComponentTypes, VarInt>(iter, length);
544 std::shared_ptr<DataComponentType> v = CreateComponentType(k);
545 if (v != nullptr)
546 {
547 v->Read(iter, length);
548 }
549 map[k] = v;
550 }
551 }
552
554 {
555 // special case, dynamic factory
556 WriteData<VarInt>(static_cast<int>(map.size()), container);
557 for (const auto& [k, v] : map)
558 {
559 WriteData<DataComponentTypes, VarInt>(k, container);
560 if (v != nullptr)
561 {
562 v->Write(container);
563 }
564 }
565 }
566
568 {
569 Json::Value output;
570
571 output["map"] = Json::Array();
572 for (const auto& p : map)
573 {
574 output["map"].push_back({
575 { "type", p.first },
576 { "data", p.second == nullptr ? nullptr : p.second->Serialize() }
577 });
578 }
579
580 return output;
581 }
582
583 template<bool LengthPrefixed>
584 const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& BasicDataComponentPatch<LengthPrefixed>::GetMap() const
585 {
586 return map;
587 }
588
589 template<bool LengthPrefixed>
591 {
592 map = map_;
593 return *this;
594 }
595
596 template<bool LengthPrefixed>
598 {
599 const int num_data = ReadData<VarInt>(iter, length);
600 const int num_void = ReadData<VarInt>(iter, length);
601
602 map.clear();
603
604 for (int i = 0; i < num_data; ++i)
605 {
606 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
607 int data_size = 0;
608 if constexpr (LengthPrefixed)
609 {
610 data_size = ReadData<VarInt>(iter, length);
611 }
612 std::shared_ptr<DataComponentType> data = CreateComponentType(type);
613 const size_t pre_data_length = length;
614
615 if (data != nullptr)
616 {
617 data->Read(iter, length);
618 if constexpr (LengthPrefixed)
619 {
620 if (pre_data_length - length != data_size)
621 {
622 throw std::runtime_error("Wrong data size when reading length prefixed component");
623 }
624 }
625 }
626 map.insert({ type, data });
627 }
628
629 for (int i = 0; i < num_void; ++i)
630 {
631 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
632 map.insert({ type, nullptr });
633 }
634 }
635
636 template<bool LengthPrefixed>
638 {
639 int num_data = 0;
640 for (const auto& p : map)
641 {
642 num_data += p.second != nullptr;
643 }
644 const int num_void = static_cast<int>(map.size()) - num_data;
645
646 WriteData<VarInt>(num_data, container);
647 WriteData<VarInt>(num_void, container);
648
649 for (const auto& p : map)
650 {
651 if (p.second == nullptr)
652 {
653 continue;
654 }
655
656 WriteData<DataComponentTypes, VarInt>(p.first, container);
657 if constexpr (LengthPrefixed)
658 {
659 WriteContainer temp;
660 p.second->Write(temp);
661 WriteData<VarInt>(static_cast<int>(temp.size()), container);
662 WriteByteArray(temp, container);
663 }
664 else
665 {
666 p.second->Write(container);
667 }
668 }
669
670 for (const auto& p : map)
671 {
672 if (p.second != nullptr)
673 {
674 continue;
675 }
676 WriteData<DataComponentTypes, VarInt>(p.first, container);
677 }
678 }
679
680 template<bool LengthPrefixed>
682 {
683 Json::Value output;
684
685 output["map"] = Json::Array();
686 for (const auto& p : map)
687 {
688 output["map"].push_back({
689 { "type", p.first },
690 { "data", p.second == nullptr ? nullptr : p.second->Serialize() }
691 });
692 }
693
694 return output;
695 }
696
697 template class BasicDataComponentPatch<false>;
698
699#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
700 template class BasicDataComponentPatch<true>;
701
703 {
704 for (const auto& [k, v] : patch.GetMap())
705 {
706 if (v == nullptr)
707 {
708 RemovedComponents.push_back(k);
709 }
710 else
711 {
712 // TODO: Fix this, this is *NOT* how the components are serialized to bytes
713 std::vector<unsigned char> serialized;
714 WriteData<DataComponentTypes, VarInt>(k, serialized);
715 v->Write(serialized);
716 // This however should be the right CRC32C hash calculation
717 // CRC32c polynomial, from https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations
718 // no idea why we need the reversed one but it works
719 constexpr unsigned int polynomial = 0x82F63B78;
720
721 unsigned int crc = 0xFFFFFFFF;
722
723 for (const auto byte : serialized)
724 {
725 crc ^= byte;
726 // TODO: replace slow loop with a faster version with lookup table and cool fancy stuff ?
727 for (int i = 0; i < 8; ++i)
728 {
729 // Trick to replace if branch
730 // if (crc & 1) crc = (crc >> 1) ^ polynomial;
731 // else crc = crc >> 1;
732 // Works because a ^ 0 == a
733 crc = (crc >> 1) ^ (crc & 1) * polynomial;
734 }
735 }
736 AddedComponents[k] = static_cast<int>(~crc);
737 }
738 }
739 }
740
742#endif
743 }
744}
745#endif
#define DEFINE_NETWORK_TYPE(ClassName)
BasicDataComponentPatch & SetMap(const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > &map_)
const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > & GetMap() const
virtual void WriteImpl(WriteContainer &container) const override
virtual Json::Value SerializeImpl() const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
virtual void WriteImpl(WriteContainer &container) const override
std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > map
virtual Json::Value SerializeImpl() const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > & GetMap() const
DataComponentPredicate & SetMap(const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > &map_)
Real class declaration, just a derived class of std::vector<Value>
Definition Json.hpp:180
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
void push_back(const Value &value)
Definition Json.cpp:257
std::shared_ptr< DataComponentType > CreateComponentType(const DataComponentTypes type)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator
void WriteByteArray(const std::vector< unsigned char > &my_array, WriteContainer &container)