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