summaryrefslogtreecommitdiff
path: root/src/nbtdsl.rs
blob: 86b2116391f90241293d1041ada312efd7c58842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use mcproto_rs::nbt::Tag;

pub trait ShittyToNbt {
    fn to_nbt(self) -> Tag;
}

impl ShittyToNbt for String {
    fn to_nbt(self) -> Tag {
        Tag::String(self)
    }
}

impl ShittyToNbt for &str {
    fn to_nbt(self) -> Tag {
        Tag::String(self.into())
    }
}

impl ShittyToNbt for i32 {
    fn to_nbt(self) -> Tag {
        Tag::Int(self)
    }
}

impl ShittyToNbt for i64 {
    fn to_nbt(self) -> Tag {
        Tag::Long(self)
    }
}

impl ShittyToNbt for bool {
    fn to_nbt(self) -> Tag {
        Tag::Byte(if self { 1 } else { 0 })
    }
}

impl ShittyToNbt for Tag {
    fn to_nbt(self) -> Tag {
        self
    }
}

impl ShittyToNbt for f32 {
    fn to_nbt(self) -> Tag {
        Tag::Float(self)
    }
}

impl ShittyToNbt for f64 {
    fn to_nbt(self) -> Tag {
        Tag::Double(self)
    }
}

#[macro_export]
macro_rules! nbt {
    ( {$($name:literal :: $value:tt),* $(,)? } ) => {
        Tag::Compound(vec![
            $(
                nbt!($value).with_name($name)
            ),*
        ])
    };
    { $($name:literal :: $value:tt),* $(,)? } => {
        Tag::Compound(vec![
            $(
                nbt!($value).with_name($name)
            ),*
        ])
    };
    ([ $($value:tt),* $(,)? ]) => {
        Tag::List(vec![
            $(
                nbt!($value)
            ),*
        ])
    };
    (($e:expr))  => {
        $crate::nbtdsl::ShittyToNbt::to_nbt($e)
    };
}