From f8117cce759660fe3657be1b6b468bb0911a39b6 Mon Sep 17 00:00:00 2001 From: SzczurekYT Date: Tue, 26 Mar 2024 21:37:42 +0100 Subject: [PATCH] feat: Add support for booleans --- src/nbt/compound.rs | 4 ++++ src/nbt/tag.rs | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/nbt/compound.rs b/src/nbt/compound.rs index b2012b9..9980e17 100644 --- a/src/nbt/compound.rs +++ b/src/nbt/compound.rs @@ -80,6 +80,10 @@ impl NbtCompound { .and_then(|tag| tag.extract_double()) } + pub fn get_bool(&self, name: &str) -> Option { + 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) diff --git a/src/nbt/tag.rs b/src/nbt/tag.rs index 7e689fa..dfafb20 100644 --- a/src/nbt/tag.rs +++ b/src/nbt/tag.rs @@ -197,7 +197,14 @@ impl NbtTag { } } - pub fn extract_byte_array(&self) -> Option { + pub fn extract_bool(&self) -> Option { + match self { + NbtTag::Byte(byte) => Some(*byte != 0), + _ => None, + } + } + + pub fn extract_byte_array(&self) -> Option<&Vec> { match self { // Note: Bytes are free to clone, so we can hand out an owned type NbtTag::ByteArray(byte_array) => Some(byte_array.clone()), @@ -252,3 +259,9 @@ impl From<&[u8]> for NbtTag { NbtTag::ByteArray(Bytes::copy_from_slice(value)) } } + +impl From for NbtTag { + fn from(value: bool) -> Self { + NbtTag::Byte(value as i8) + } +}