1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! 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,
}
}
}