1use crate::common::{Aabb, SelectionShape, lookup_name};
14use crate::generated::v26_1 as g;
15use core::fmt;
16
17pub use g::blocks::Property;
18
19pub mod dig;
20pub mod entities;
21pub mod items;
22
23pub use entities::{
24 EntityTypeData, entities, entity, entity_by_name, entity_type, entity_types,
25 vanilla_entity_size, vanilla_entity_sizes,
26};
27pub use items::{BlockSet, Food, Item, ItemData, Tool, ToolRule, food};
28
29pub const BLOCK_COUNT: usize = g::blocks::BLOCKS.len();
31pub const STATE_COUNT: usize = g::blocks::STATE_BLOCK.len();
33
34impl Property {
35 #[must_use]
37 pub fn from_name(name: &str) -> Option<Property> {
38 g::blocks::PROPERTY_NAMES
39 .binary_search_by(|e| e.0.cmp(name))
40 .ok()
41 .map(|i| g::blocks::PROPERTY_NAMES[i].1)
42 }
43}
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47pub struct PropertyDef {
48 pub property: Property,
50 pub values: &'static [&'static str],
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub struct StateLayout {
57 pub min_state: u16,
59 pub state_count: u16,
61 pub default_state: u16,
63 pub(crate) properties: &'static [u8],
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
69#[non_exhaustive]
70pub enum BlockKind {
71 Other = 0,
73 Cobweb,
75 SweetBerryBush,
77 PowderSnow,
79 HoneyBlock,
81 BubbleColumn,
83 SlimeBlock,
85 Scaffolding,
87 Ladder,
89 Water,
91 Lava,
93 MovingPiston,
95}
96
97pub mod block_flags {
99 pub const CLIMBABLE: u32 = 1;
101 pub const FENCES: u32 = 2;
103 pub const WALLS: u32 = 4;
105 pub const FENCE_GATE: u32 = 8;
107 pub const TRAPDOOR: u32 = 16;
109 pub const ICE: u32 = 32;
111 pub const BED: u32 = 64;
113 pub const FALL_DAMAGE_RESETTING: u32 = 128;
115 pub const SOUL_SPEED_BLOCKS: u32 = 256;
117 pub const FIRE: u32 = 512;
119}
120
121#[derive(Clone, Copy, Debug, PartialEq)]
123pub struct BlockData {
124 pub name: &'static str,
126 #[cfg(feature = "display-names")]
128 pub display_name: &'static str,
129 pub hardness: Option<f32>,
131 pub resistance: f32,
133 pub diggable: bool,
135 pub transparent: bool,
137 pub emit_light: u8,
139 pub filter_light: u8,
141 pub friction: f32,
143 pub speed_factor: f32,
145 pub jump_factor: f32,
147 pub flags: u32,
149 pub kind: BlockKind,
151 pub offset: Option<(f32, f32, bool)>,
154}
155
156#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
158pub enum VoxelClass {
159 Simple,
161 Array,
163}
164
165#[derive(Clone, Copy, Debug, PartialEq)]
167pub struct VoxelShapeRef {
168 pub class: VoxelClass,
170 pub sizes: [u8; 3],
172 pub xs: &'static [f64],
174 pub ys: &'static [f64],
176 pub zs: &'static [f64],
178 pub cells: &'static [u64],
180 pub empty: bool,
182 pub boxes: &'static [Aabb],
184}
185
186impl VoxelShapeRef {
187 #[must_use]
189 pub fn filled(&self, x: usize, y: usize, z: usize) -> bool {
190 let [nx, ny, nz] = self.sizes.map(usize::from);
191 if x >= nx || y >= ny || z >= nz {
192 return false;
193 }
194 let i = (x * ny + y) * nz + z;
195 self.cells[i / 64] & (1 << (i % 64)) != 0
196 }
197}
198
199#[must_use]
201pub fn voxel_shapes() -> &'static [VoxelShapeRef] {
202 &g::blocks::VOXEL_SHAPES
203}
204
205pub const FULL_CUBE_VOXEL: u16 = g::blocks::FULL_CUBE_VOXEL;
207pub const SCAFFOLDING_BOTTOM_VOXEL: u16 = g::blocks::SCAFFOLDING_BOTTOM_VOXEL;
209pub const POWDER_SNOW_FALLING_VOXEL: u16 = g::blocks::POWDER_SNOW_FALLING_VOXEL;
211
212#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
214pub struct StatePhys(pub u64);
215
216pub mod state_flags {
218 pub const FULL_CUBE: u16 = 1;
220 pub const EMPTY: u16 = 2;
222 pub const LARGE: u16 = 4;
224 pub const SUFFOCATING: u16 = 8;
226 pub const AIR: u16 = 16;
228 pub const BLOCKS_MOTION: u16 = 32;
230 pub const STURDY_NORTH: u16 = 64;
232 pub const CONTEXT: u16 = 1 << 10;
234 pub const OFFSET: u16 = 1 << 11;
237 pub const MOVING_PISTON: u16 = 1 << 12;
239}
240
241impl StatePhys {
242 #[inline]
244 #[must_use]
245 pub const fn shape(self) -> u16 {
246 self.0 as u16
247 }
248
249 #[inline]
251 #[must_use]
252 pub const fn flags(self) -> u16 {
253 (self.0 >> 16) as u16
254 }
255
256 #[inline]
258 #[must_use]
259 pub const fn has(self, bits: u16) -> bool {
260 self.flags() & bits == bits
261 }
262
263 #[inline]
265 #[must_use]
266 pub const fn fluid(self) -> u8 {
267 (self.0 >> 32) as u8
268 }
269
270 #[inline]
272 #[must_use]
273 pub const fn aux(self) -> u8 {
274 (self.0 >> 40) as u8
275 }
276
277 #[inline]
279 #[must_use]
280 pub const fn kind(self) -> u8 {
281 (self.0 >> 48) as u8
282 }
283}
284
285#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
287pub struct Block(pub(crate) u16);
288
289impl Block {
290 #[must_use]
292 pub const fn from_id(id: u16) -> Option<Block> {
293 if (id as usize) < BLOCK_COUNT {
294 Some(Block(id))
295 } else {
296 None
297 }
298 }
299
300 #[must_use]
302 pub fn from_name(name: &str) -> Option<Block> {
303 lookup_name(&g::blocks::BLOCK_NAMES, name).map(Block)
304 }
305
306 #[must_use]
308 pub const fn id(self) -> u16 {
309 self.0
310 }
311
312 #[inline]
314 #[must_use]
315 pub fn data(self) -> &'static BlockData {
316 &g::blocks::BLOCKS[self.0 as usize]
317 }
318
319 #[must_use]
321 pub fn name(self) -> &'static str {
322 self.data().name
323 }
324
325 #[must_use]
327 pub const fn is_air(self) -> bool {
328 matches!(self, Block::AIR | Block::CAVE_AIR | Block::VOID_AIR)
329 }
330
331 #[must_use]
333 pub fn layout(self) -> &'static StateLayout {
334 &g::blocks::LAYOUTS[self.0 as usize]
335 }
336
337 #[must_use]
339 pub fn default_state(self) -> BlockState {
340 BlockState(self.layout().default_state)
341 }
342
343 pub fn states(self) -> impl ExactSizeIterator<Item = BlockState> {
345 let l = self.layout();
346 (l.min_state..l.min_state + l.state_count).map(BlockState)
347 }
348
349 pub fn properties(self) -> impl ExactSizeIterator<Item = &'static PropertyDef> {
351 self.layout()
352 .properties
353 .iter()
354 .map(|&i| &g::blocks::PROPERTY_DEFS[i as usize])
355 }
356
357 #[must_use]
360 pub fn state_with(self, props: &[(Property, &str)]) -> Option<BlockState> {
361 props
362 .iter()
363 .try_fold(self.default_state(), |s, &(p, v)| s.with(p, v))
364 }
365
366 pub fn all() -> impl ExactSizeIterator<Item = Block> {
368 (0..BLOCK_COUNT as u16).map(Block)
369 }
370}
371
372#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
374pub struct BlockState(u16);
375
376impl BlockState {
377 pub const AIR: BlockState = BlockState(0);
379
380 #[must_use]
382 pub const fn from_id(id: u16) -> Option<BlockState> {
383 if (id as usize) < STATE_COUNT {
384 Some(BlockState(id))
385 } else {
386 None
387 }
388 }
389
390 #[inline]
392 #[must_use]
393 pub const fn id(self) -> u16 {
394 self.0
395 }
396
397 #[inline]
399 #[must_use]
400 pub fn block(self) -> Block {
401 Block(g::blocks::STATE_BLOCK[self.0 as usize])
402 }
403
404 #[must_use]
406 pub fn is_air(self) -> bool {
407 self.phys().has(state_flags::AIR)
408 }
409
410 #[inline]
412 #[must_use]
413 pub fn phys(self) -> StatePhys {
414 StatePhys(g::blocks::STATE_PHYS[self.0 as usize])
415 }
416
417 #[must_use]
419 pub fn voxel_shape(self) -> &'static VoxelShapeRef {
420 &g::blocks::VOXEL_SHAPES[self.phys().shape() as usize]
421 }
422
423 #[must_use]
427 pub fn outline_shape(self) -> &'static SelectionShape {
428 &g::selection::SELECTION_SHAPES[g::selection::STATE_OUTLINE[self.0 as usize] as usize]
429 }
430
431 #[must_use]
434 pub fn outline_shape_holding_own_item(self) -> &'static SelectionShape {
435 let t = &g::selection::OUTLINE_HOLDING_OWN_ITEM;
436 match t.binary_search_by_key(&self.0, |e| e.0) {
437 Ok(i) => &g::selection::SELECTION_SHAPES[t[i].1 as usize],
438 Err(_) => self.outline_shape(),
439 }
440 }
441
442 #[must_use]
444 pub fn outline_offset(self) -> bool {
445 g::selection::STATE_OUTLINE_OFFSET[self.0 as usize / 64] & (1 << (self.0 % 64)) != 0
446 }
447
448 #[must_use]
452 pub fn interaction_shape(self) -> &'static SelectionShape {
453 &g::selection::SELECTION_SHAPES[g::selection::STATE_INTERACTION[self.0 as usize] as usize]
454 }
455
456 #[must_use]
458 pub fn get_index(self, prop: Property) -> Option<usize> {
459 let layout = self.block().layout();
460 let mut rem = usize::from(self.0 - layout.min_state);
461 for &d in layout.properties.iter().rev() {
462 let def = &g::blocks::PROPERTY_DEFS[d as usize];
463 let n = def.values.len();
464 if def.property == prop {
465 return Some(rem % n);
466 }
467 rem /= n;
468 }
469 None
470 }
471
472 #[must_use]
474 pub fn get(self, prop: Property) -> Option<&'static str> {
475 let def = self.block().properties().find(|d| d.property == prop)?;
476 self.get_index(prop).map(|i| def.values[i])
477 }
478
479 #[must_use]
481 pub fn with(self, prop: Property, value: &str) -> Option<BlockState> {
482 let layout = self.block().layout();
483 let mut stride = 1usize;
484 for &d in layout.properties.iter().rev() {
485 let def = &g::blocks::PROPERTY_DEFS[d as usize];
486 let n = def.values.len();
487 if def.property == prop {
488 let new = def.values.iter().position(|v| *v == value)?;
489 let base = usize::from(self.0 - layout.min_state);
490 let old = base / stride % n;
491 let id = base - old * stride + new * stride + usize::from(layout.min_state);
492 return Some(BlockState(id as u16));
493 }
494 stride *= n;
495 }
496 None
497 }
498
499 pub fn properties(self) -> impl Iterator<Item = (Property, &'static str)> {
501 let block = self.block();
502 block
503 .properties()
504 .filter_map(move |d| self.get(d.property).map(|v| (d.property, v)))
505 }
506
507 #[must_use]
509 pub fn parse(s: &str) -> Option<BlockState> {
510 let s = s.strip_prefix("minecraft:").unwrap_or(s);
511 let (name, props) = match s.split_once('[') {
512 Some((n, rest)) => (n, rest.strip_suffix(']')?),
513 None => (s, ""),
514 };
515 let mut st = Block::from_name(name)?.default_state();
516 for kv in props.split(',').filter(|p| !p.is_empty()) {
517 let (k, v) = kv.split_once('=')?;
518 st = st.with(Property::from_name(k)?, v)?;
519 }
520 Some(st)
521 }
522}
523
524impl fmt::Display for BlockState {
526 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527 write!(f, "minecraft:{}", self.block().name())?;
528 let mut first = true;
529 for (p, v) in self.properties() {
530 f.write_str(if first { "[" } else { "," })?;
531 write!(f, "{}={v}", p.name())?;
532 first = false;
533 }
534 if !first {
535 f.write_str("]")?;
536 }
537 Ok(())
538 }
539}
540
541#[cfg(test)]
542mod tests;