Skip to main content

ghast_data/v26_1/
items.rs

1//! 26.1 items and dig facts, from the jar (oracle `tests/golden/26.1/items.jsonl.gz`).
2//!
3//! Items: id ↔ name, the default max stack size and max damage, food, the block an item places
4//! (`BlockItem`), and the default `minecraft:tool` component. These are the item type's
5//! defaults; a server can change any component on a stack (ghast-inventory decodes stack
6//! components).
7//!
8//! Dig facts: per block its destroy time and whether drops need the correct tool; a tool's speed
9//! and "correct for drops" come from its rules, each testing a block set (a block tag as the jar
10//! resolves it, or a direct block list). [`dig`](super::dig) turns these into vanilla's per-tick
11//! destroy progress.
12
13use super::g;
14use super::{Block, BlockState};
15
16/// A 26.1 item type (registry id).
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
18pub struct Item(pub(crate) u16);
19
20pub use super::ItemData;
21
22/// The `minecraft:food` component.
23#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct Food {
25    /// Hunger points restored.
26    pub nutrition: u8,
27    /// Saturation restored.
28    pub saturation: f32,
29    /// Edible with a full hunger bar.
30    pub can_always_eat: bool,
31}
32
33/// A set of blocks a tool rule tests (`HolderSet<Block>`): a tag or a direct list.
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
35pub struct BlockSet(pub(crate) u16);
36
37impl BlockSet {
38    /// `true` when the block is in the set.
39    #[inline]
40    #[must_use]
41    pub fn contains(self, b: Block) -> bool {
42        let w = &g::items::BLOCK_SETS[self.0 as usize];
43        let i = usize::from(b.id());
44        w[i / 64] & (1 << (i % 64)) != 0
45    }
46
47    /// The tag name (`"minecraft:mineable/pickaxe"`); `None` for a direct block list.
48    #[must_use]
49    pub fn tag(self) -> Option<&'static str> {
50        let n = set_name(self.0 as usize)?;
51        (!n.is_empty()).then_some(n)
52    }
53
54    /// The set of a block tag the tool rules use, by name (`"minecraft:mineable/axe"`).
55    #[must_use]
56    pub fn from_tag(name: &str) -> Option<BlockSet> {
57        (0..g::items::BLOCK_SET_NAMES.len())
58            .position(|i| set_name(i).is_some_and(|n| !n.is_empty() && n == name))
59            .map(|i| BlockSet(i as u16))
60    }
61}
62
63/// One rule of a tool component.
64#[derive(Clone, Copy, Debug, PartialEq)]
65pub struct ToolRule {
66    /// The blocks the rule applies to.
67    pub blocks: BlockSet,
68    /// Mining speed on those blocks.
69    pub speed: Option<f32>,
70    /// Whether the tool is correct for their drops.
71    pub correct_for_drops: Option<bool>,
72}
73
74/// The `minecraft:tool` component (`Tool` record).
75#[derive(Clone, Copy, Debug, PartialEq)]
76pub struct Tool {
77    /// Rules in order; the first matching one with the asked field set decides.
78    pub rules: &'static [ToolRule],
79    /// Speed when no rule with a speed matches.
80    pub default_mining_speed: f32,
81    /// Durability lost per block broken.
82    pub damage_per_block: u32,
83    /// Whether a creative player can break blocks while holding it.
84    pub can_destroy_blocks_in_creative: bool,
85}
86
87impl Tool {
88    /// `Tool.getMiningSpeed(state)`.
89    #[must_use]
90    pub fn mining_speed(&self, b: Block) -> f32 {
91        for r in self.rules {
92            if let Some(s) = r.speed
93                && r.blocks.contains(b)
94            {
95                return s;
96            }
97        }
98        self.default_mining_speed
99    }
100
101    /// `Tool.isCorrectForDrops(state)`.
102    #[must_use]
103    pub fn correct_for_drops(&self, b: Block) -> bool {
104        for r in self.rules {
105            if let Some(c) = r.correct_for_drops
106                && r.blocks.contains(b)
107            {
108                return c;
109            }
110        }
111        false
112    }
113}
114
115impl Item {
116    /// Air (the empty stack's item).
117    pub const AIR: Item = Item(0);
118
119    /// The item with this id, if it exists.
120    #[must_use]
121    pub fn from_id(id: u16) -> Option<Item> {
122        (usize::from(id) < g::items::ITEMS.len()).then_some(Item(id))
123    }
124
125    /// Looks an item up by registry name (`"diamond_pickaxe"`, no namespace).
126    #[must_use]
127    pub fn from_name(name: &str) -> Option<Item> {
128        crate::common::lookup_name(g::items::ITEM_NAMES_TEXT, &g::items::ITEM_NAMES, name).map(Item)
129    }
130
131    /// Registry id.
132    #[must_use]
133    pub const fn id(self) -> u16 {
134        self.0
135    }
136
137    /// Static data.
138    #[must_use]
139    pub fn data(self) -> &'static ItemData {
140        &g::items::ITEMS[self.0 as usize]
141    }
142
143    /// Registry name.
144    #[must_use]
145    pub fn name(self) -> &'static str {
146        super::item_name(self.0)
147    }
148
149    /// English display name.
150    #[cfg(feature = "display-names")]
151    #[must_use]
152    pub fn display_name(self) -> &'static str {
153        super::item_display_name(self.0)
154    }
155
156    /// Default max stack size.
157    #[must_use]
158    pub fn max_stack_size(self) -> u8 {
159        self.data().max_stack_size
160    }
161
162    /// The block this item places (`BlockItem.getBlock`).
163    #[must_use]
164    pub fn block(self) -> Option<Block> {
165        self.data().block.map(Block)
166    }
167
168    /// Default food component.
169    #[must_use]
170    pub fn food(self) -> Option<Food> {
171        self.data().food
172    }
173
174    /// Default tool component.
175    #[must_use]
176    pub fn tool(self) -> Option<&'static Tool> {
177        self.data().tool.map(|i| &g::items::TOOLS[i as usize])
178    }
179
180    /// Default destroy speed of this item on a block (`Item.getDestroySpeed`: the tool's speed,
181    /// else 1).
182    #[must_use]
183    pub fn destroy_speed(self, b: Block) -> f32 {
184        self.tool().map_or(1.0, |t| t.mining_speed(b))
185    }
186
187    /// Default "correct tool for drops" (`Item.isCorrectToolForDrops`).
188    #[must_use]
189    pub fn correct_for_drops(self, b: Block) -> bool {
190        self.tool().is_some_and(|t| t.correct_for_drops(b))
191    }
192
193    /// All items in id order.
194    pub fn all() -> impl ExactSizeIterator<Item = Item> {
195        (0..g::items::ITEMS.len() as u16).map(Item)
196    }
197}
198
199/// Food data of an item (default component).
200#[must_use]
201pub fn food(item: Item) -> Option<Food> {
202    item.food()
203}
204
205impl Block {
206    /// Destroy time (vanilla "hardness" / `getDestroySpeed`), from the jar; `-1.0` = unbreakable.
207    #[must_use]
208    pub fn destroy_time(self) -> f32 {
209        g::items::BLOCK_DESTROY_TIME[self.0 as usize]
210    }
211
212    /// Drops need the correct tool (`requiresCorrectToolForDrops`).
213    #[must_use]
214    pub fn requires_correct_tool_for_drops(self) -> bool {
215        let i = self.0 as usize;
216        g::items::BLOCK_REQUIRES_TOOL[i / 64] & (1 << (i % 64)) != 0
217    }
218
219    /// The block's item form (`Block.asItem`; [`Item::AIR`] when it has none).
220    #[must_use]
221    pub fn item(self) -> Item {
222        Item(g::items::BLOCK_ITEM[self.0 as usize])
223    }
224}
225
226impl BlockState {
227    /// [`Block::destroy_time`] (the same for every state in 26.1, checked by the generator).
228    #[must_use]
229    pub fn destroy_time(self) -> f32 {
230        self.block().destroy_time()
231    }
232
233    /// [`Block::requires_correct_tool_for_drops`].
234    #[must_use]
235    pub fn requires_correct_tool_for_drops(self) -> bool {
236        self.block().requires_correct_tool_for_drops()
237    }
238}
239
240/// The name of block set `i` (empty for a direct block list).
241fn set_name(i: usize) -> Option<&'static str> {
242    let end = usize::from(*g::items::BLOCK_SET_NAMES.get(i)?);
243    let start = if i == 0 {
244        0
245    } else {
246        usize::from(g::items::BLOCK_SET_NAMES[i - 1])
247    };
248    g::items::BLOCK_SET_NAMES_TEXT.get(start..end)
249}