Botcraft 1.21.4
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
100 return "mending";
102 return "vanishing_curse";
103 default:
104 return "";
105 }
106 }
107
108 short GetEnchantmentLvl(const Slot& item, const Enchantment enchantment)
109 {
110 if (item.IsEmptySlot())
111 {
112 return 0;
113 }
114
115#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
116 const NBT::Value& item_nbt = item.GetNbt();
117 if (!item_nbt.HasData())
118 {
119 return 0;
120 }
121
122#if PROTOCOL_VERSION < 393 /* < 1.13 */
123 const std::string enchantment_key = "ench";
124#else
125 const std::string enchantment_key = "Enchantments";
126#endif
127 if (!item_nbt.contains(enchantment_key) ||
128 !item_nbt[enchantment_key].is_list_of<NBT::TagCompound>())
129 {
130 return 0;
131 }
132
133 for (const auto& enchantment_tag : item_nbt[enchantment_key].as_list_of<NBT::TagCompound>())
134 {
135 if (!enchantment_tag.contains("id") ||
136 !enchantment_tag.contains("lvl") ||
137#if PROTOCOL_VERSION < 393 /* < 1.13 */
138 !enchantment_tag["id"].is<NBT::TagInt>() ||
139#else
140 !enchantment_tag["id"].is<NBT::TagString>() ||
141#endif
142 (!enchantment_tag["lvl"].is<NBT::TagShort>() && !enchantment_tag["lvl"].is<NBT::TagInt>()))
143 {
144 continue;
145 }
146
147#if PROTOCOL_VERSION < 393 /* < 1.13 */
148 if (enchantment_tag["id"].get<NBT::TagInt>() == static_cast<int>(enchantment))
149#else
150 if (enchantment_tag["id"].get<NBT::TagString>() == EnchantmentToString(enchantment))
151#endif
152 {
153 if (enchantment_tag["lvl"].is<NBT::TagShort>())
154 {
155 return enchantment_tag["lvl"].get<NBT::TagShort>();
156 }
157 else if (enchantment_tag["lvl"].is<NBT::TagInt>())
158 {
159 return enchantment_tag["lvl"].get<NBT::TagInt>();
160 }
161 }
162 }
163
164 return 0;
165#else
166 const auto& components = item.GetComponents().GetMap();
167 auto c = components.find(Components::DataComponentTypes::Enchantments);
168 if (c == components.end())
169 {
170 return 0;
171 }
172
173 const std::map<int, int>& enchantments = std::static_pointer_cast<Components::DataComponentTypeItemEnchantments>(c->second)->GetEnchantments();
174
175 auto it = enchantments.find(static_cast<int>(enchantment));
176 if (it == enchantments.end())
177 {
178 return 0;
179 }
180
181 return it->second;
182#endif
183 }
184
185 int GetDamageCount(const Slot& item)
186 {
187 if (item.IsEmptySlot())
188 {
189 return 0;
190 }
191
192#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
193 const NBT::Value& item_nbt = item.GetNbt();
194 if (!item_nbt.HasData())
195 {
196 return 0;
197 }
198
199 if (!item_nbt.contains("Damage") ||
200 !item_nbt["Damage"].is<NBT::TagInt>())
201 {
202 return 0;
203 }
204
205 return item_nbt["Damage"].get<NBT::TagInt>();
206#else
207 const auto& components = item.GetComponents().GetMap();
208 auto it = components.find(Components::DataComponentTypes::Damage);
209 if (it == components.end())
210 {
211 return 0;
212 }
213
214 std::shared_ptr<Components::DataComponentTypeInteger> damage = std::static_pointer_cast<Components::DataComponentTypeInteger>(it->second);
215 return damage->GetValue();
216
217#endif
218 }
219}
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