Add valence_nbt to valence

This commit is contained in:
Ryan 2022-08-29 19:28:19 -07:00
parent 8f3a46bccf
commit c8cc7a021c
8 changed files with 27 additions and 26 deletions

View file

@ -17,14 +17,12 @@ async-trait = "0.1"
base64 = "0.13" base64 = "0.13"
bitfield-struct = "0.1" bitfield-struct = "0.1"
bitvec = "1" bitvec = "1"
bumpalo = "3.11.0"
byteorder = "1" byteorder = "1"
cesu8 = "1.1.0" cesu8 = "1.1.0"
cfb8 = "0.7" cfb8 = "0.7"
flate2 = "1" flate2 = "1"
flume = "0.10" flume = "0.10"
futures = "0.3" futures = "0.3"
hematite-nbt = "0.5"
log = "0.4" log = "0.4"
num = "0.4" num = "0.4"
paste = "1" paste = "1"
@ -39,6 +37,7 @@ sha2 = "0.10"
thiserror = "1" thiserror = "1"
url = { version = "2.2.2", features = ["serde"] } url = { version = "2.2.2", features = ["serde"] }
uuid = "1" uuid = "1"
valence_nbt = { path = "valence_nbt" }
vek = "0.15" vek = "0.15"
[dependencies.tokio] [dependencies.tokio]

View file

