ghast-inventory 0.5.6

Inventory and container windows for the ghast Minecraft Java Edition client library: vanilla client-side click prediction, the window confirm protocol, hashed 26.1 clicks and lazy NBT / data-component item stacks (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3)
Documentation
//! Attack attributes of a held stack: [`Stack::attack`] (legacy versions; the module exists
//! when one of their features is on).

use ghast_protocol::nbt::Tag;

use crate::rules::Version;
use crate::stack::{Stack, last};

pub use ghast_data::ItemAttack;

impl Stack {
    /// The attack attributes this stack gives when held in the main hand, as the server applies
    /// them: [`ItemAttack::attack_damage`] / [`ItemAttack::attack_speed`] are the player's
    /// attribute values (base 1.0 / 4.0 plus the item's modifiers). 1.8.9, 1.12.2 and 1.16.5.
    ///
    /// `None` when the stack carries an `AttributeModifiers` list (the server then uses that
    /// list instead of the item's modifiers; not modelled), on 26.x, or when the version's
    /// feature is off. The bare hand is `ghast_data::<version>::item_attack(0)`.
    #[must_use]
    pub fn attack(&self, v: Version) -> Option<ItemAttack> {
        if self.has_attribute_modifiers() {
            return None;
        }
        let id = u16::try_from(self.id).ok()?;
        match v {
            #[cfg(feature = "v1_8")]
            Version::V1_8 => Some(ghast_data::v1_8::item_attack(id)),
            #[cfg(feature = "v1_12")]
            Version::V1_12 => Some(ghast_data::v1_12::item_attack(id)),
            #[cfg(feature = "v1_16_5")]
            Version::V1_16_5 => Some(ghast_data::v1_16_5::item_attack(id)),
            _ => {
                let _ = id;
                None
            }
        }
    }

    /// The NBT has an `AttributeModifiers` list (`contains("AttributeModifiers", 9)`), which
    /// replaces the item's own modifiers.
    fn has_attribute_modifiers(&self) -> bool {
        let Some(Ok(Some(n))) = self.nbt().map(|n| n.parse()) else {
            return false;
        };
        match &n.tag {
            Tag::Compound(c) => matches!(last(c, "AttributeModifiers"), Some(Tag::List(_))),
            _ => false,
        }
    }
}