Botcraft 26.1.2
Loading...
Searching...
No Matches
SlotDisplay.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
3
11#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
15#endif
16
18
19namespace ProtocolCraft
20{
28#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
32#endif
33
34
36 {
37 if (type <= SlotDisplaysDataType::None || type >= SlotDisplaysDataType::NUM_SLOT_DISPLAYS_DATA_TYPES)
38 {
39 throw std::runtime_error("Unable to create SlotDisplaysData with id: " + std::to_string(static_cast<int>(type)) + ".");
40 }
41 Type = type;
42
43 switch (Type)
44 {
47 Data = std::make_shared<SlotDisplayDataEmpty>();
48 break;
50 Data = std::make_shared<ItemSlotDisplay>();
51 break;
53 Data = std::make_shared<ItemStackSlotDisplay>();
54 break;
56 Data = std::make_shared<TagSlotDisplay>();
57 break;
59 Data = std::make_shared<SmithingTrimDemoSlotDisplay>();
60 break;
62 Data = std::make_shared<WithRemainderSlotDisplay>();
63 break;
65 Data = std::make_shared<CompositeSlotDisplay>();
66 break;
67#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
69 Data = std::make_shared<WithAnyPotionSlotDisplay>();
70 break;
72 Data = std::make_shared<OnlyWithComponentSlotDisplay>();
73 break;
75 Data = std::make_shared<DyedSlotDemoDisplayData>();
76 break;
77#endif
78 default:
79 // This should not happen
80 throw std::runtime_error("Unable to create SlotDisplaysData with id: " + std::to_string(static_cast<int>(Type)) + ".");
81 Data = nullptr;
82 break;
83 }
84 return *this;
85 }
86
87
88 void SlotDisplay::ReadImpl(ReadIterator& iter, size_t& length)
89 {
90 SetType(ReadData<SlotDisplaysDataType, VarInt>(iter, length));
91 if (GetData() != nullptr)
92 {
93 GetData()->Read(iter, length);
94 }
95 }
96
97 void SlotDisplay::WriteImpl(WriteContainer& container) const
98 {
99 WriteData<SlotDisplaysDataType, VarInt>(GetType(), container);
100 if (GetData() != nullptr)
101 {
102 GetData()->Write(container);
103 }
104 }
105
106 Json::Value SlotDisplay::SerializeImpl() const
107 {
108 Json::Value output;
109
110 output[std::string(field_name<Type_index>)] = GetType();
111 output[std::string(field_name<Data_index>)] = GetData() == nullptr ? Json::Object() : GetData()->Serialize();
112
113 return output;
114 }
115}
116#endif
#define DEFINE_NETWORK_TYPE(ClassName)
virtual Json::Value SerializeImpl() const =0
virtual void WriteImpl(WriteContainer &container) const =0
virtual Json::Value Serialize() const
virtual void ReadImpl(ReadIterator &iter, size_t &length)=0
SlotDisplay & SetType(const SlotDisplaysDataType type)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator