1use crate::common::{
11 Aabb, BiomeInfo, BlockInfo, BlockPhysics, Collision, DigMaterial, EffectInfo, EnchantmentInfo,
12 EntityInfo, EntityKind, Fluid, FluidKind, FoodInfo, ItemInfo, SelectionShape, ShapeDependency,
13 lookup_id, lookup_name,
14};
15use crate::generated::v1_16_5 as g;
16
17pub mod dig;
18use core::fmt;
19
20pub use g::blocks::{Material, Property};
21
22pub const BLOCK_COUNT: usize = g::blocks::BLOCKS.len();
24pub const STATE_COUNT: usize = g::blocks::STATE_BLOCK.len();
26
27impl Property {
28 #[must_use]
30 pub fn from_name(name: &str) -> Option<Property> {
31 lookup_name(&g::blocks::PROPERTY_NAMES, name)
32 }
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub struct PropertyDef {
38 pub property: Property,
40 pub values: &'static [&'static str],
42}
43
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46pub struct StateLayout {
47 pub min_state: u16,
49 pub state_count: u16,
51 pub default_state: u16,
53 pub(crate) properties: &'static [u8],
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
60pub enum OffsetType {
61 None,
63 Xz,
65 Xyz,
67}
68
69#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
71pub struct VanillaBlock {
72 pub collidable: bool,
74 pub offset: OffsetType,
76}
77
78#[derive(Clone, Copy, Debug, PartialEq)]
85pub struct ScaffoldingShapes {
86 pub stable: &'static [Aabb],
88 pub bottom: &'static [Aabb],
90 pub stable_voxel: &'static VoxelShapeRef,
92 pub bottom_voxel: &'static VoxelShapeRef,
94 pub has_bottom: bool,
96}
97
98impl ScaffoldingShapes {
99 #[must_use]
102 pub fn select(
103 &self,
104 feet_min_y: f64,
105 block_y: i32,
106 descending: bool,
107 ) -> (&'static [Aabb], &'static VoxelShapeRef) {
108 let eps = f64::from(1.0e-5_f32);
109 if feet_min_y > f64::from(block_y) + 1.0 - eps && !descending {
110 (self.stable, self.stable_voxel)
111 } else if self.has_bottom && feet_min_y > f64::from(block_y) + 0.0 - eps {
112 (self.bottom, self.bottom_voxel)
113 } else {
114 (&[], &g::blocks::VOXEL_SHAPES[0])
115 }
116 }
117}
118
119#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
121pub enum VoxelClass {
122 Simple,
124 Array,
126}
127
128#[derive(Clone, Copy, Debug, PartialEq)]
130pub struct VoxelShapeRef {
131 pub class: VoxelClass,
133 pub sizes: [u8; 3],
135 pub xs: &'static [f64],
137 pub ys: &'static [f64],
139 pub zs: &'static [f64],
141 pub cells: &'static [u64],
143 pub empty: bool,
145}
146
147impl VoxelShapeRef {
148 #[must_use]
150 pub fn filled(&self, x: usize, y: usize, z: usize) -> bool {
151 let [nx, ny, nz] = self.sizes.map(usize::from);
152 if x >= nx || y >= ny || z >= nz {
153 return false;
154 }
155 let i = (x * ny + y) * nz + z;
156 self.cells[i / 64] & (1 << (i % 64)) != 0
157 }
158}
159
160#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
162pub enum Side {
163 North = 0,
165 East = 1,
167 South = 2,
169 West = 3,
171 Up = 4,
173 Down = 5,
175}
176
177#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
179pub struct Block(pub(crate) u16);
180
181impl Block {
182 #[must_use]
184 pub const fn from_id(id: u16) -> Option<Block> {
185 if (id as usize) < BLOCK_COUNT {
186 Some(Block(id))
187 } else {
188 None
189 }
190 }
191
192 #[must_use]
194 pub fn from_name(name: &str) -> Option<Block> {
195 lookup_name(&g::blocks::BLOCK_NAMES, name).map(Block)
196 }
197
198 #[must_use]
200 pub const fn id(self) -> u16 {
201 self.0
202 }
203
204 #[must_use]
206 pub fn info(self) -> &'static BlockInfo {
207 &g::blocks::BLOCKS[self.0 as usize]
208 }
209
210 #[must_use]
212 pub fn name(self) -> &'static str {
213 self.info().name
214 }
215
216 #[must_use]
218 pub fn physics(self) -> &'static BlockPhysics {
219 &g::blocks::BLOCKS[self.0 as usize].physics
220 }
221
222 #[must_use]
224 pub fn shape_dependency(self) -> ShapeDependency {
225 g::blocks::BLOCK_SHAPE_DEPENDENCY[self.0 as usize]
226 }
227
228 #[must_use]
230 pub const fn is_air(self) -> bool {
231 matches!(self, Block::AIR | Block::CAVE_AIR | Block::VOID_AIR)
232 }
233
234 #[must_use]
236 pub const fn is_bed(self) -> bool {
237 self.0 >= Block::WHITE_BED.0 && self.0 <= Block::BLACK_BED.0
238 }
239
240 #[must_use]
242 pub fn material(self) -> Material {
243 g::blocks::BLOCK_MATERIAL[self.0 as usize]
244 }
245
246 #[must_use]
248 pub fn vanilla(self) -> &'static VanillaBlock {
249 &g::blocks::VANILLA_BLOCKS[self.0 as usize]
250 }
251
252 #[must_use]
254 pub fn layout(self) -> &'static StateLayout {
255 &g::blocks::LAYOUTS[self.0 as usize]
256 }
257
258 #[must_use]
260 pub fn default_state(self) -> BlockState {
261 BlockState(self.layout().default_state)
262 }
263
264 pub fn states(self) -> impl ExactSizeIterator<Item = BlockState> {
266 let l = self.layout();
267 (l.min_state..l.min_state + l.state_count).map(BlockState)
268 }
269
270 pub fn properties(self) -> impl ExactSizeIterator<Item = &'static PropertyDef> {
272 self.layout()
273 .properties
274 .iter()
275 .map(|&i| &g::blocks::PROPERTY_DEFS[i as usize])
276 }
277
278 #[must_use]
281 pub fn state_with(self, props: &[(Property, &str)]) -> Option<BlockState> {
282 props
283 .iter()
284 .try_fold(self.default_state(), |s, &(p, v)| s.with(p, v))
285 }
286
287 pub fn all() -> impl ExactSizeIterator<Item = Block> {
289 (0..BLOCK_COUNT as u16).map(Block)
290 }
291}
292
293#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
295pub struct BlockState(u16);
296
297impl BlockState {
298 pub const AIR: BlockState = BlockState(0);
300
301 #[must_use]
303 pub const fn from_id(id: u16) -> Option<BlockState> {
304 if (id as usize) < STATE_COUNT {
305 Some(BlockState(id))
306 } else {
307 None
308 }
309 }
310
311 #[must_use]
313 pub const fn id(self) -> u16 {
314 self.0
315 }
316
317 #[must_use]
319 pub fn block(self) -> Block {
320 Block(g::blocks::STATE_BLOCK[self.0 as usize])
321 }
322
323 #[must_use]
325 pub fn is_air(self) -> bool {
326 self.block().is_air()
327 }
328
329 #[must_use]
331 pub fn collision(self) -> Collision {
332 Collision {
333 boxes: self.shape(),
334 dependency: g::blocks::BLOCK_SHAPE_DEPENDENCY
335 [g::blocks::STATE_BLOCK[self.0 as usize] as usize],
336 world_coords: None,
338 }
339 }
340
341 #[must_use]
343 pub fn full_cube(self) -> bool {
344 g::blocks::STATE_VANILLA_FLAGS[self.0 as usize] & 1 != 0
345 }
346
347 #[must_use]
349 pub fn solid_block(self) -> bool {
350 g::blocks::STATE_VANILLA_FLAGS[self.0 as usize] & 2 != 0
351 }
352
353 #[must_use]
355 pub fn should_suffocate(self) -> bool {
356 g::blocks::STATE_VANILLA_FLAGS[self.0 as usize] & 4 != 0
357 }
358
359 #[must_use]
361 pub fn voxel_shape(self) -> &'static VoxelShapeRef {
362 &g::blocks::VOXEL_SHAPES[g::blocks::STATE_VOXEL[self.0 as usize] as usize]
363 }
364
365 #[must_use]
369 pub fn outline_shape(self) -> &'static SelectionShape {
370 &g::selection::SELECTION_SHAPES[g::selection::STATE_OUTLINE[self.0 as usize] as usize]
371 }
372
373 #[must_use]
376 pub fn outline_shape_holding_own_item(self) -> &'static SelectionShape {
377 let t = &g::selection::OUTLINE_HOLDING_OWN_ITEM;
378 match t.binary_search_by_key(&self.0, |e| e.0) {
379 Ok(i) => &g::selection::SELECTION_SHAPES[t[i].1 as usize],
380 Err(_) => self.outline_shape(),
381 }
382 }
383
384 #[must_use]
386 pub fn outline_offset(self) -> bool {
387 g::selection::STATE_OUTLINE_OFFSET[self.0 as usize / 64] & (1 << (self.0 % 64)) != 0
388 }
389
390 #[must_use]
394 pub fn raycast_shape(self) -> &'static SelectionShape {
395 &g::selection::SELECTION_SHAPES[g::selection::STATE_INTERACTION[self.0 as usize] as usize]
396 }
397
398 fn voxel_flag(self, bit: u16) -> bool {
399 g::blocks::STATE_VOXEL_FLAGS[self.0 as usize] & bit != 0
400 }
401
402 #[must_use]
404 pub fn full_cube_identity(self) -> bool {
405 self.voxel_flag(1)
406 }
407
408 #[must_use]
410 pub fn empty_identity(self) -> bool {
411 self.voxel_flag(1 << 12)
412 }
413
414 #[must_use]
416 pub fn material_liquid(self) -> bool {
417 self.voxel_flag(1 << 13)
418 }
419
420 #[must_use]
422 pub fn material_solid(self) -> bool {
423 self.voxel_flag(1 << 14)
424 }
425
426 #[must_use]
428 pub fn material_replaceable(self) -> bool {
429 self.voxel_flag(1 << 15)
430 }
431
432 #[must_use]
434 pub fn exceeds_cube(self) -> bool {
435 self.voxel_flag(2)
436 }
437
438 #[must_use]
440 pub fn has_dynamic_bounds(self) -> bool {
441 self.voxel_flag(16)
442 }
443
444 #[must_use]
446 pub fn material_blocks_movement(self) -> bool {
447 self.voxel_flag(32)
448 }
449
450 #[must_use]
452 pub fn side_solid_full_square(self, side: Side) -> bool {
453 self.voxel_flag(64 << side as u16)
454 }
455
456 #[must_use]
458 pub fn scaffolding_shapes(self) -> Option<ScaffoldingShapes> {
459 if self.block() != Block::SCAFFOLDING {
460 return None;
461 }
462 let has_bottom =
463 self.get(Property::Bottom) == Some("true") && self.get(Property::Distance) != Some("0");
464 Some(ScaffoldingShapes {
465 stable: self.shape(),
466 bottom: if has_bottom {
467 g::blocks::SCAFFOLDING_BOTTOM
468 } else {
469 &[]
470 },
471 stable_voxel: self.voxel_shape(),
472 has_bottom,
473 bottom_voxel: &g::blocks::VOXEL_SHAPES[if has_bottom {
474 g::blocks::SCAFFOLDING_BOTTOM_VOXEL as usize
475 } else {
476 0
477 }],
478 })
479 }
480
481 #[must_use]
483 pub fn shape(self) -> &'static [Aabb] {
484 g::blocks::SHAPES[g::blocks::STATE_SHAPE[self.0 as usize] as usize]
485 }
486
487 #[must_use]
490 pub fn fluid(self) -> Option<Fluid> {
491 let f = g::blocks::STATE_FLUID[self.0 as usize];
492 let kind = match f >> 4 {
493 1 => FluidKind::Water,
494 2 => FluidKind::Lava,
495 _ => return None,
496 };
497 Some(Fluid {
498 kind,
499 level: f & 15,
500 })
501 }
502
503 #[must_use]
505 pub fn get_index(self, prop: Property) -> Option<usize> {
506 let layout = self.block().layout();
507 let mut rem = usize::from(self.0 - layout.min_state);
508 for &d in layout.properties.iter().rev() {
509 let def = &g::blocks::PROPERTY_DEFS[d as usize];
510 let n = def.values.len();
511 if def.property == prop {
512 return Some(rem % n);
513 }
514 rem /= n;
515 }
516 None
517 }
518
519 #[must_use]
521 pub fn get(self, prop: Property) -> Option<&'static str> {
522 let def = self.property_def(prop)?;
523 self.get_index(prop).map(|i| def.values[i])
524 }
525
526 #[must_use]
528 pub fn with(self, prop: Property, value: &str) -> Option<BlockState> {
529 let layout = self.block().layout();
530 let mut stride = 1usize;
531 for &d in layout.properties.iter().rev() {
532 let def = &g::blocks::PROPERTY_DEFS[d as usize];
533 let n = def.values.len();
534 if def.property == prop {
535 let new = def.values.iter().position(|v| *v == value)?;
536 let base = usize::from(self.0 - layout.min_state);
537 let old = base / stride % n;
538 let id = base - old * stride + new * stride + usize::from(layout.min_state);
539 return Some(BlockState(id as u16));
540 }
541 stride *= n;
542 }
543 None
544 }
545
546 pub fn properties(self) -> impl Iterator<Item = (Property, &'static str)> {
548 let block = self.block();
549 block
550 .properties()
551 .filter_map(move |d| self.get(d.property).map(|v| (d.property, v)))
552 }
553
554 fn property_def(self, prop: Property) -> Option<&'static PropertyDef> {
555 self.block().properties().find(|d| d.property == prop)
556 }
557}
558
559impl fmt::Display for BlockState {
561 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
562 write!(f, "minecraft:{}", self.block().name())?;
563 let mut first = true;
564 for (p, v) in self.properties() {
565 f.write_str(if first { "[" } else { "," })?;
566 write!(f, "{}={v}", p.name())?;
567 first = false;
568 }
569 if !first {
570 f.write_str("]")?;
571 }
572 Ok(())
573 }
574}
575
576#[must_use]
579#[allow(clippy::cast_precision_loss)]
580pub fn slime_dimensions(n: i32) -> crate::common::EntityDimensions {
581 let ratio = 0.255_f32 * n as f32;
582 let s = 2.04_f32 * ratio;
583 crate::common::EntityDimensions {
584 width: s,
585 height: s,
586 eye_height: 0.625 * s,
587 }
588}
589
590crate::macros::common_api!();