use ghast_protocol::nbt::{Compound, Tag};
use crate::rules::Version;
use crate::stack::{Stack, last, number};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ItemText {
Legacy(String),
Json(String),
#[cfg(feature = "v26_1")]
Nbt(ghast_protocol::nbt::AnonNbt),
}
impl ItemText {
#[must_use]
pub fn to_plain(&self) -> String {
match self {
ItemText::Legacy(s) => ghast_protocol::chat::strip_formatting(s),
ItemText::Json(s) => s.clone(),
#[cfg(feature = "v26_1")]
ItemText::Nbt(n) => ghast_protocol::chat::nbt_to_plain_text(n),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EnchantmentId {
Legacy(i16),
Name(String),
Registry(i32),
}
impl EnchantmentId {
#[must_use]
pub fn name<'a>(&'a self, registry: &'a [String]) -> Option<&'a str> {
match self {
EnchantmentId::Legacy(_) => None,
EnchantmentId::Name(n) => Some(n),
EnchantmentId::Registry(i) => {
registry.get(usize::try_from(*i).ok()?).map(String::as_str)
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Enchantment {
pub id: EnchantmentId,
pub level: i32,
}
fn root(s: &Stack) -> Option<Compound> {
match s.nbt()?.parse().ok()??.tag {
Tag::Compound(c) => Some(c),
_ => None,
}
}
fn display(s: &Stack) -> Option<Compound> {
match last(&root(s)?, "display")? {
Tag::Compound(c) => Some(c.clone()),
_ => None,
}
}
fn legacy_text(v: Version, s: String) -> ItemText {
if v == Version::V1_16_5 {
ItemText::Json(s)
} else {
ItemText::Legacy(s)
}
}
fn nbt_enchantments(s: &Stack, v: Version, stored: bool) -> Vec<Enchantment> {
let key = match (v, stored) {
(_, true) => "StoredEnchantments",
(Version::V1_16_5, false) => "Enchantments",
_ => "ench",
};
let Some(r) = root(s) else { return Vec::new() };
let Some(Tag::List(l)) = last(&r, key) else {
return Vec::new();
};
l.items
.iter()
.filter_map(|e| {
let Tag::Compound(e) = e else { return None };
let level = last(e, "lvl").and_then(number).unwrap_or(0) as i32;
let id = match last(e, "id")? {
Tag::String(n) => EnchantmentId::Name(n.clone()),
t => EnchantmentId::Legacy(number(t)? as i16),
};
Some(Enchantment { id, level })
})
.collect()
}
impl Stack {
#[must_use]
pub fn item_name(&self, v: Version) -> Option<&'static str> {
let n = v.name(self.id);
(!n.is_empty()).then_some(n)
}
#[must_use]
pub fn max_stack_size(&self, v: Version) -> i32 {
v.max_count(self)
}
#[must_use]
pub fn max_damage(&self, v: Version) -> Option<i32> {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::max_damage(self),
_ => v.max_durability(self.id),
}
}
#[must_use]
pub fn is_damageable(&self, v: Version) -> bool {
v.is_damageable(self)
}
#[must_use]
pub fn damage_value(&self, v: Version) -> i32 {
match v {
Version::V1_8 | Version::V1_12 => {
if v.has_subtypes(self.id) {
0
} else {
i32::from(self.damage)
}
}
Version::V1_16_5 => self.nbt_int("Damage"),
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::damage_value(self),
#[allow(unreachable_patterns)]
_ => 0,
}
}
#[must_use]
pub fn unbreakable(&self, v: Version) -> bool {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::unbreakable(self),
_ => self.nbt_bool("Unbreakable"),
}
}
#[must_use]
pub fn custom_name(&self, v: Version) -> Option<ItemText> {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => {
crate::v26_1::text(self, crate::v26_1::component::CUSTOM_NAME).map(ItemText::Nbt)
}
_ => match last(&display(self)?, "Name")? {
Tag::String(n) => Some(legacy_text(v, n.clone())),
_ => None,
},
}
}
#[must_use]
pub fn lore(&self, v: Version) -> Vec<ItemText> {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::lore(self)
.into_iter()
.map(ItemText::Nbt)
.collect(),
_ => {
let Some(d) = display(self) else {
return Vec::new();
};
let Some(Tag::List(l)) = last(&d, "Lore") else {
return Vec::new();
};
l.items
.iter()
.filter_map(|t| match t {
Tag::String(s) => Some(legacy_text(v, s.clone())),
_ => None,
})
.collect()
}
}
}
#[must_use]
pub fn enchantments(&self, v: Version) -> Vec<Enchantment> {
self.enchants(v, false)
}
#[must_use]
pub fn stored_enchantments(&self, v: Version) -> Vec<Enchantment> {
self.enchants(v, true)
}
fn enchants(&self, v: Version, stored: bool) -> Vec<Enchantment> {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::enchantments(self, stored)
.unwrap_or_default()
.into_iter()
.map(|(id, level)| Enchantment {
id: EnchantmentId::Registry(id),
level,
})
.collect(),
_ => nbt_enchantments(self, v, stored),
}
}
#[must_use]
pub fn potion(&self, v: Version) -> Option<String> {
match v {
#[cfg(feature = "v26_1")]
Version::V26_1 => crate::v26_1::potion(self).map(str::to_string),
Version::V1_12 | Version::V1_16_5 => match last(&root(self)?, "Potion")? {
Tag::String(p) => Some(p.clone()),
_ => None,
},
_ => None,
}
}
}