Skip to main content

ghast_data/
attack.rs

1//! Attack attributes of a held item (1.8.9, 1.12.2, 1.16.5), from the jar.
2//!
3//! The tables are generated from the oracle's `item_attacks.jsonl.gz` dumps
4//! (`tests/golden/<version>/`): the real item classes' main-hand attribute modifiers
5//! (`ItemStack.getAttributes` / `getAttributeModifiers(MAINHAND)` of a stack without NBT) and the
6//! real player's base values. minecraft-data has no per-item attack numbers; its attribute
7//! defaults (2.0 / 4.0, which a player overrides with base damage 1.0) agree with the jar.
8
9/// What an item held in the main hand gives the player's attack attributes, as a server applies
10/// it (stack without an `AttributeModifiers` tag; see `ghast_inventory` for stacks).
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct ItemAttack {
13    /// Sum of the item's additive (operation 0) `attack_damage` modifiers; 0 for items without.
14    pub damage_modifier: f64,
15    /// Sum of the item's additive `attack_speed` modifiers (`None` on 1.8: no attack speed
16    /// attribute).
17    pub speed_modifier: Option<f64>,
18    /// The player's base attack damage (1.0).
19    pub base_damage: f64,
20    /// The player's base attack speed (4.0; `None` on 1.8).
21    pub base_speed: Option<f64>,
22}
23
24impl ItemAttack {
25    /// The player's `attack_damage` attribute value with the item held (base + modifier,
26    /// computed as `EntityAttributeInstance` does; vanilla items never reach the clamp).
27    #[must_use]
28    pub fn attack_damage(&self) -> f64 {
29        self.base_damage + self.damage_modifier
30    }
31
32    /// The player's `attack_speed` attribute value with the item held (`None` on 1.8).
33    #[must_use]
34    pub fn attack_speed(&self) -> Option<f64> {
35        Some(self.base_speed? + self.speed_modifier?)
36    }
37}
38
39/// Binary search of a `(id, damage, speed)` table.
40#[allow(dead_code)]
41pub(crate) fn find3(table: &[(u16, f64, f64)], item: u16) -> (f64, f64) {
42    table
43        .binary_search_by_key(&item, |r| r.0)
44        .map_or((0.0, 0.0), |i| (table[i].1, table[i].2))
45}