Botcraft 1.21.11
Loading...
Searching...
No Matches
ItemUtilities.cpp
Go to the documentation of this file.
2
4#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
7#endif
8
9using namespace ProtocolCraft;
10
11namespace Botcraft::Utilities
12{
13 std::string_view EnchantmentToString(const Botcraft::Enchantment enchantment)
14 {
15 switch (enchantment)
16 {
18 return "protection";
20 return "fire_protection";
22 return "feather_falling";
24 return "blast_protection";
26 return "projectile_protection";
28 return "respiration";
30 return "aqua_affinity";
32 return "thorns";
34 return "depth_strider";
36 return "frost_walker";
38 return "binding_curse";
39#if PROTOCOL_VERSION > 578 /* > 1.15.2 */
41 return "soul_speed";
42#endif
43#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
45 return "swift_sneak";
46#endif
48 return "sharpness";
50 return "smite";
52 return "bane_of_arthropods";
54 return "knockback";
56 return "fire_aspect";
58 return "looting";
60 return "sweeping_edge";
62 return "efficiency";
64 return "silk_touch";
66 return "unbreaking";
68 return "fortune";
70 return "power";
72 return "punch";
74 return "flame";
76 return "infinity";
78 return "luck_of_the_sea";
80 return "lure";
81#if PROTOCOL_VERSION > 340 /* > 1.12.2 */
83 return "loyalty";
85 return "impaling";
87 return "riptide";
89 return "channeling";
90#endif
91#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
93 return "multishot";
95 return "quick_charge";
97 return "piercing";
98#endif
99#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
101 return "density";
103 return "breach";
105 return "wind_burst";
106#endif
108 return "mending";
110 return "vanishing_curse";
111#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
113 return "lunge";
114#endif
115 default:
116 return "";
117 }
118 }
119
120 short GetEnchantmentLvl(const Slot& item, const Enchantment enchantment)
121 {
122 if (item.IsEmptySlot())
123 {
124 return 0;
125 }
126
127#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
128 const NBT::Value& item_nbt = item.GetNbt();
129 if (!item_nbt.HasData())
130 {
131 return 0;
132 }
133
134#if PROTOCOL_VERSION < 393 /* < 1.13 */
135 const std::string enchantment_key = "ench";
136#else
137 const std::string enchantment_key = "Enchantments";
138#endif
139 if (!item_nbt.contains(enchantment_key) ||
140 !item_nbt[enchantment_key].is_list_of<NBT::TagCompound>())
141 {
142 return 0;
143 }
144
145 for (const auto& enchantment_tag : item_nbt[enchantment_key].as_list_of<NBT::TagCompound>())
146 {
147 if (!enchantment_tag.contains("id") ||
148 !enchantment_tag.contains("lvl") ||
149#if PROTOCOL_VERSION < 393 /* < 1.13 */
150 !enchantment_tag["id"].is<NBT::TagInt>() ||
151#else
152 !enchantment_tag["id"].is<NBT::TagString>() ||
153#endif
154 (!enchantment_tag["lvl"].is<NBT::TagShort>() && !enchantment_tag["lvl"].is<NBT::TagInt>()))
155 {
156 continue;
157 }
158
159#if PROTOCOL_VERSION < 393 /* < 1.13 */
160 if (enchantment_tag["id"].get<NBT::TagInt>() == static_cast<int>(enchantment))
161#else
162 if (enchantment_tag["id"].get<NBT::TagString>() == EnchantmentToString(enchantment))
163#endif
164 {
165 if (enchantment_tag["lvl"].is<NBT::TagShort>())
166 {
167 return enchantment_tag["lvl"].get<NBT::TagShort>();
168 }
169 else if (enchantment_tag["lvl"].is<NBT::TagInt>())
170 {
171 return enchantment_tag["lvl"].get<NBT::TagInt>();
172 }
173 }
174 }
175
176 return 0;
177#else
178 const auto& components = item.GetComponents().GetMap();
179 auto c = components.find(Components::DataComponentTypes::Enchantments);
180 if (c == components.end())
181 {
182 return 0;
183 }
184
185 const std::map<int, int>& enchantments = std::static_pointer_cast<Components::DataComponentTypeItemEnchantments>(c->second)->GetEnchantments();
186
187 auto it = enchantments.find(static_cast<int>(enchantment));
188 if (it == enchantments.end())
189 {
190 return 0;
191 }
192
193 return it->second;
194#endif
195 }
196
197 int GetDamageCount(const Slot& item)
198 {
199 if (item.IsEmptySlot())
200 {
201 return 0;
202 }
203
204#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
205 const NBT::Value& item_nbt = item.GetNbt();
206 if (!item_nbt.HasData())
207 {
208 return 0;
209 }
210
211 if (!item_nbt.contains("Damage") ||
212 !item_nbt["Damage"].is<NBT::TagInt>())
213 {
214 return 0;
215 }
216
217 return item_nbt["Damage"].get<NBT::TagInt>();
218#else
219 const auto& components = item.GetComponents().GetMap();
220 auto it = components.find(Components::DataComponentTypes::Damage);
221 if (it == components.end())
222 {
223 return 0;
224 }
225
226 std::shared_ptr<Components::DataComponentTypeInteger> damage = std::static_pointer_cast<Components::DataComponentTypeInteger>(it->second);
227 return damage->GetValue();
228
229#endif
230 }
231}
bool contains(const std::string &s) const
Definition Tag.cpp:404
bool is() const
Definition Tag.hpp:173
bool is_list_of() const
Definition Tag.hpp:193
const std::vector< T > & as_list_of() const
Definition Tag.hpp:264
bool HasData() const
Definition NBT.cpp:63
bool IsEmptySlot() const
Definition Slot.hpp:92
int GetDamageCount(const ProtocolCraft::Slot &item)
short GetEnchantmentLvl(const ProtocolCraft::Slot &item, const Botcraft::Enchantment enchantment)
std::string_view EnchantmentToString(const Botcraft::Enchantment enchantment)
Enchantment
Definition Enums.hpp:94