aboutsummaryrefslogtreecommitdiff
path: root/azalea-nbt/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-06-25 05:09:26 +0000
committerGitHub <noreply@github.com>2022-06-25 05:09:26 +0000
commit7d3e57763e32ac9cf94180b1c714704cfbc3034d (patch)
tree2dcfe72bf09a42f6614f9dc988dc0254162ea0bf /azalea-nbt/src
parent69c47eda4c496b13dadd80976bffd2fab7ea5894 (diff)
parentca7067e173129f3044ebc8c77634f06da29a086e (diff)
downloadazalea-drasl-7d3e57763e32ac9cf94180b1c714704cfbc3034d.tar.xz
Merge pull request #10 from mat-1/azalea-entity
azalea-entity
Diffstat (limited to 'azalea-nbt/src')
-rwxr-xr-xazalea-nbt/src/decode.rs11
-rwxr-xr-xazalea-nbt/src/encode.rs8
-rwxr-xr-xazalea-nbt/src/lib.rs34
3 files changed, 53 insertions, 0 deletions
diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs
index 7f2ca754..1c011839 100755
--- a/azalea-nbt/src/decode.rs
+++ b/azalea-nbt/src/decode.rs
@@ -1,5 +1,6 @@
use crate::Error;
use crate::Tag;
+use azalea_buf::McBufReadable;
use byteorder::{ReadBytesExt, BE};
use flate2::read::{GzDecoder, ZlibDecoder};
use std::collections::HashMap;
@@ -136,3 +137,13 @@ impl Tag {
Tag::read(&mut gz)
}
}
+
+impl McBufReadable for Tag {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ match Tag::read(buf) {
+ Ok(r) => Ok(r),
+ // Err(e) => Err(e.to_string()),
+ Err(e) => Err(e.to_string()).unwrap(),
+ }
+ }
+}
diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs
index fb5585b3..3763ffd1 100755
--- a/azalea-nbt/src/encode.rs
+++ b/azalea-nbt/src/encode.rs
@@ -1,5 +1,6 @@
use crate::Error;
use crate::Tag;
+use azalea_buf::McBufWritable;
use byteorder::{WriteBytesExt, BE};
use flate2::write::{GzEncoder, ZlibEncoder};
use std::collections::HashMap;
@@ -217,3 +218,10 @@ impl Tag {
self.write(&mut encoder)
}
}
+
+impl McBufWritable for Tag {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ self.write(buf)
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
+ }
+}
diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs
index d14fd929..cb2eb28e 100755
--- a/azalea-nbt/src/lib.rs
+++ b/azalea-nbt/src/lib.rs
@@ -5,3 +5,37 @@ mod tag;
pub use error::Error;
pub use tag::Tag;
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use azalea_buf::{McBufReadable, McBufWritable};
+ use std::{collections::HashMap, io::Cursor};
+
+ #[test]
+ fn mcbuf_nbt() {
+ let mut buf = Vec::new();
+ let tag = Tag::Compound(HashMap::from_iter(vec![(
+ "hello world".to_string(),
+ Tag::Compound(HashMap::from_iter(vec![(
+ "name".to_string(),
+ Tag::String("Bananrama".to_string()),
+ )])),
+ )]));
+ tag.write_into(&mut buf).unwrap();
+
+ let mut buf = Cursor::new(buf);
+
+ let result = Tag::read_into(&mut buf).unwrap();
+ assert_eq!(
+ result,
+ Tag::Compound(HashMap::from_iter(vec![(
+ "hello world".to_string(),
+ Tag::Compound(HashMap::from_iter(vec![(
+ "name".to_string(),
+ Tag::String("Bananrama".to_string()),
+ )])),
+ )]))
+ );
+ }
+}