Botcraft 1.21.4
Loading...
Searching...
No Matches
SlotDisplay.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
3
11
13
14namespace ProtocolCraft
15{
23
25 {
26 if (type <= SlotDisplaysDataType::None || type >= SlotDisplaysDataType::NUM_SLOT_DISPLAYS_DATA_TYPES)
27 {
28 throw std::runtime_error("Unable to create SlotDisplaysData with id: " + std::to_string(static_cast<int>(type)) + ".");
29 }
30 Type = type;
31
32 switch (Type)
33 {
36 Data = std::make_shared<SlotDisplayDataEmpty>();
37 break;
39 Data = std::make_shared<ItemSlotDisplay>();
40 break;
42 Data = std::make_shared<ItemStackSlotDisplay>();
43 break;
45 Data = std::make_shared<TagSlotDisplay>();
46 break;
48 Data = std::make_shared<SmithingTrimDemoSlotDisplay>();
49 break;
51 Data = std::make_shared<WithRemainderSlotDisplay>();
52 break;
54 Data = std::make_shared<CompositeSlotDisplay>();
55 break;
56 default:
57 // This should not happen
58 throw std::runtime_error("Unable to create SlotDisplaysData with id: " + std::to_string(static_cast<int>(Type)) + ".");
59 Data = nullptr;
60 break;
61 }
62 return *this;
63 }
64
65
66 void SlotDisplay::ReadImpl(ReadIterator& iter, size_t& length)
67 {
68 SetType(ReadData<SlotDisplaysDataType, VarInt>(iter, length));
69 if (GetData() != nullptr)
70 {
71 GetData()->Read(iter, length);
72 }
73 }
74
75 void SlotDisplay::WriteImpl(WriteContainer& container) const
76 {
77 WriteData<SlotDisplaysDataType, VarInt>(GetType(), container);
78 if (GetData() != nullptr)
79 {
80 GetData()->Write(container);
81 }
82 }
83
84 Json::Value SlotDisplay::SerializeImpl() const
85 {
86 Json::Value output;
87
88 output[std::string(field_name<Type_index>)] = GetType();
89 output[std::string(field_name<Data_index>)] = GetData() == nullptr ? Json::Object() : GetData()->Serialize();
90
91 return output;
92 }
93}
94#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