From 18d1f3f5668175db98eafcbaae92bc5b22ad8423 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 5 Aug 2023 01:38:19 +0800 Subject: [PATCH] add From> for NbtList --- src/data.rs | 25 +++++++++++++++++++++++++ src/read.rs | 13 +++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/data.rs b/src/data.rs index a21497f..31dc3ff 100644 --- a/src/data.rs +++ b/src/data.rs @@ -71,6 +71,31 @@ impl NbtItem { } } +impl From> for NbtList { + #[inline] + fn from(value: Vec) -> Self { Self::List(Rc::new(RefCell::new(value))) } +} + +impl From, NbtItem>> for NbtList { + #[inline] + fn from(value: HashMap, NbtItem>) -> Self { Self::Compound(Rc::new(RefCell::new(value))) } +} + +impl From> for NbtList { + #[inline] + fn from(value: Vec) -> Self { Self::BoolArray(Rc::new(RefCell::new(value))) } +} + +impl From> for NbtList { + #[inline] + fn from(value: Vec) -> Self { Self::IntArray(Rc::new(RefCell::new(value))) } +} + +impl From> for NbtList { + #[inline] + fn from(value: Vec) -> Self { Self::LongArray(Rc::new(RefCell::new(value))) } +} + macro_rules! export_data { ($name:ident, $nbt_name:ident, $type:ty) => { #[inline] diff --git a/src/read.rs b/src/read.rs index cf4e45e..47d36ff 100644 --- a/src/read.rs +++ b/src/read.rs @@ -108,8 +108,7 @@ pub mod read { [0x07] => { // ByteArray for _ in 0..len { - let arr = Rc::new(RefCell::new(from_bool_array(value))); - vec.push(NbtItem::Array(NbtList::BoolArray(arr))); + vec.push(NbtItem::Array(NbtList::from(from_bool_array(value)))); } } [0x08] => { @@ -121,6 +120,10 @@ pub mod read { [0x09] => { // NbtList // 要命 (虽说没 Compound 那么麻烦) + // 直接递归就行 + for _ in 0..len { + vec.push(NbtItem::Array(NbtList::from(read_nbt_list(value)))); + } } [0x0A] => { // Compound @@ -129,15 +132,13 @@ pub mod read { [0x0B] => { // IntArray for _ in 0..len { - let arr = Rc::new(RefCell::new(from_i32_array(value))); - vec.push(NbtItem::Array(NbtList::IntArray(arr))); + vec.push(NbtItem::Array(NbtList::from(from_i32_array(value)))); } } [0x0C] => { // LongArray for _ in 0..len { - let arr = Rc::new(RefCell::new(from_i64_array(value))); - vec.push(NbtItem::Array(NbtList::LongArray(arr))); + vec.push(NbtItem::Array(NbtList::from(from_i64_array(value)))); } } _ => {