1use crate::common::{
18 Aabb, BiomeInfo, BlockInfo, BlockPhysics, Collision, DigMaterial, EffectInfo, EnchantmentInfo,
19 EntityInfo, EntityKind, Fluid, FluidKind, FoodInfo, ItemInfo, NeighbourShape, PhysicsFlags,
20 ShapeDependency, lookup_id, lookup_name,
21};
22use crate::generated::v1_12 as g;
23
24pub mod dig;
25
26pub const BLOCK_ID_SPACE: usize = 256;
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
31pub enum MaterialKind {
32 Air,
34 Water,
36 Lava,
38 Ice,
40 Other,
42}
43
44#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
46pub struct Material {
47 pub name: &'static str,
49 pub kind: MaterialKind,
51 pub blocks_movement: bool,
53 pub solid: bool,
55 pub liquid: bool,
57 pub opaque: bool,
59 pub replaceable: bool,
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
65pub struct StateFlags(pub u8);
66
67impl StateFlags {
68 pub const FULL_CUBE: StateFlags = StateFlags(1 << 0);
70 pub const NORMAL_CUBE: StateFlags = StateFlags(1 << 1);
73 pub const BLOCK_NORMAL_CUBE: StateFlags = StateFlags(1 << 2);
75 pub const TOP_SOLID: StateFlags = StateFlags(1 << 3);
77 pub const OPAQUE_CUBE: StateFlags = StateFlags(1 << 4);
79 pub const FULL_BLOCK: StateFlags = StateFlags(1 << 5);
81 pub const CAUSES_SUFFOCATION: StateFlags = StateFlags(1 << 6);
83
84 #[must_use]
86 pub const fn contains(self, other: StateFlags) -> bool {
87 self.0 & other.0 == other.0
88 }
89}
90
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
93pub enum Face {
94 Down,
96 Up,
98 North,
100 South,
102 West,
104 East,
106}
107
108#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
111pub enum FaceShape {
112 Solid,
114 Bowl,
116 CenterSmall,
118 MiddlePoleThin,
120 Center,
122 MiddlePole,
124 CenterBig,
126 MiddlePoleThick,
128 Undefined,
130}
131
132const FACE_SHAPES: [FaceShape; 9] = [
133 FaceShape::Solid,
134 FaceShape::Bowl,
135 FaceShape::CenterSmall,
136 FaceShape::MiddlePoleThin,
137 FaceShape::Center,
138 FaceShape::MiddlePole,
139 FaceShape::CenterBig,
140 FaceShape::MiddlePoleThick,
141 FaceShape::Undefined,
142];
143
144#[derive(Clone, Copy, Debug, PartialEq)]
146pub struct Selection {
147 pub bounds: Aabb,
149 pub dependency: ShapeDependency,
151 pub multi_pass_ray: bool,
153 pub can_collide: bool,
155 pub can_collide_liquid: bool,
157 pub has_collision_box: bool,
159}
160
161#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
163pub struct Block(pub(crate) u8);
164
165impl Block {
166 #[must_use]
168 pub fn from_id(id: u8) -> Option<Block> {
169 (g::blocks::BLOCK_SLOT[id as usize] != 255).then_some(Block(id))
170 }
171
172 #[must_use]
174 pub fn from_name(name: &str) -> Option<Block> {
175 lookup_name(&g::blocks::BLOCK_NAMES, name).map(Block)
176 }
177
178 #[must_use]
180 pub const fn id(self) -> u8 {
181 self.0
182 }
183
184 #[must_use]
186 pub fn info(self) -> &'static BlockInfo {
187 &g::blocks::BLOCKS[g::blocks::BLOCK_SLOT[self.0 as usize] as usize]
188 }
189
190 #[must_use]
192 pub fn name(self) -> &'static str {
193 self.info().name
194 }
195
196 #[must_use]
198 pub fn physics(self) -> &'static BlockPhysics {
199 &self.info().physics
200 }
201
202 #[must_use]
204 pub fn material(self) -> Material {
205 g::blocks::MATERIALS[g::blocks::BLOCK_MATERIAL[self.0 as usize] as usize]
206 }
207
208 #[must_use]
210 pub fn is_collidable(self) -> bool {
211 g::blocks::BLOCK_BITS[self.0 as usize] & 1 != 0
212 }
213
214 #[must_use]
217 pub fn fence_excluded(self) -> bool {
218 g::blocks::BLOCK_BITS[self.0 as usize] & 2 != 0
219 }
220
221 #[must_use]
223 pub fn wall_excluded(self) -> bool {
224 g::blocks::BLOCK_BITS[self.0 as usize] & 4 != 0
225 }
226
227 #[must_use]
229 pub fn pane_excluded(self) -> bool {
230 g::blocks::BLOCK_BITS[self.0 as usize] & 8 != 0
231 }
232
233 #[must_use]
235 pub fn shape_dependency(self) -> ShapeDependency {
236 g::blocks::BLOCK_SHAPE_DEPENDENCY[self.0 as usize]
237 }
238
239 #[must_use]
241 pub const fn with_meta(self, meta: u8) -> BlockState {
242 BlockState(((self.0 as u16) << 4) | (meta & 15) as u16)
243 }
244
245 #[must_use]
247 pub const fn default_state(self) -> BlockState {
248 self.with_meta(0)
249 }
250
251 pub fn all() -> impl Iterator<Item = Block> {
253 (0..=255u8).filter_map(Block::from_id)
254 }
255}
256
257#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
259pub struct BlockState(u16);
260
261impl BlockState {
262 pub const AIR: BlockState = BlockState(0);
264
265 #[must_use]
267 pub fn from_raw(raw: u16) -> Option<BlockState> {
268 (raw < 4096 && g::blocks::BLOCK_SLOT[(raw >> 4) as usize] != 255).then_some(BlockState(raw))
269 }
270
271 #[must_use]
273 pub fn from_raw_vanilla(raw: u16) -> BlockState {
274 match BlockState::from_raw(raw) {
275 Some(s) if s.is_registered() => s,
276 _ => BlockState::AIR,
277 }
278 }
279
280 #[must_use]
282 pub fn is_registered(self) -> bool {
283 g::blocks::REGISTERED_METAS[(self.0 >> 4) as usize] & (1 << (self.0 & 15)) != 0
284 }
285
286 #[must_use]
288 pub const fn new(block: Block, meta: u8) -> BlockState {
289 block.with_meta(meta)
290 }
291
292 #[must_use]
294 pub const fn raw(self) -> u16 {
295 self.0
296 }
297
298 #[must_use]
300 pub const fn block(self) -> Block {
301 Block((self.0 >> 4) as u8)
302 }
303
304 #[must_use]
306 pub const fn meta(self) -> u8 {
307 (self.0 & 15) as u8
308 }
309
310 #[must_use]
312 pub const fn is_air(self) -> bool {
313 self.0 >> 4 == 0
314 }
315
316 #[must_use]
319 pub fn collision(self) -> Collision {
320 Collision {
321 boxes: self.shape(),
322 dependency: g::blocks::BLOCK_SHAPE_DEPENDENCY[(self.0 >> 4) as usize],
323 world_coords: None,
324 }
325 }
326
327 #[must_use]
329 pub fn shape(self) -> &'static [Aabb] {
330 g::blocks::SHAPES[g::blocks::STATE_SHAPE[self.0 as usize] as usize]
331 }
332
333 #[must_use]
335 pub fn flags(self) -> StateFlags {
336 StateFlags(g::blocks::STATE_FLAGS[self.0 as usize])
337 }
338
339 #[must_use]
341 pub fn is_normal_cube(self) -> bool {
342 self.flags().contains(StateFlags::NORMAL_CUBE)
343 }
344
345 #[must_use]
347 pub fn is_full_cube(self) -> bool {
348 self.flags().contains(StateFlags::FULL_CUBE)
349 }
350
351 #[must_use]
354 pub fn face_shape(self, face: Face) -> FaceShape {
355 let v = g::blocks::STATE_FACES[self.0 as usize] >> (4 * face as u32);
356 FACE_SHAPES[(v & 15) as usize]
357 }
358
359 #[must_use]
361 pub fn selection(self) -> Selection {
362 let f = g::blocks::STATE_SELECTION_FLAGS[self.0 as usize];
363 let dependency = g::blocks::BLOCK_SHAPE_DEPENDENCY[(self.0 >> 4) as usize];
364 Selection {
365 bounds: g::blocks::SELECTION_BOUNDS
366 [g::blocks::STATE_SELECTION[self.0 as usize] as usize],
367 dependency,
368 multi_pass_ray: dependency == ShapeDependency::Neighbours(NeighbourShape::Stairs),
369 can_collide: f & 1 != 0,
370 can_collide_liquid: f & 2 != 0,
371 has_collision_box: f & 4 != 0,
372 }
373 }
374
375 #[must_use]
377 pub fn fluid(self) -> Option<Fluid> {
378 let flags = self.block().physics().flags;
379 let kind = if flags.contains(PhysicsFlags::WATER) {
380 FluidKind::Water
381 } else if flags.contains(PhysicsFlags::LAVA) {
382 FluidKind::Lava
383 } else {
384 return None;
385 };
386 Some(Fluid {
387 kind,
388 level: self.meta(),
389 })
390 }
391}
392
393#[must_use]
396#[allow(clippy::cast_precision_loss)]
397pub fn slime_dimensions(n: i32) -> crate::common::EntityDimensions {
398 let s = 0.510_000_05_f32 * n as f32;
399 crate::common::EntityDimensions {
400 width: s,
401 height: s,
402 eye_height: 0.625 * s,
403 }
404}
405
406crate::macros::common_api!();