@ -125,7 +125,7 @@ impl Value {
Value::Facing(_) => quote!(Facing), Value::Facing(_) => quote!(Facing),
Value::OptionalUuid(_) => quote!(Option<Uuid>), Value::OptionalUuid(_) => quote!(Option<Uuid>),
Value::OptionalBlockState(_) => quote!(BlockState), Value::OptionalBlockState(_) => quote!(BlockState),
Value::NbtCompound(_) => quote!(nbt::Blob), Value::NbtCompound(_) => quote!(crate::nbt::Compound),
Value::Particle(_) => quote!(Particle), Value::Particle(_) => quote!(Particle),
Value::VillagerData { .. } => quote!(VillagerData), Value::VillagerData { .. } => quote!(VillagerData),
Value::OptionalInt(_) => quote!(OptionalInt), Value::OptionalInt(_) => quote!(OptionalInt),
@ -142,7 +142,7 @@ impl Value {
Value::String(_) => quote!(&str), Value::String(_) => quote!(&str),
Value::TextComponent(_) => quote!(&Text), Value::TextComponent(_) => quote!(&Text),
Value::OptionalTextComponent(_) => quote!(Option<&Text>), Value::OptionalTextComponent(_) => quote!(Option<&Text>),
Value::NbtCompound(_) => quote!(&nbt::Blob), Value::NbtCompound(_) => quote!(&crate::nbt::Compound),
_ => self.field_type(), _ => self.field_type(),
} }
} }
@ -187,7 +187,7 @@ impl Value {
} }
Value::OptionalUuid(_) => quote!(None), // TODO Value::OptionalUuid(_) => quote!(None), // TODO
Value::OptionalBlockState(_) => quote!(BlockState::default()), // 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) => { Value::Particle(p) => {
let variant = ident(p.to_pascal_case()); let variant = ident(p.to_pascal_case());
quote!(Particle::#variant) quote!(Particle::#variant)

View file

@ -20,7 +20,7 @@ use crate::dimension::DimensionId;
use crate::protocol_inner::packets::s2c::play::{ use crate::protocol_inner::packets::s2c::play::{
BlockUpdate, ChunkData, ChunkDataHeightmaps, ChunkSectionUpdate, S2cPlayPacket, 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; use crate::server::SharedServer;
/// A container for all [`Chunks`]s in a [`World`](crate::world::World). /// A container for all [`Chunks`]s in a [`World`](crate::world::World).
@ -311,7 +311,7 @@ impl<C: Config> Chunk<C> {
ChunkData { ChunkData {
chunk_x: pos.x, chunk_x: pos.x,
chunk_z: pos.z, chunk_z: pos.z,
heightmaps: Nbt(ChunkDataHeightmaps { heightmaps: NbtBridge(ChunkDataHeightmaps {
motion_blocking: self.heightmap.clone(), motion_blocking: self.heightmap.clone(),
}), }),
blocks_and_biomes, blocks_and_biomes,

View file

@ -37,7 +37,7 @@ use crate::protocol_inner::packets::s2c::play::{
PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, PlayerSpawnPosition, RegistryCodec, PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, PlayerSpawnPosition, RegistryCodec,
Rotate, RotateAndMoveRelative, S2cPlayPacket, UnloadChunk, UpdateSubtitle, UpdateTitle, 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::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer};
use crate::slab_versioned::{Key, VersionedSlab}; use crate::slab_versioned::{Key, VersionedSlab};
use crate::text::Text; use crate::text::Text;
@ -900,7 +900,7 @@ impl<C: Config> Client<C> {
gamemode: self.new_game_mode, gamemode: self.new_game_mode,
previous_gamemode: self.old_game_mode, previous_gamemode: self.old_game_mode,
dimension_names, dimension_names,
registry_codec: Nbt(make_registry_codec(shared)), registry_codec: NbtBridge(make_registry_codec(shared)),
dimension_type_name: ident!( dimension_type_name: ident!(
"{LIBRARY_NAMESPACE}:dimension_type_{}", "{LIBRARY_NAMESPACE}:dimension_type_{}",
world.meta.dimension().0 world.meta.dimension().0

View file

@ -128,7 +128,7 @@ pub use async_trait::async_trait;
#[doc(inline)] #[doc(inline)]
pub use server::start_server; pub use server::start_server;
#[doc(inline)] #[doc(inline)]
pub use {uuid, vek}; pub use {uuid, valence_nbt as nbt, vek};
pub mod biome; pub mod biome;
pub mod block; pub mod block;

View file

@ -14,6 +14,7 @@ pub use var_long::VarLong;
use vek::{Vec2, Vec3, Vec4}; use vek::{Vec2, Vec3, Vec4};
use crate::entity::EntityId; use crate::entity::EntityId;
use crate::nbt;
mod byte_angle; mod byte_angle;
pub mod codec; 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<()> { 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<Self> { fn decode(r: &mut impl Read) -> anyhow::Result<Self> {
Ok(nbt::from_reader(r)?) Ok(nbt::binary::from_reader(r)?)
} }
} }
/// Wrapper type acting as a bridge between Serde and [Encode]/[Decode] through /// Wrapper type acting as a bridge between Serde and [Encode]/[Decode] through
/// the NBT format. /// the NBT format.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Debug)]
pub struct Nbt<T>(pub T); pub struct NbtBridge<T>(pub T);
impl<T: Serialize> Encode for Nbt<T> { impl<T: Serialize> Encode for NbtBridge<T> {
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { 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<T: DeserializeOwned> Decode for Nbt<T> { impl<T: DeserializeOwned> Decode for NbtBridge<T> {
fn decode(r: &mut impl Read) -> anyhow::Result<Self> { fn decode(r: &mut impl Read) -> anyhow::Result<Self> {
Ok(Self(nbt::from_reader(r)?)) Ok(Self(nbt::binary::from_reader(r)?))
} }
} }

View file

@ -18,9 +18,10 @@ use vek::Vec3;
// use {def_bitfield, def_enum, def_struct}; // use {def_bitfield, def_enum, def_struct};
use crate::block_pos::BlockPos; use crate::block_pos::BlockPos;
use crate::ident::Ident; use crate::ident::Ident;
use crate::nbt::Compound;
use crate::protocol_inner::{ use crate::protocol_inner::{
BoundedArray, BoundedInt, BoundedString, ByteAngle, Decode, Encode, Nbt, RawBytes, VarInt, BoundedArray, BoundedInt, BoundedString, ByteAngle, Decode, Encode, NbtBridge, RawBytes,
VarLong, VarInt, VarLong,
}; };
use crate::text::Text; use crate::text::Text;

View file

@ -138,7 +138,7 @@ pub mod play {
BlockEntityUpdate { BlockEntityUpdate {
location: BlockPos, location: BlockPos,
kind: VarInt, // TODO: use enum here kind: VarInt, // TODO: use enum here
data: nbt::Blob, data: Compound,
} }
} }
@ -294,7 +294,7 @@ pub mod play {
ChunkData { ChunkData {
chunk_x: i32, chunk_x: i32,
chunk_z: i32, chunk_z: i32,
heightmaps: Nbt<ChunkDataHeightmaps>, heightmaps: NbtBridge<ChunkDataHeightmaps>,
blocks_and_biomes: Vec<u8>, blocks_and_biomes: Vec<u8>,
block_entities: Vec<ChunkDataBlockEntity>, block_entities: Vec<ChunkDataBlockEntity>,
trust_edges: bool, trust_edges: bool,
@ -309,7 +309,7 @@ pub mod play {
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ChunkDataHeightmaps { 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<i64>, pub motion_blocking: Vec<i64>,
} }
@ -318,7 +318,7 @@ pub mod play {
packed_xz: i8, packed_xz: i8,
y: i16, y: i16,
kind: VarInt, kind: VarInt,
data: nbt::Blob, data: Compound,
} }
} }
@ -330,7 +330,7 @@ pub mod play {
gamemode: GameMode, gamemode: GameMode,
previous_gamemode: GameMode, previous_gamemode: GameMode,
dimension_names: Vec<Ident>, dimension_names: Vec<Ident>,
registry_codec: Nbt<RegistryCodec>, registry_codec: NbtBridge<RegistryCodec>,
/// The name of the dimension type being spawned into. /// The name of the dimension type being spawned into.
dimension_type_name: Ident, dimension_type_name: Ident,
/// The name of the dimension being spawned into. /// The name of the dimension being spawned into.