Botcraft 1.21.4
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 class Slot : public NetworkType
13 {
14 // We don't store conditioned values as optional in Slot cause we already have a IsEmptySlot function to check for data presence
15#if PROTOCOL_VERSION < 393 /* < 1.13 */
16 DEFINE_CONDITION(HasContent, GetBlockId() != -1);
17 SERIALIZED_FIELD(BlockId, short);
21#elif PROTOCOL_VERSION < 404 /* < 1.13.2 */
22 DEFINE_CONDITION(HasContent, GetItemId() != -1);
23 SERIALIZED_FIELD(ItemId, short);
26#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
27 DEFINE_CONDITION(HasContent, GetPresent());
28 SERIALIZED_FIELD(Present, bool);
32#else
33 DEFINE_CONDITION(HasContent, GetItemCount() > 0);
37#endif
38
40
41 GETTER(ItemCount);
42#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
43 SETTER(ItemCount);
44#endif
45
46 public:
47 // Default constructor for empty slot
49 {
50#if PROTOCOL_VERSION < 350 /* < 1.13 */
51 BlockId = 0;
52#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
53 ItemId = -1;
54#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
55 Present = false;
56 ItemId = -1;
57#elif PROTOCOL_VERSION > 765 /* > 1.20.4 */
58 ItemId = 0;
59#endif
60 ItemCount = 0;
61 }
62
63 virtual ~Slot() override
64 {
65
66 }
67
68 bool SameItem(const Slot& s) const
69 {
70 if (IsEmptySlot() && s.IsEmptySlot())
71 {
72 return true;
73 }
74 if (IsEmptySlot() && !s.IsEmptySlot())
75 {
76 return false;
77 }
78 // !IsEmptySlot
79 if (s.IsEmptySlot())
80 {
81 return false;
82 }
83
84 // Both are non empty
85#if PROTOCOL_VERSION < 350 /* < 1.13 */
86 return GetBlockId() == s.GetBlockId() && GetItemDamage() == s.GetItemDamage();
87#else
88 return GetItemId() == s.GetItemId();
89#endif
90 }
91
92 bool IsEmptySlot() const
93 {
94#if PROTOCOL_VERSION < 350 /* < 1.13 */
95 return GetBlockId() == -1;
96#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
97 return GetItemId() == -1;
98#elif PROTOCOL_VERSION < 766 /* < 1.20.5 */
99 return !GetPresent();
100#elif PROTOCOL_VERSION > 765 /* > 1.20.4 */
101 return GetItemCount() <= 0 || GetItemId() == 0;
102#endif
103 }
104
105#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
106 auto& SetItemCount(const char ItemCount_)
107 {
108 ItemCount = ItemCount_;
109 if (ItemCount == 0)
110 {
111#if PROTOCOL_VERSION < 350 /* < 1.13 */
112 SetBlockId(-1);
113#elif PROTOCOL_VERSION < 402 /* < 1.13.2 */
114 SetItemId(-1);
115#elif PROTOCOL_VERSION > 401 /* > 1.13.1 */
116 SetPresent(false);
117 SetItemId(-1);
118#endif
119 SetNbt(NBT::Value());
120 }
121 return *this;
122 }
123#endif
124
125#if PROTOCOL_VERSION < 350 /* < 1.13 */
126 /// @brief Utility function to simplify interfacing between 1.12.2 and later versions
127 /// @return Paired { block_id, item_damage }
128 std::pair<int, unsigned char> GetItemId() const
129 {
130 return { static_cast<int>(GetBlockId()), static_cast<unsigned char>(GetItemDamage()) };
131 }
132#endif
133 };
134} // 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:92
bool SameItem(const Slot &s) const
Definition Slot.hpp:68
virtual ~Slot() override
Definition Slot.hpp:63
SERIALIZED_FIELD(Components, Internal::Conditioned< Components::DataComponentPatch, &Slot::HasContent, false >)
A type wrapper to conditionally serialize a type.
Definition Templates.hpp:83