ghast_data/v26_1/
items.rs1use super::{Block, BlockState};
14use crate::generated::v26_1 as g;
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
18pub struct Item(pub(crate) u16);
19
20#[derive(Clone, Copy, Debug, PartialEq)]
22pub struct ItemData {
23 pub name: &'static str,
25 #[cfg(feature = "display-names")]
27 pub display_name: &'static str,
28 pub max_stack_size: u8,
30 pub max_damage: Option<u16>,
32 pub block: Option<u16>,
34 pub food: Option<Food>,
36 pub tool: Option<u16>,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq)]
42pub struct Food {
43 pub nutrition: u8,
45 pub saturation: f32,
47 pub can_always_eat: bool,
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
53pub struct BlockSet(pub(crate) u16);
54
55impl BlockSet {
56 #[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 #[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 #[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#[derive(Clone, Copy, Debug, PartialEq)]
83pub struct ToolRule {
84 pub blocks: BlockSet,
86 pub speed: Option<f32>,
88 pub correct_for_drops: Option<bool>,
90}
91
92#[derive(Clone, Copy, Debug, PartialEq)]
94pub struct Tool {
95 pub rules: &'static [ToolRule],
97 pub default_mining_speed: f32,
99 pub damage_per_block: u32,
101 pub can_destroy_blocks_in_creative: bool,
103}
104
105impl Tool {
106 #[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 #[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 pub const AIR: Item = Item(0);
136
137 #[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 #[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 #[must_use]
151 pub const fn id(self) -> u16 {
152 self.0
153 }
154
155 #[must_use]
157 pub fn data(self) -> &'static ItemData {
158 &g::items::ITEMS[self.0 as usize]
159 }
160
161 #[must_use]
163 pub fn name(self) -> &'static str {
164 self.data().name
165 }
166
167 #[must_use]
169 pub fn max_stack_size(self) -> u8 {
170 self.data().max_stack_size
171 }
172
173 #[must_use]
175 pub fn block(self) -> Option<Block> {
176 self.data().block.map(Block)
177 }
178
179 #[must_use]
181 pub fn food(self) -> Option<Food> {
182 self.data().food
183 }
184
185 #[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 #[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 #[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 pub fn all() -> impl ExactSizeIterator<Item = Item> {
206 (0..g::items::ITEMS.len() as u16).map(Item)
207 }
208}
209
210#[must_use]
212pub fn food(item: Item) -> Option<Food> {
213 item.food()
214}
215
216impl Block {
217 #[must_use]
219 pub fn destroy_time(self) -> f32 {
220 g::items::BLOCK_DESTROY_TIME[self.0 as usize]
221 }
222
223 #[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 #[must_use]
232 pub fn item(self) -> Item {
233 Item(g::items::BLOCK_ITEM[self.0 as usize])
234 }
235}
236
237impl BlockState {
238 #[must_use]
240 pub fn destroy_time(self) -> f32 {
241 self.block().destroy_time()
242 }
243
244 #[must_use]
246 pub fn requires_correct_tool_for_drops(self) -> bool {
247 self.block().requires_correct_tool_for_drops()
248 }
249}
250
251fn 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}