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 = g::items::BLOCK_SET_NAMES[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 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#[derive(Clone, Copy, Debug, PartialEq)]
84pub struct ToolRule {
85 pub blocks: BlockSet,
87 pub speed: Option<f32>,
89 pub correct_for_drops: Option<bool>,
91}
92
93#[derive(Clone, Copy, Debug, PartialEq)]
95pub struct Tool {
96 pub rules: &'static [ToolRule],
98 pub default_mining_speed: f32,
100 pub damage_per_block: u32,
102 pub can_destroy_blocks_in_creative: bool,
104}
105
106impl Tool {
107 #[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 #[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 pub const AIR: Item = Item(0);
137
138 #[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 #[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 #[must_use]
152 pub const fn id(self) -> u16 {
153 self.0
154 }
155
156 #[must_use]
158 pub fn data(self) -> &'static ItemData {
159 &g::items::ITEMS[self.0 as usize]
160 }
161
162 #[must_use]
164 pub fn name(self) -> &'static str {
165 self.data().name
166 }
167
168 #[must_use]
170 pub fn max_stack_size(self) -> u8 {
171 self.data().max_stack_size
172 }
173
174 #[must_use]
176 pub fn block(self) -> Option<Block> {
177 self.data().block.map(Block)
178 }
179
180 #[must_use]
182 pub fn food(self) -> Option<Food> {
183 self.data().food
184 }
185
186 #[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 #[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 #[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 pub fn all() -> impl ExactSizeIterator<Item = Item> {
207 (0..g::items::ITEMS.len() as u16).map(Item)
208 }
209}
210
211#[must_use]
213pub fn food(item: Item) -> Option<Food> {
214 item.food()
215}
216
217impl Block {
218 #[must_use]
220 pub fn destroy_time(self) -> f32 {
221 g::items::BLOCK_DESTROY_TIME[self.0 as usize]
222 }
223
224 #[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 #[must_use]
233 pub fn item(self) -> Item {
234 Item(g::items::BLOCK_ITEM[self.0 as usize])
235 }
236}
237
238impl BlockState {
239 #[must_use]
241 pub fn destroy_time(self) -> f32 {
242 self.block().destroy_time()
243 }
244
245 #[must_use]
247 pub fn requires_correct_tool_for_drops(self) -> bool {
248 self.block().requires_correct_tool_for_drops()
249 }
250}