ghast_data/v26_1/
items.rs1use super::g;
14use super::{Block, BlockState};
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
18pub struct Item(pub(crate) u16);
19
20pub use super::ItemData;
21
22#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct Food {
25 pub nutrition: u8,
27 pub saturation: f32,
29 pub can_always_eat: bool,
31}
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
35pub struct BlockSet(pub(crate) u16);
36
37impl BlockSet {
38 #[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 #[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 #[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#[derive(Clone, Copy, Debug, PartialEq)]
65pub struct ToolRule {
66 pub blocks: BlockSet,
68 pub speed: Option<f32>,
70 pub correct_for_drops: Option<bool>,
72}
73
74#[derive(Clone, Copy, Debug, PartialEq)]
76pub struct Tool {
77 pub rules: &'static [ToolRule],
79 pub default_mining_speed: f32,
81 pub damage_per_block: u32,
83 pub can_destroy_blocks_in_creative: bool,
85}
86
87impl Tool {
88 #[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 #[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 pub const AIR: Item = Item(0);
118
119 #[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 #[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 #[must_use]
133 pub const fn id(self) -> u16 {
134 self.0
135 }
136
137 #[must_use]
139 pub fn data(self) -> &'static ItemData {
140 &g::items::ITEMS[self.0 as usize]
141 }
142
143 #[must_use]
145 pub fn name(self) -> &'static str {
146 super::item_name(self.0)
147 }
148
149 #[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 #[must_use]
158 pub fn max_stack_size(self) -> u8 {
159 self.data().max_stack_size
160 }
161
162 #[must_use]
164 pub fn block(self) -> Option<Block> {
165 self.data().block.map(Block)
166 }
167
168 #[must_use]
170 pub fn food(self) -> Option<Food> {
171 self.data().food
172 }
173
174 #[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 #[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 #[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 pub fn all() -> impl ExactSizeIterator<Item = Item> {
195 (0..g::items::ITEMS.len() as u16).map(Item)
196 }
197}
198
199#[must_use]
201pub fn food(item: Item) -> Option<Food> {
202 item.food()
203}
204
205impl Block {
206 #[must_use]
208 pub fn destroy_time(self) -> f32 {
209 g::items::BLOCK_DESTROY_TIME[self.0 as usize]
210 }
211
212 #[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 #[must_use]
221 pub fn item(self) -> Item {
222 Item(g::items::BLOCK_ITEM[self.0 as usize])
223 }
224}
225
226impl BlockState {
227 #[must_use]
229 pub fn destroy_time(self) -> f32 {
230 self.block().destroy_time()
231 }
232
233 #[must_use]
235 pub fn requires_correct_tool_for_drops(self) -> bool {
236 self.block().requires_correct_tool_for_drops()
237 }
238}
239
240fn 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}