Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged] Use Bytes in NbtTag #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/nbt/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 @@ -204,6 +204,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 @@ -259,3 +266,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)
}
}