Skip to content

Commit

Permalink
feat: Add support for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
SzczurekYT authored and Norbiros committed Apr 7, 2024
1 parent 5341f64 commit 69482d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/nbt/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ impl NbtCompound {
.and_then(|tag| tag.extract_double())
}

pub fn get_bool(&self, name: &str) -> Option<bool> {
self.child_tags.get(name).and_then(|tag| tag.extract_bool())
}

pub fn get_string(&self, name: &str) -> Option<&String> {
self.child_tags
.get(name)
Expand Down
13 changes: 13 additions & 0 deletions src/nbt/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ impl NbtTag {
}
}

pub fn extract_bool(&self) -> Option<bool> {
match self {
NbtTag::Byte(byte) => Some(*byte != 0),
_ => None,
}
}

pub fn extract_byte_array(&self) -> Option<Bytes> {
match self {
// Note: Bytes are free to clone, so we can hand out an owned type
Expand Down Expand Up @@ -252,3 +259,9 @@ impl From<&[u8]> for NbtTag {
NbtTag::ByteArray(Bytes::copy_from_slice(value))
}
}

impl From<bool> for NbtTag {
fn from(value: bool) -> Self {
NbtTag::Byte(value as i8)
}
}

0 comments on commit 69482d2

Please sign in to comment.