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 = g::items::BLOCK_SET_NAMES[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        g::items::BLOCK_SET_NAMES
76            .iter()
77            .position(|n| !n.is_empty() && *n == name)
78            .map(|i| BlockSet(i as u16))
79    }
80}
81
82/// One rule of a tool component.
83#[derive(Clone, Copy, Debug, PartialEq)]
84pub struct ToolRule {
85    /// The blocks the rule applies to.
86    pub blocks: BlockSet,
87    /// Mining speed on those blocks.
88    pub speed: Option<f32>,
89    /// Whether the tool is correct for their drops.
90    pub correct_for_drops: Option<bool>,
91}
92
93/// The `minecraft:tool` component (`Tool` record).
94#[derive(Clone, Copy, Debug, PartialEq)]
95pub struct Tool {
96    /// Rules in order; the first matching one with the asked field set decides.
97    pub rules: &'static [ToolRule],
98    /// Speed when no rule with a speed matches.
99    pub default_mining_speed: f32,
100    /// Durability lost per block broken.
101    pub damage_per_block: u32,
102    /// Whether a creative player can break blocks while holding it.
103    pub can_destroy_blocks_in_creative: bool,
104}
105
106impl Tool {
107    /// `Tool.getMiningSpeed(state)`.
108    #[must_use]
109    pub fn mining_speed(&self, b: Block) -> f32 {
110        for r in self.rules {
111            if let Some(s) = r.speed
112                && r.blocks.contains(b)
113            {
114                return s;
115            }
116        }
117        self.default_mining_speed
118    }
119
120    /// `Tool.isCorrectForDrops(state)`.
121    #[must_use]
122    pub fn correct_for_drops(&self, b: Block) -> bool {
123        for r in self.rules {
124            if let Some(c) = r.correct_for_drops
125                && r.blocks.contains(b)
126            {
127                return c;
128            }
129        }
130        false
131    }
132}
133
134impl Item {
135    /// Air (the empty stack's item).
136    pub const AIR: Item = Item(0);
137
138    /// The item with this id, if it exists.
139    #[must_use]
140    pub fn from_id(id: u16) -> Option<Item> {
141        (usize::from(id) < g::items::ITEMS.len()).then_some(Item(id))
142    }
143
144    /// Looks an item up by registry name (`"diamond_pickaxe"`, no namespace).
145    #[must_use]
146    pub fn from_name(name: &str) -> Option<Item> {
147        crate::common::lookup_name(&g::items::ITEM_NAMES, name).map(Item)
148    }
149
150    /// Registry id.
151    #[must_use]
152    pub const fn id(self) -> u16 {
153        self.0
154    }
155
156    /// Static data.
157    #[must_use]
158    pub fn data(self) -> &'static ItemData {
159        &g::items::ITEMS[self.0 as usize]
160    }
161
162    /// Registry name.
163    #[must_use]
164    pub fn name(self) -> &'static str {
165        self.data().name
166    }
167
168    /// Default max stack size.
169    #[must_use]
170    pub fn max_stack_size(self) -> u8 {
171        self.data().max_stack_size
172    }
173
174    /// The block this item places (`BlockItem.getBlock`).
175    #[must_use]
176    pub fn block(self) -> Option<Block> {
177        self.data().block.map(Block)
178    }
179
180    /// Default food component.
181    #[must_use]
182    pub fn food(self) -> Option<Food> {
183        self.data().food
184    }
185
186    /// Default tool component.
187    #[must_use]
188    pub fn tool(self) -> Option<&'static Tool> {
189        self.data().tool.map(|i| &g::items::TOOLS[i as usize])
190    }
191
192    /// Default destroy speed of this item on a block (`Item.getDestroySpeed`: the tool's speed,
193    /// else 1).
194    #[must_use]
195    pub fn destroy_speed(self, b: Block) -> f32 {
196        self.tool().map_or(1.0, |t| t.mining_speed(b))
197    }
198
199    /// Default "correct tool for drops" (`Item.isCorrectToolForDrops`).
200    #[must_use]
201    pub fn correct_for_drops(self, b: Block) -> bool {
202        self.tool().is_some_and(|t| t.correct_for_drops(b))
203    }
204
205    /// All items in id order.
206    pub fn all() -> impl ExactSizeIterator<Item = Item> {
207        (0..g::items::ITEMS.len() as u16).map(Item)
208    }
209}
210
211/// Food data of an item (default component).
212#[must_use]
213pub fn food(item: Item) -> Option<Food> {
214    item.food()
215}
216
217impl Block {
218    /// Destroy time (vanilla "hardness" / `getDestroySpeed`), from the jar; `-1.0` = unbreakable.
219    #[must_use]
220    pub fn destroy_time(self) -> f32 {
221        g::items::BLOCK_DESTROY_TIME[self.0 as usize]
222    }
223
224    /// Drops need the correct tool (`requiresCorrectToolForDrops`).
225    #[must_use]
226    pub fn requires_correct_tool_for_drops(self) -> bool {
227        let i = self.0 as usize;
228        g::items::BLOCK_REQUIRES_TOOL[i / 64] & (1 << (i % 64)) != 0
229    }
230
231    /// The block's item form (`Block.asItem`; [`Item::AIR`] when it has none).
232    #[must_use]
233    pub fn item(self) -> Item {
234        Item(g::items::BLOCK_ITEM[self.0 as usize])
235    }
236}
237
238impl BlockState {
239    /// [`Block::destroy_time`] (the same for every state in 26.1, checked by the generator).
240    #[must_use]
241    pub fn destroy_time(self) -> f32 {
242        self.block().destroy_time()
243    }
244
245    /// [`Block::requires_correct_tool_for_drops`].
246    #[must_use]
247    pub fn requires_correct_tool_for_drops(self) -> bool {
248        self.block().requires_correct_tool_for_drops()
249    }
250}