1use ghast_protocol::nbt::{Compound, Tag};
20
21use crate::rules::Version;
22use crate::stack::{Stack, last, number};
23
24#[derive(Debug, Clone, PartialEq)]
26#[non_exhaustive]
27pub enum ItemText {
28 Legacy(String),
30 Json(String),
32 #[cfg(modern)]
34 Nbt(ghast_protocol::nbt::AnonNbt),
35}
36
37impl ItemText {
38 #[must_use]
41 pub fn to_plain(&self) -> String {
42 match self {
43 ItemText::Legacy(s) => ghast_protocol::chat::strip_formatting(s),
44 ItemText::Json(s) => s.clone(),
45 #[cfg(modern)]
46 ItemText::Nbt(n) => ghast_protocol::chat::nbt_to_plain_text(n),
47 }
48 }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Hash)]
53#[non_exhaustive]
54pub enum EnchantmentId {
55 Legacy(i16),
57 Name(String),
59 Registry(i32),
62}
63
64impl EnchantmentId {
65 #[must_use]
68 pub fn name<'a>(&'a self, registry: &'a [String]) -> Option<&'a str> {
69 match self {
70 EnchantmentId::Legacy(_) => None,
71 EnchantmentId::Name(n) => Some(n),
72 EnchantmentId::Registry(i) => {
73 registry.get(usize::try_from(*i).ok()?).map(String::as_str)
74 }
75 }
76 }
77}
78
79#[derive(Debug, Clone, PartialEq, Eq, Hash)]
81#[non_exhaustive]
82pub struct Enchantment {
83 pub id: EnchantmentId,
85 pub level: i32,
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92#[non_exhaustive]
93pub enum ItemKind {
94 Sword,
96 Axe,
98 Pickaxe,
100 Shovel,
102 Hoe,
104 Bow,
106 Crossbow,
108 Trident,
110 Mace,
112 Shield,
114 FishingRod,
116 Helmet,
118 Chestplate,
120 Leggings,
122 Boots,
124 Other,
126}
127
128impl ItemKind {
129 #[must_use]
132 pub fn of_name(name: &str) -> ItemKind {
133 let name = name.strip_prefix("minecraft:").unwrap_or(name);
134 let exact = match name {
135 "bow" => Some(ItemKind::Bow),
136 "crossbow" => Some(ItemKind::Crossbow),
137 "trident" => Some(ItemKind::Trident),
138 "mace" => Some(ItemKind::Mace),
139 "shield" => Some(ItemKind::Shield),
140 "fishing_rod" => Some(ItemKind::FishingRod),
141 "turtle_helmet" => Some(ItemKind::Helmet),
142 _ => None,
143 };
144 if let Some(k) = exact {
145 return k;
146 }
147 for (suffix, k) in [
149 ("_sword", ItemKind::Sword),
150 ("_pickaxe", ItemKind::Pickaxe),
151 ("_axe", ItemKind::Axe),
152 ("_shovel", ItemKind::Shovel),
153 ("_hoe", ItemKind::Hoe),
154 ("_helmet", ItemKind::Helmet),
155 ("_chestplate", ItemKind::Chestplate),
156 ("_leggings", ItemKind::Leggings),
157 ("_boots", ItemKind::Boots),
158 ] {
159 if name.ends_with(suffix) {
160 return k;
161 }
162 }
163 ItemKind::Other
164 }
165}
166
167pub(crate) const LEGACY_ENCHANTMENTS: &[(i16, &str)] = &[
170 (0, "protection"),
171 (1, "fire_protection"),
172 (2, "feather_falling"),
173 (3, "blast_protection"),
174 (4, "projectile_protection"),
175 (5, "respiration"),
176 (6, "aqua_affinity"),
177 (7, "thorns"),
178 (8, "depth_strider"),
179 (9, "frost_walker"),
180 (10, "binding_curse"),
181 (16, "sharpness"),
182 (17, "smite"),
183 (18, "bane_of_arthropods"),
184 (19, "knockback"),
185 (20, "fire_aspect"),
186 (21, "looting"),
187 (22, "sweeping"),
188 (32, "efficiency"),
189 (33, "silk_touch"),
190 (34, "unbreaking"),
191 (35, "fortune"),
192 (48, "power"),
193 (49, "punch"),
194 (50, "flame"),
195 (51, "infinity"),
196 (61, "luck_of_the_sea"),
197 (62, "lure"),
198 (70, "mending"),
199 (71, "vanishing_curse"),
200];
201
202impl EnchantmentId {
203 #[must_use]
207 pub fn plain_name<'a>(&'a self, registry: &'a [String]) -> Option<&'a str> {
208 let n = match self {
209 EnchantmentId::Legacy(id) => {
210 return LEGACY_ENCHANTMENTS
211 .iter()
212 .find(|(i, _)| i == id)
213 .map(|(_, n)| *n);
214 }
215 other => other.name(registry)?,
216 };
217 Some(n.strip_prefix("minecraft:").unwrap_or(n))
218 }
219}
220
221impl Stack {
222 #[must_use]
224 pub fn kind(&self, v: Version) -> ItemKind {
225 self.item_name(v).map_or(ItemKind::Other, ItemKind::of_name)
226 }
227
228 #[must_use]
230 pub fn is_sword(&self, v: Version) -> bool {
231 self.kind(v) == ItemKind::Sword
232 }
233}
234
235fn root(s: &Stack) -> Option<Compound> {
236 match s.nbt()?.parse().ok()??.tag {
237 Tag::Compound(c) => Some(c),
238 _ => None,
239 }
240}
241
242fn display(s: &Stack) -> Option<Compound> {
243 match last(&root(s)?, "display")? {
244 Tag::Compound(c) => Some(c.clone()),
245 _ => None,
246 }
247}
248
249fn legacy_text(v: Version, s: String) -> ItemText {
250 if v == Version::V1_16_5 {
251 ItemText::Json(s)
252 } else {
253 ItemText::Legacy(s)
254 }
255}
256
257fn nbt_enchantments(s: &Stack, v: Version, stored: bool) -> Vec<Enchantment> {
258 let key = match (v, stored) {
259 (_, true) => "StoredEnchantments",
260 (Version::V1_16_5, false) => "Enchantments",
261 _ => "ench",
262 };
263 let Some(r) = root(s) else { return Vec::new() };
264 let Some(Tag::List(l)) = last(&r, key) else {
265 return Vec::new();
266 };
267 l.items
268 .iter()
269 .filter_map(|e| {
270 let Tag::Compound(e) = e else { return None };
271 let level = last(e, "lvl").and_then(number).unwrap_or(0) as i32;
272 let id = match last(e, "id")? {
273 Tag::String(n) => EnchantmentId::Name(n.clone()),
274 t => EnchantmentId::Legacy(number(t)? as i16),
275 };
276 Some(Enchantment { id, level })
277 })
278 .collect()
279}
280
281impl Stack {
282 #[must_use]
284 pub fn item_name(&self, v: Version) -> Option<&'static str> {
285 let n = v.name(self.id);
286 (!n.is_empty()).then_some(n)
287 }
288
289 #[must_use]
291 pub fn max_stack_size(&self, v: Version) -> i32 {
292 v.max_count(self)
293 }
294
295 #[must_use]
297 pub fn max_damage(&self, v: Version) -> Option<i32> {
298 match v {
299 #[cfg(modern)]
300 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::max_damage(
301 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
302 self,
303 ),
304 _ => v.max_durability(self.id),
305 }
306 }
307
308 #[must_use]
311 pub fn is_damageable(&self, v: Version) -> bool {
312 v.is_damageable(self)
313 }
314
315 #[must_use]
318 pub fn damage_value(&self, v: Version) -> i32 {
319 match v {
320 Version::V1_8 | Version::V1_12 => {
321 if v.has_subtypes(self.id) {
322 0
323 } else {
324 i32::from(self.damage)
325 }
326 }
327 Version::V1_16_5 => self.nbt_int("Damage"),
328 #[cfg(modern)]
329 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::damage_value(
330 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
331 self,
332 ),
333 #[allow(unreachable_patterns)]
334 _ => 0,
335 }
336 }
337
338 #[must_use]
340 pub fn unbreakable(&self, v: Version) -> bool {
341 match v {
342 #[cfg(modern)]
343 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::unbreakable(
344 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
345 self,
346 ),
347 _ => self.nbt_bool("Unbreakable"),
348 }
349 }
350
351 #[must_use]
353 pub fn custom_name(&self, v: Version) -> Option<ItemText> {
354 match v {
355 #[cfg(modern)]
356 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::text(
357 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
358 self,
359 crate::tables::kind::CUSTOM_NAME,
360 )
361 .map(ItemText::Nbt),
362 _ => match last(&display(self)?, "Name")? {
363 Tag::String(n) => Some(legacy_text(v, n.clone())),
364 _ => None,
365 },
366 }
367 }
368
369 #[must_use]
371 pub fn lore(&self, v: Version) -> Vec<ItemText> {
372 match v {
373 #[cfg(modern)]
374 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::lore(
375 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
376 self,
377 )
378 .into_iter()
379 .map(ItemText::Nbt)
380 .collect(),
381 _ => {
382 let Some(d) = display(self) else {
383 return Vec::new();
384 };
385 let Some(Tag::List(l)) = last(&d, "Lore") else {
386 return Vec::new();
387 };
388 l.items
389 .iter()
390 .filter_map(|t| match t {
391 Tag::String(s) => Some(legacy_text(v, s.clone())),
392 _ => None,
393 })
394 .collect()
395 }
396 }
397 }
398
399 #[must_use]
401 pub fn enchantments(&self, v: Version) -> Vec<Enchantment> {
402 self.enchants(v, false)
403 }
404
405 #[must_use]
407 pub fn stored_enchantments(&self, v: Version) -> Vec<Enchantment> {
408 self.enchants(v, true)
409 }
410
411 fn enchants(&self, v: Version, stored: bool) -> Vec<Enchantment> {
412 match v {
413 #[cfg(modern)]
414 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::enchantments(
415 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
416 self,
417 stored,
418 )
419 .unwrap_or_default()
420 .into_iter()
421 .map(|(id, level)| Enchantment {
422 id: EnchantmentId::Registry(id),
423 level,
424 })
425 .collect(),
426 _ => nbt_enchantments(self, v, stored),
427 }
428 }
429
430 #[must_use]
433 pub fn potion(&self, v: Version) -> Option<String> {
434 match v {
435 #[cfg(modern)]
436 Version::V26_1 | Version::V26_2 | Version::V26_3 => crate::item26::potion(
437 crate::tables::Mv::of(v).unwrap_or(crate::tables::Mv::ANY),
438 self,
439 )
440 .map(str::to_string),
441 Version::V1_12 | Version::V1_16_5 => match last(&root(self)?, "Potion")? {
442 Tag::String(p) => Some(p.clone()),
443 _ => None,
444 },
445 _ => None,
446 }
447 }
448}