Botcraft 1.21.4
Loading...
Searching...
No Matches
HolderSet.hpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
2#pragma once
5
6#include <optional>
7#include <vector>
8
9namespace ProtocolCraft
10{
11 class HolderSet : public NetworkType
12 {
13 public:
14 virtual ~HolderSet()
15 {
16
17 }
18
19
20 const std::optional<Identifier>& GetKey() const
21 {
22 return key;
23 }
24
25 const std::vector<int>& GetContents() const
26 {
27 return contents;
28 }
29
30
31 auto& SetKey(const std::optional<Identifier>& key_)
32 {
33 key = key_;
34 if (key.has_value())
35 {
36 contents.clear();
37 }
38 return *this;
39 }
40
41 auto& SetContents(const std::vector<int>& contents_)
42 {
43 contents = contents_;
44 if (contents.size() > 0)
45 {
46 key = std::optional<Identifier>();
47 }
48 return *this;
49 }
50
51 protected:
52 virtual void ReadImpl(ReadIterator& iter, size_t& length) override
53 {
54 const int size = ReadData<VarInt>(iter, length) - 1;
55 if (size == -1)
56 {
57 key = ReadData<Identifier>(iter, length);
58 }
59 else
60 {
61 contents = std::vector<int>(size);
62 for (int i = 0; i < size; ++i)
63 {
64 contents[i] = ReadData<VarInt>(iter, length);
65 }
66 }
67 }
68
69 virtual void WriteImpl(WriteContainer& container) const override
70 {
71 if (key.has_value())
72 {
73 WriteData<VarInt>(0, container);
74 WriteData<Identifier>(key.value(), container);
75 }
76 else
77 {
78 WriteData<VarInt>(static_cast<int>(contents.size()) + 1, container);
79 for (const int i : contents)
80 {
81 WriteData<VarInt>(i, container);
82 }
83 }
84 }
85
86 virtual Json::Value SerializeImpl() const override
87 {
88 Json::Value output;
89
90 if (key.has_value())
91 {
92 output["key"] = key.value();
93 }
94 else
95 {
96 output["content"] = contents;
97 }
98
99 return output;
100 }
101
102 private:
103 std::optional<Identifier> key;
104 std::vector<int> contents;
105
106 };
107}
108#endif
std::vector< int > contents
auto & SetKey(const std::optional< Identifier > &key_)
Definition HolderSet.hpp:31
const std::vector< int > & GetContents() const
Definition HolderSet.hpp:25
auto & SetContents(const std::vector< int > &contents_)
Definition HolderSet.hpp:41
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition HolderSet.hpp:52
const std::optional< Identifier > & GetKey() const
Definition HolderSet.hpp:20
virtual void WriteImpl(WriteContainer &container) const override
Definition HolderSet.hpp:69
virtual Json::Value SerializeImpl() const override
Definition HolderSet.hpp:86
std::optional< Identifier > key
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator