mc173/block/
mod.rs

1//! Block enumeration and functions to query their metadata state.
2
3use crate::item::Item;
4
5// Block behaviors.
6pub mod material;
7
8// Block specific functions for their metadata.
9pub mod dispenser;
10pub mod trapdoor;
11pub mod repeater;
12pub mod pumpkin;
13pub mod sapling;
14pub mod button;
15pub mod ladder;
16pub mod piston;
17pub mod lever;
18pub mod stair;
19pub mod torch;
20pub mod fluid;
21pub mod door;
22pub mod bed;
23
24
25/// Internal macro to easily define blocks registry.
26macro_rules! blocks {
27    (
28        $($ident:ident / $id:literal : $name:literal),* $(,)?
29    ) => {
30
31        static NAMES: [&'static str; 256] = {
32            let mut arr = [""; 256];
33            $(arr[$id as usize] = $name;)*
34            arr
35        };
36
37        const ITEMS: [Item; 256] = {
38            let mut arr = [Item {
39                name: "",
40                block: true,
41                max_stack_size: 64,
42                max_damage: 0,
43            }; 256];
44            $(arr[$id as usize].name = $name;)*
45            arr
46        };
47
48        $(pub const $ident: u8 = $id;)*
49
50    };
51}
52
53blocks! {
54    AIR/0:              "air",
55    STONE/1:            "stone",
56    GRASS/2:            "grass",
57    DIRT/3:             "dirt",
58    COBBLESTONE/4:      "cobblestone",
59    WOOD/5:             "wood",
60    SAPLING/6:          "sapling",
61    BEDROCK/7:          "bedrock",
62    WATER_MOVING/8:     "water_moving",
63    WATER_STILL/9:      "water_still",
64    LAVA_MOVING/10:     "lava_moving",
65    LAVA_STILL/11:      "lava_still",
66    SAND/12:            "sand",
67    GRAVEL/13:          "gravel",
68    GOLD_ORE/14:        "gold_ore",
69    IRON_ORE/15:        "iron_ore",
70    COAL_ORE/16:        "coal_ore",
71    LOG/17:             "log",
72    LEAVES/18:          "leaves",
73    SPONGE/19:          "sponge",
74    GLASS/20:           "glass",
75    LAPIS_ORE/21:       "lapis_ore",
76    LAPIS_BLOCK/22:     "lapis_block",
77    DISPENSER/23:       "dispenser",
78    SANDSTONE/24:       "sandstone",
79    NOTE_BLOCK/25:      "note_block",
80    BED/26:             "bed",
81    POWERED_RAIL/27:    "powered_rail",
82    DETECTOR_RAIL/28:   "detector_rail",
83    STICKY_PISTON/29:   "sticky_piston",
84    COBWEB/30:          "cobweb",
85    TALL_GRASS/31:      "tall_grass",
86    DEAD_BUSH/32:       "dead_bush",
87    PISTON/33:          "piston",
88    PISTON_EXT/34:      "piston_ext",
89    WOOL/35:            "wool",
90    PISTON_MOVING/36:   "piston_moving",
91    DANDELION/37:       "dandelion",
92    POPPY/38:           "poppy",
93    BROWN_MUSHROOM/39:  "brown_mushroom",
94    RED_MUSHROOM/40:    "red_mushroom",
95    GOLD_BLOCK/41:      "gold_block",
96    IRON_BLOCK/42:      "iron_block",
97    DOUBLE_SLAB/43:     "double_slab",
98    SLAB/44:            "slab",
99    BRICK/45:           "brick",
100    TNT/46:             "tnt",
101    BOOKSHELF/47:       "bookshelf",
102    MOSSY_COBBLESTONE/48: "mossy_cobblestone",
103    OBSIDIAN/49:        "obsidian",
104    TORCH/50:           "torch",
105    FIRE/51:            "fire",
106    SPAWNER/52:         "spawner",
107    WOOD_STAIR/53:      "wood_stair",
108    CHEST/54:           "chest",
109    REDSTONE/55:        "redstone",
110    DIAMOND_ORE/56:     "diamond_ore",
111    DIAMOND_BLOCK/57:   "diamond_block",
112    CRAFTING_TABLE/58:  "crafting_table",
113    WHEAT/59:           "wheat",
114    FARMLAND/60:        "farmland",
115    FURNACE/61:         "furnace",
116    FURNACE_LIT/62:     "furnace_lit",
117    SIGN/63:            "sign",
118    WOOD_DOOR/64:       "wood_door",
119    LADDER/65:          "ladder",
120    RAIL/66:            "rail",
121    COBBLESTONE_STAIR/67: "cobblestone_stair",
122    WALL_SIGN/68:       "wall_sign",
123    LEVER/69:           "lever",
124    STONE_PRESSURE_PLATE/70: "stone_pressure_plate",
125    IRON_DOOR/71:       "iron_door",
126    WOOD_PRESSURE_PLATE/72: "wood_pressure_plate",
127    REDSTONE_ORE/73:    "redstone_ore",
128    REDSTONE_ORE_LIT/74: "redstone_ore_lit",
129    REDSTONE_TORCH/75:  "redstone_torch",
130    REDSTONE_TORCH_LIT/76:  "redstone_torch_lit",
131    BUTTON/77:          "button",
132    SNOW/78:            "snow",
133    ICE/79:             "ice",
134    SNOW_BLOCK/80:      "snow_block",
135    CACTUS/81:          "cactus",
136    CLAY/82:            "clay",
137    SUGAR_CANES/83:     "sugar_canes",
138    JUKEBOX/84:         "jukebox",
139    FENCE/85:           "fence",
140    PUMPKIN/86:         "pumpkin",
141    NETHERRACK/87:      "netherrack",
142    SOULSAND/88:        "soulsand",
143    GLOWSTONE/89:       "glowstone",
144    PORTAL/90:          "portal",
145    PUMPKIN_LIT/91:     "pumpkin_lit",
146    CAKE/92:            "cake",
147    REPEATER/93:        "repeater",
148    REPEATER_LIT/94:    "repeater_lit",
149    LOCKED_CHEST/95:    "locked_chest",
150    TRAPDOOR/96:        "trapdoor",
151}
152
153/// Find a block name from its id.
154#[inline]
155pub fn name(id: u8) -> &'static str {
156    NAMES[id as usize]
157}
158
159/// Find a block id from its name.
160pub fn from_name(name: &str) -> Option<u8> {
161    NAMES.iter().position(|&n| n == name).map(|n| n as u8)
162}
163
164/// Find the item associated to the given block id. 
165/// TODO: This should be removed in the future when items module is reworked.
166#[inline]
167pub fn item(id: u8) -> &'static Item {
168    &ITEMS[id as usize]
169}