Skip to main content

ghast_data/v1_12/
mod.rs

1//! Minecraft 1.12.x (protocol 335 – 340) data: numeric block ids with 4-bit metadata.
2//!
3//! As in 1.8, a block state travels on the wire and in chunk palettes as one `u16`:
4//! `block_id << 4 | meta` ([`BlockState::raw`]). Block ids are `0..=255` (253 and 254 are unused).
5//! Every table here comes from the vanilla 1.12.2 oracle (`tests/golden/1.12.2/`): collision boxes
6//! per (id, meta) with air neighbours, the per-state shape flags, block materials, ray-test bounds
7//! and the 1.12 "face shapes" fences, walls and panes connect by.
8//!
9//! Unlike 1.8 there are no history-dependent shapes: 1.12 builds every box from constants and the
10//! block's actual state. Blocks whose actual state depends on neighbours (fences, panes, walls,
11//! stairs, doors, chests, chorus plants) report a [`ShapeDependency::Neighbours`]; their table
12//! shape is the one vanilla has with only air around.
13//!
14//! Unregistered combinations (an unknown id, or a meta the block never produces) read as air, as
15//! vanilla's global palette maps them: [`BlockState::from_raw_vanilla`].
16
17use 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
26/// Size of the block id space (`0..=255`; not every id is a block, see [`Block::from_id`]).
27pub const BLOCK_ID_SPACE: usize = 256;
28
29/// Identity of the vanilla materials that 1.12 movement code compares with `==`.
30#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
31pub enum MaterialKind {
32    /// `Material.AIR`.
33    Air,
34    /// `Material.WATER`.
35    Water,
36    /// `Material.LAVA`.
37    Lava,
38    /// `Material.ICE` (only `ice`; packed ice and frosted ice have their own).
39    Ice,
40    /// Any other material.
41    Other,
42}
43
44/// A block's vanilla 1.12.2 material (flags from the oracle).
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
46pub struct Material {
47    /// Our name for the material (from the Yarn field name, e.g. `"stone"`, `"wood"`).
48    pub name: &'static str,
49    /// Identity, for `== Material.X` checks.
50    pub kind: MaterialKind,
51    /// `Material.blocksMovement()`.
52    pub blocks_movement: bool,
53    /// `Material.isSolid()`.
54    pub solid: bool,
55    /// `Material.isLiquid()`.
56    pub liquid: bool,
57    /// `Material.isOpaque()`.
58    pub opaque: bool,
59    /// `Material.isReplaceable()`.
60    pub replaceable: bool,
61}
62
63/// Per-state shape flags (1.12 moved these from the block to the state).
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
65pub struct StateFlags(pub u8);
66
67impl StateFlags {
68    /// `BlockState.isFullCube()` (Legacy Yarn `method_11730`).
69    pub const FULL_CUBE: StateFlags = StateFlags(1 << 0);
70    /// `BlockState.isNormalCube()` (`method_11734`): opaque material, full cube, no redstone power.
71    /// `ClientPlayerEntity.pushOutOfBlocks` tests it.
72    pub const NORMAL_CUBE: StateFlags = StateFlags(1 << 1);
73    /// `BlockState.isBlockNormalCube()` (`method_11733`): material blocks movement and full cube.
74    pub const BLOCK_NORMAL_CUBE: StateFlags = StateFlags(1 << 2);
75    /// `BlockState.isTopSolid()` (`method_11739`).
76    pub const TOP_SOLID: StateFlags = StateFlags(1 << 3);
77    /// `BlockState.isOpaqueCube()` (`isFullBoundsCubeForCulling`).
78    pub const OPAQUE_CUBE: StateFlags = StateFlags(1 << 4);
79    /// The `Block.fullBlock` field.
80    pub const FULL_BLOCK: StateFlags = StateFlags(1 << 5);
81    /// `BlockState.causesSuffocation()` (`method_13763`).
82    pub const CAUSES_SUFFOCATION: StateFlags = StateFlags(1 << 6);
83
84    /// `true` when every flag of `other` is set.
85    #[must_use]
86    pub const fn contains(self, other: StateFlags) -> bool {
87        self.0 & other.0 == other.0
88    }
89}
90
91/// A block face (`Direction` order: down, up, north, south, west, east).
92#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
93pub enum Face {
94    /// `-Y`.
95    Down,
96    /// `+Y`.
97    Up,
98    /// `-Z`.
99    North,
100    /// `+Z`.
101    South,
102    /// `-X`.
103    West,
104    /// `+X`.
105    East,
106}
107
108/// What a block face looks like to connecting blocks (vanilla 1.12 `BlockFaceShape`; Legacy Yarn
109/// names the enum `BlockRenderLayer`).
110#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
111pub enum FaceShape {
112    /// A full solid face.
113    Solid,
114    /// `BOWL` (cauldron, hopper top).
115    Bowl,
116    /// `CENTER_SMALL`.
117    CenterSmall,
118    /// `MIDDLE_POLE_THIN` (panes, iron bars).
119    MiddlePoleThin,
120    /// `CENTER`.
121    Center,
122    /// `MIDDLE_POLE` (fences, fence gates).
123    MiddlePole,
124    /// `CENTER_BIG`.
125    CenterBig,
126    /// `MIDDLE_POLE_THICK` (walls).
127    MiddlePoleThick,
128    /// `UNDEFINED`.
129    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/// What the 1.12.2 crosshair ray test sees of a block state.
145#[derive(Clone, Copy, Debug, PartialEq)]
146pub struct Selection {
147    /// `getBoundingBox(state, world, pos)` with all neighbours air (block-local).
148    pub bounds: Aabb,
149    /// Same classes as the collision dependency.
150    pub dependency: ShapeDependency,
151    /// Stairs: the ray test runs per collision box instead of using `bounds`.
152    pub multi_pass_ray: bool,
153    /// `Block.canCollideCheck(state, false)`.
154    pub can_collide: bool,
155    /// `Block.canCollideCheck(state, true)` (ray tests that stop at fluids).
156    pub can_collide_liquid: bool,
157    /// The state has a collision box.
158    pub has_collision_box: bool,
159}
160
161/// A 1.12 block type (registry id `0..=255`).
162#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
163pub struct Block(pub(crate) u8);
164
165impl Block {
166    /// The block with this registry id, if it exists in 1.12.
167    #[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    /// Looks a block up by registry name (`"stone"`, no namespace; minecraft-data names).
173    #[must_use]
174    pub fn from_name(name: &str) -> Option<Block> {
175        lookup_name(&g::blocks::BLOCK_NAMES, name).map(Block)
176    }
177
178    /// Registry id.
179    #[must_use]
180    pub const fn id(self) -> u8 {
181        self.0
182    }
183
184    /// Static information (names, hardness, drops, physics).
185    #[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    /// Registry name.
191    #[must_use]
192    pub fn name(self) -> &'static str {
193        self.info().name
194    }
195
196    /// Physics properties (slipperiness, climbable, ...).
197    #[must_use]
198    pub fn physics(self) -> &'static BlockPhysics {
199        &self.info().physics
200    }
201
202    /// Vanilla material.
203    #[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    /// `Block.isCollidable()`: ray tests skip the block when false.
209    #[must_use]
210    pub fn is_collidable(self) -> bool {
211        g::blocks::BLOCK_BITS[self.0 as usize] & 1 != 0
212    }
213
214    /// Fences never connect to this block's solid faces (`FenceBlock`'s exception list: leaves,
215    /// glass, shulker boxes, pistons, barrier, pumpkins, melon, ...).
216    #[must_use]
217    pub fn fence_excluded(self) -> bool {
218        g::blocks::BLOCK_BITS[self.0 as usize] & 2 != 0
219    }
220
221    /// Walls never connect to this block's solid faces.
222    #[must_use]
223    pub fn wall_excluded(self) -> bool {
224        g::blocks::BLOCK_BITS[self.0 as usize] & 4 != 0
225    }
226
227    /// Panes and iron bars never connect to this block's solid faces.
228    #[must_use]
229    pub fn pane_excluded(self) -> bool {
230        g::blocks::BLOCK_BITS[self.0 as usize] & 8 != 0
231    }
232
233    /// What the collision shape depends on besides (id, meta).
234    #[must_use]
235    pub fn shape_dependency(self) -> ShapeDependency {
236        g::blocks::BLOCK_SHAPE_DEPENDENCY[self.0 as usize]
237    }
238
239    /// The state with this metadata (`meta & 15`).
240    #[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    /// The state with metadata 0.
246    #[must_use]
247    pub const fn default_state(self) -> BlockState {
248        self.with_meta(0)
249    }
250
251    /// All blocks in id order.
252    pub fn all() -> impl Iterator<Item = Block> {
253        (0..=255u8).filter_map(Block::from_id)
254    }
255}
256
257/// A 1.12 block state: `block_id << 4 | meta`, exactly the wire / palette value.
258#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
259pub struct BlockState(u16);
260
261impl BlockState {
262    /// Air (raw 0).
263    pub const AIR: BlockState = BlockState(0);
264
265    /// Wraps a raw `id << 4 | meta` value; `None` if the id is not a 1.12 block.
266    #[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    /// Vanilla's palette mapping: unknown block ids and unregistered metas become air.
272    #[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    /// `true` when vanilla 1.12.2 has a block state for this (id, meta); otherwise it reads as air.
281    #[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    /// Builds a state from a block and metadata (`meta & 15`).
287    #[must_use]
288    pub const fn new(block: Block, meta: u8) -> BlockState {
289        block.with_meta(meta)
290    }
291
292    /// The raw `id << 4 | meta` value.
293    #[must_use]
294    pub const fn raw(self) -> u16 {
295        self.0
296    }
297
298    /// The block type.
299    #[must_use]
300    pub const fn block(self) -> Block {
301        Block((self.0 >> 4) as u8)
302    }
303
304    /// Metadata `0..=15`.
305    #[must_use]
306    pub const fn meta(self) -> u8 {
307        (self.0 & 15) as u8
308    }
309
310    /// `true` for air.
311    #[must_use]
312    pub const fn is_air(self) -> bool {
313        self.0 >> 4 == 0
314    }
315
316    /// Collision boxes and what else they depend on (1.12 places them in `double`:
317    /// [`Aabb::at`]).
318    #[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    /// The collision boxes from the table (for dependent shapes: the shape with air neighbours).
328    #[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    /// Per-state shape flags.
334    #[must_use]
335    pub fn flags(self) -> StateFlags {
336        StateFlags(g::blocks::STATE_FLAGS[self.0 as usize])
337    }
338
339    /// `BlockState.isNormalCube()` (see [`StateFlags::NORMAL_CUBE`]).
340    #[must_use]
341    pub fn is_normal_cube(self) -> bool {
342        self.flags().contains(StateFlags::NORMAL_CUBE)
343    }
344
345    /// `BlockState.isFullCube()`.
346    #[must_use]
347    pub fn is_full_cube(self) -> bool {
348        self.flags().contains(StateFlags::FULL_CUBE)
349    }
350
351    /// The face shape of `face` with only air around (stairs: the straight shape; their real
352    /// face shape follows the actual stairs shape).
353    #[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    /// Crosshair ray-test data (oracle `selection_boxes.jsonl.gz`).
360    #[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    /// Fluid in this state (water / lava blocks; `level` = meta).
376    #[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/// 1.12.2 slime / magma cube dimensions for metadata size `n` (`0.51000005F * n` square, eye height
394/// `0.625F * height`), as in 1.8.
395#[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!();