mc173/block/
lever.rs

1//! Lever special functions for metadata.
2
3use crate::geom::Face;
4
5
6/// The the face the lever is connected to. In b1.7.3, levers can only attach to X/Z and
7/// bottom Y. This function also returns the secondary face where this lever's stick 
8/// points to when not active.
9#[inline]
10pub fn get_face(metadata: u8) -> Option<(Face, Face)> {
11    Some(match metadata & 7 {
12        1 => (Face::NegX, Face::PosY),
13        2 => (Face::PosX, Face::PosY),
14        3 => (Face::NegZ, Face::PosY),
15        4 => (Face::PosZ, Face::PosY),
16        5 => (Face::NegY, Face::PosZ),
17        6 => (Face::NegY, Face::PosX),
18        _ => return None
19    })
20}
21
22/// Set the face the lever is connected to and the direction of the lever's stick when
23/// not active, not that X/Z faces forces the direction to positive Y. Only positive X/Z
24/// should be used when facing bottom, other values will be forced to positive Z.
25#[inline]
26pub fn set_face(metadata: &mut u8, face: Face, dir: Face) {
27    *metadata &= !7;
28    *metadata |= match (face, dir) {
29        (Face::NegY, Face::PosZ) => 5,
30        (Face::NegY, _) => 6,
31        (Face::PosY, _) => 0,
32        (Face::NegZ, _) => 3,
33        (Face::PosZ, _) => 4,
34        (Face::NegX, _) => 1,
35        (Face::PosX, _) => 2,
36    }
37}
38
39/// Return true if the lever is currently active.
40#[inline]
41pub fn is_active(metadata: u8) -> bool {
42    metadata & 8 != 0
43}
44
45/// Set the lever active or not.
46#[inline]
47pub fn set_active(metadata: &mut u8, active: bool) {
48    *metadata &= !8;
49    *metadata |= (active as u8) << 3;
50}