minecraft_command_types/
block.rs

1use crate::nbt_path::SNBTCompound;
2use crate::resource_location::ResourceLocation;
3use crate::snbt::fmt_snbt_compound;
4use minecraft_command_types_derive::HasMacro;
5use std::collections::BTreeMap;
6use std::fmt::{Display, Formatter};
7
8#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
9pub struct BlockState {
10    pub id: ResourceLocation,
11    pub block_states: BTreeMap<String, String>,
12    pub data_tags: Option<SNBTCompound>,
13}
14
15impl Display for BlockState {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        self.id.fmt(f)?;
18
19        if !self.block_states.is_empty() {
20            let states: Vec<String> = self
21                .block_states
22                .iter()
23                .map(|(k, v)| format!("{}={}", k, v))
24                .collect();
25            write!(f, "[{}]", states.join(", "))?;
26        }
27
28        if let Some(snbt) = &self.data_tags {
29            fmt_snbt_compound(f, snbt)?;
30        }
31
32        Ok(())
33    }
34}
35
36impl BlockState {
37    pub fn new(id: ResourceLocation) -> Self {
38        Self {
39            id,
40            block_states: BTreeMap::new(),
41            data_tags: None,
42        }
43    }
44}