Botcraft 26.2
Loading...
Searching...
No Matches
Slot.hpp
Go to the documentation of this file.
1#pragma once
2
4#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
6#else
8#endif
9
10namespace ProtocolCraft
11{
12#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
13 class UntrustedItemStack;
14#endif
15
16 class Slot : public NetworkType
17 {
18 // We don't store conditioned values as optional in Slot cause we already have a IsEmptySlot function to check for data presence
19#if PROTOCOL_VERSION < 393 /* < 1.13 */
20 DEFINE_CONDITION(HasContent, GetBlockId() != -1);
21 SERIALIZED_FIELD(BlockId, short);
25#elif PROTOCOL_VERSION < 404 /* < 1.13.2 */
26 DEFINE_CONDITION(HasContent, GetItemId() != -1);
27 SERIALIZED_FIELD(ItemId, short);
30#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
31 DEFINE_CONDITION(HasContent, GetPresent());
32 SERIALIZED_FIELD(Present, bool);
36#else
37 DEFINE_CONDITION(HasContent, GetItemCount() > 0);
41#endif
42
44
45 GETTER(ItemCount);
46#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
47 SETTER(ItemCount);
48#endif
49
50 public:
51 // Default constructor for empty slot
53 {
54#if PROTOCOL_VERSION < 350 /* < 1.13 */
55 BlockId = 0;
56#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
57 ItemId = -1;
58#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
59 Present = false;
60 ItemId = -1;
61#elif PROTOCOL_VERSION > 765 /* > 1.20.4 */
62 ItemId = 0;
63#endif
64 ItemCount = 0;
65 }
66
67#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
68 Slot(const UntrustedItemStack& s);
69#endif
70
71 virtual ~Slot() override
72 {
73
74 }
75
76 bool SameItem(const Slot& s) const
77 {
78 if (IsEmptySlot() && s.IsEmptySlot())
79 {
80 return true;
81 }
82 if (IsEmptySlot() && !s.IsEmptySlot())
83 {
84 return false;
85 }
86 // !IsEmptySlot
87 if (s.IsEmptySlot())
88 {
89 return false;
90 }
91
92 // Both are non empty
93#if PROTOCOL_VERSION < 350 /* < 1.13 */
94 return GetBlockId() == s.GetBlockId() && GetItemDamage() == s.GetItemDamage();
95#else
96 return GetItemId() == s.GetItemId();
97#endif
98 }
99
100 bool IsEmptySlot() const
101 {
102#if PROTOCOL_VERSION < 350 /* < 1.13 */
103 return GetBlockId() == -1;
104#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
105 return GetItemId() == -1;
106#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
107 return !GetPresent();
108#elif PROTOCOL_VERSION > 765 /* > 1.20.4 */
109 return GetItemCount() <= 0 || GetItemId() == 0;
110#endif
111 }
112
113#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
114 auto& SetItemCount(const char ItemCount_)
115 {
116 ItemCount = ItemCount_;
117 if (ItemCount == 0)
118 {
119#if PROTOCOL_VERSION < 350 /* < 1.13 */
120 SetBlockId(-1);
121#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
122 SetItemId(-1);
123#elif PROTOCOL_VERSION > 401 /* > 1.13.1 */
124 SetPresent(false);
125 SetItemId(-1);
126#endif
127 SetNbt(NBT::Value());
128 }
129 return *this;
130 }
131#endif
132
133#if PROTOCOL_VERSION < 350 /* < 1.13 */
134 /// @brief Utility function to simplify interfacing between 1.12.2 and later versions
135 /// @return Paired { block_id, item_damage }
136 std::pair<int, unsigned char> GetItemId() const
137 {
138 return { static_cast<int>(GetBlockId()), static_cast<unsigned char>(GetItemDamage()) };
139 }
140#endif
141 };
142
143
144#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
146 {
147 public:
149
151 {
152 ItemCount = s.GetItemCount();
153 ItemId = s.GetItemId();
154 Components = s.GetComponents();
155 }
156
157 DEFINE_CONDITION(HasContent, GetItemCount() > 0);
161
163 };
164
166 {
167 ItemCount = s.GetItemCount();
168 ItemId = s.GetItemId();
169 Components = s.GetComponents();
170 }
171#endif
172
173} // ProtocolCraft
#define SERIALIZED_FIELD_WITHOUT_GETTER_SETTER(Name,...)
Define a field that will be automatically integrated in Read/Write and Json serialization We start by...
#define DEFINE_CONDITION(Name,...)
Define a condition that can be used later inside an Internal::Conditioned type.
#define SERIALIZED_FIELD(Name,...)
SERIALIZED_FIELD(ItemId, Internal::Conditioned< VarInt, &Slot::HasContent, false >)
SERIALIZED_FIELD_WITHOUT_GETTER_SETTER(ItemCount, VarInt)
DEFINE_CONDITION(HasContent, GetItemCount() > 0)
bool IsEmptySlot() const
Definition Slot.hpp:100
bool SameItem(const Slot &s) const
Definition Slot.hpp:76
virtual ~Slot() override
Definition Slot.hpp:71
SERIALIZED_FIELD(Components, Internal::Conditioned< Components::DataComponentPatch, &Slot::HasContent, false >)
SERIALIZED_FIELD(ItemId, Internal::Conditioned< VarInt, &UntrustedItemStack::HasContent, false >)
SERIALIZED_FIELD(Components, Internal::Conditioned< Components::LengthPrefixedDataComponentPatch, &UntrustedItemStack::HasContent, false >)
DEFINE_CONDITION(HasContent, GetItemCount() > 0)
UntrustedItemStack(const Slot &s)
Definition Slot.hpp:150
SERIALIZED_FIELD(ItemCount, VarInt)
A type wrapper to conditionally serialize a type.
Definition Templates.hpp:83