From c8cc7a021ce5ea8af0b65407118f1e653aa35fb8 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 29 Aug 2022 19:28:19 -0700 Subject: [PATCH] Add valence_nbt to valence --- Cargo.toml | 3 +-- build/entity.rs | 6 +++--- src/chunk.rs | 4 ++-- src/client.rs | 4 ++-- src/lib.rs | 2 +- src/protocol_inner.rs | 19 ++++++++++--------- src/protocol_inner/packets.rs | 5 +++-- src/protocol_inner/packets/s2c.rs | 10 +++++----- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b6ab29c..48e60c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,14 +17,12 @@ async-trait = "0.1" base64 = "0.13" bitfield-struct = "0.1" bitvec = "1" -bumpalo = "3.11.0" byteorder = "1" cesu8 = "1.1.0" cfb8 = "0.7" flate2 = "1" flume = "0.10" futures = "0.3" -hematite-nbt = "0.5" log = "0.4" num = "0.4" paste = "1" @@ -39,6 +37,7 @@ sha2 = "0.10" thiserror = "1" url = { version = "2.2.2", features = ["serde"] } uuid = "1" +valence_nbt = { path = "valence_nbt" } vek = "0.15" [dependencies.tokio] diff --git a/build/entity.rs b/build/entity.rs index 043557b..c0e45f0 100644 --- a/build/entity.rs +++ b/build/entity.rs @@ -125,7 +125,7 @@ impl Value { Value::Facing(_) => quote!(Facing), Value::OptionalUuid(_) => quote!(Option), Value::OptionalBlockState(_) => quote!(BlockState), - Value::NbtCompound(_) => quote!(nbt::Blob), + Value::NbtCompound(_) => quote!(crate::nbt::Compound), Value::Particle(_) => quote!(Particle), Value::VillagerData { .. } => quote!(VillagerData), Value::OptionalInt(_) => quote!(OptionalInt), @@ -142,7 +142,7 @@ impl Value { Value::String(_) => quote!(&str), Value::TextComponent(_) => quote!(&Text), Value::OptionalTextComponent(_) => quote!(Option<&Text>), - Value::NbtCompound(_) => quote!(&nbt::Blob), + Value::NbtCompound(_) => quote!(&crate::nbt::Compound), _ => self.field_type(), } } @@ -187,7 +187,7 @@ impl Value { } Value::OptionalUuid(_) => quote!(None), // TODO Value::OptionalBlockState(_) => quote!(BlockState::default()), // TODO - Value::NbtCompound(_) => quote!(nbt::Blob::default()), // TODO + Value::NbtCompound(_) => quote!(crate::nbt::Compound::default()), // TODO Value::Particle(p) => { let variant = ident(p.to_pascal_case()); quote!(Particle::#variant) diff --git a/src/chunk.rs b/src/chunk.rs index 8198c9a..7ee75ef 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -20,7 +20,7 @@ use crate::dimension::DimensionId; use crate::protocol_inner::packets::s2c::play::{ BlockUpdate, ChunkData, ChunkDataHeightmaps, ChunkSectionUpdate, S2cPlayPacket, }; -use crate::protocol_inner::{Encode, Nbt, VarInt, VarLong}; +use crate::protocol_inner::{Encode, NbtBridge, VarInt, VarLong}; use crate::server::SharedServer; /// A container for all [`Chunks`]s in a [`World`](crate::world::World). @@ -311,7 +311,7 @@ impl Chunk { ChunkData { chunk_x: pos.x, chunk_z: pos.z, - heightmaps: Nbt(ChunkDataHeightmaps { + heightmaps: NbtBridge(ChunkDataHeightmaps { motion_blocking: self.heightmap.clone(), }), blocks_and_biomes, diff --git a/src/client.rs b/src/client.rs index 12d0b57..074eb33 100644 --- a/src/client.rs +++ b/src/client.rs @@ -37,7 +37,7 @@ use crate::protocol_inner::packets::s2c::play::{ PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, PlayerSpawnPosition, RegistryCodec, Rotate, RotateAndMoveRelative, S2cPlayPacket, UnloadChunk, UpdateSubtitle, UpdateTitle, }; -use crate::protocol_inner::{BoundedInt, ByteAngle, Nbt, RawBytes, VarInt}; +use crate::protocol_inner::{BoundedInt, ByteAngle, NbtBridge, RawBytes, VarInt}; use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer}; use crate::slab_versioned::{Key, VersionedSlab}; use crate::text::Text; @@ -900,7 +900,7 @@ impl Client { gamemode: self.new_game_mode, previous_gamemode: self.old_game_mode, dimension_names, - registry_codec: Nbt(make_registry_codec(shared)), + registry_codec: NbtBridge(make_registry_codec(shared)), dimension_type_name: ident!( "{LIBRARY_NAMESPACE}:dimension_type_{}", world.meta.dimension().0 diff --git a/src/lib.rs b/src/lib.rs index 17b00e4..b7884c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,7 +128,7 @@ pub use async_trait::async_trait; #[doc(inline)] pub use server::start_server; #[doc(inline)] -pub use {uuid, vek}; +pub use {uuid, valence_nbt as nbt, vek}; pub mod biome; pub mod block; diff --git a/src/protocol_inner.rs b/src/protocol_inner.rs index 558cb8b..d5af64b 100644 --- a/src/protocol_inner.rs +++ b/src/protocol_inner.rs @@ -14,6 +14,7 @@ pub use var_long::VarLong; use vek::{Vec2, Vec3, Vec4}; use crate::entity::EntityId; +use crate::nbt; mod byte_angle; pub mod codec; @@ -503,32 +504,32 @@ impl Decode for Uuid { } } -impl Encode for nbt::Blob { +impl Encode for nbt::Compound { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { - Ok(nbt::to_writer(w, self, None)?) + Ok(nbt::binary::to_writer(w, self)?) } } -impl Decode for nbt::Blob { +impl Decode for nbt::Compound { fn decode(r: &mut impl Read) -> anyhow::Result { - Ok(nbt::from_reader(r)?) + Ok(nbt::binary::from_reader(r)?) } } /// Wrapper type acting as a bridge between Serde and [Encode]/[Decode] through /// the NBT format. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Debug)] -pub struct Nbt(pub T); +pub struct NbtBridge(pub T); -impl Encode for Nbt { +impl Encode for NbtBridge { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { - Ok(nbt::to_writer(w, &self.0, None)?) + Ok(nbt::binary::to_writer(w, &self.0)?) } } -impl Decode for Nbt { +impl Decode for NbtBridge { fn decode(r: &mut impl Read) -> anyhow::Result { - Ok(Self(nbt::from_reader(r)?)) + Ok(Self(nbt::binary::from_reader(r)?)) } } diff --git a/src/protocol_inner/packets.rs b/src/protocol_inner/packets.rs index 7dc632a..e5c2b84 100644 --- a/src/protocol_inner/packets.rs +++ b/src/protocol_inner/packets.rs @@ -18,9 +18,10 @@ use vek::Vec3; // use {def_bitfield, def_enum, def_struct}; use crate::block_pos::BlockPos; use crate::ident::Ident; +use crate::nbt::Compound; use crate::protocol_inner::{ - BoundedArray, BoundedInt, BoundedString, ByteAngle, Decode, Encode, Nbt, RawBytes, VarInt, - VarLong, + BoundedArray, BoundedInt, BoundedString, ByteAngle, Decode, Encode, NbtBridge, RawBytes, + VarInt, VarLong, }; use crate::text::Text; diff --git a/src/protocol_inner/packets/s2c.rs b/src/protocol_inner/packets/s2c.rs index 3bec72e..0989f79 100644 --- a/src/protocol_inner/packets/s2c.rs +++ b/src/protocol_inner/packets/s2c.rs @@ -138,7 +138,7 @@ pub mod play { BlockEntityUpdate { location: BlockPos, kind: VarInt, // TODO: use enum here - data: nbt::Blob, + data: Compound, } } @@ -294,7 +294,7 @@ pub mod play { ChunkData { chunk_x: i32, chunk_z: i32, - heightmaps: Nbt, + heightmaps: NbtBridge, blocks_and_biomes: Vec, block_entities: Vec, trust_edges: bool, @@ -309,7 +309,7 @@ pub mod play { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ChunkDataHeightmaps { - #[serde(rename = "MOTION_BLOCKING", serialize_with = "nbt::i64_array")] + #[serde(rename = "MOTION_BLOCKING", with = "crate::nbt::long_array")] pub motion_blocking: Vec, } @@ -318,7 +318,7 @@ pub mod play { packed_xz: i8, y: i16, kind: VarInt, - data: nbt::Blob, + data: Compound, } } @@ -330,7 +330,7 @@ pub mod play { gamemode: GameMode, previous_gamemode: GameMode, dimension_names: Vec, - registry_codec: Nbt, + registry_codec: NbtBridge, /// The name of the dimension type being spawned into. dimension_type_name: Ident, /// The name of the dimension being spawned into.