Botcraft 1.21.4
Loading...
Searching...
No Matches
RecipeDisplay.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
3
9
11
12
13namespace ProtocolCraft
14{
20
22 {
23 if (type <= RecipeDisplayDataType::None || type >= RecipeDisplayDataType::NUM_RECIPE_DISPLAY_DATA_TYPES)
24 {
25 throw std::runtime_error("Unable to create RecipeDisplayData with id: " + std::to_string(static_cast<int>(type)) + ".");
26 }
27 Type = type;
28
29 switch (Type)
30 {
32 Data = std::make_shared<ShapelessCraftingRecipeDisplay>();
33 break;
35 Data = std::make_shared<ShapedCraftingRecipeDisplay>();
36 break;
38 Data = std::make_shared<FurnaceRecipeDisplay>();
39 break;
41 Data = std::make_shared<StonecutterRecipeDisplay>();
42 break;
44 Data = std::make_shared<SmithingRecipeDisplay>();
45 break;
46 default:
47 // This should not happen
48 throw std::runtime_error("Unable to create RecipeDisplayData with id: " + std::to_string(static_cast<int>(Type)) + ".");
49 Data = nullptr;
50 break;
51 }
52 return *this;
53 }
54
55
56 void RecipeDisplay::ReadImpl(ReadIterator& iter, size_t& length)
57 {
58 SetType(ReadData<RecipeDisplayDataType, VarInt>(iter, length));
59 if (GetData() != nullptr)
60 {
61 GetData()->Read(iter, length);
62 }
63 }
64
65 void RecipeDisplay::WriteImpl(WriteContainer& container) const
66 {
67 WriteData<RecipeDisplayDataType, VarInt>(GetType(), container);
68 if (GetData() != nullptr)
69 {
70 GetData()->Write(container);
71 }
72 }
73
74 Json::Value RecipeDisplay::SerializeImpl() const
75 {
76 Json::Value output;
77
78 output[std::string(field_name<Type_index>)] = GetType();
79 output[std::string(field_name<Data_index>)] = GetData() == nullptr ? Json::Object() : GetData()->Serialize();
80
81 return output;
82 }
83}
84#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
RecipeDisplay & SetType(const RecipeDisplayDataType type)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator