diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2026-01-12 02:09:41 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-12 02:09:41 -0600 |
| commit | 1accbac964168af5fa0d87cb170389f0a9d01363 (patch) | |
| tree | 1509b26c19beaa23a492289f6bf00d3958be44d5 /azalea-core/src | |
| parent | 58339b9d229592dee40e15b8648fe4075cc391f4 (diff) | |
| download | azalea-drasl-1accbac964168af5fa0d87cb170389f0a9d01363.tar.xz | |
Make Bevy dependencies optional in azalea-protocol (#303)
* Make Bevy dependencies optional in azalea-protocol
* derive serde traits on Direction again
* update docs for types that may not have Component
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/attribute_modifier_operation.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/data_registry.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/direction.rs | 8 | ||||
| -rw-r--r-- | azalea-core/src/entity_id.rs | 78 | ||||
| -rw-r--r-- | azalea-core/src/filterable.rs | 5 | ||||
| -rw-r--r-- | azalea-core/src/heightmap_kind.rs | 50 | ||||
| -rw-r--r-- | azalea-core/src/lib.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 18 | ||||
| -rw-r--r-- | azalea-core/src/sound.rs | 4 |
9 files changed, 161 insertions, 20 deletions
diff --git a/azalea-core/src/attribute_modifier_operation.rs b/azalea-core/src/attribute_modifier_operation.rs index 8418ff22..ab135080 100644 --- a/azalea-core/src/attribute_modifier_operation.rs +++ b/azalea-core/src/attribute_modifier_operation.rs @@ -1,11 +1,11 @@ use std::str::FromStr; use azalea_buf::AzBuf; -use serde::Serialize; use simdnbt::{FromNbtTag, borrow::NbtTag}; -#[derive(AzBuf, Clone, Copy, Debug, PartialEq, Serialize)] -#[serde(rename_all = "snake_case")] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[derive(AzBuf, Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] pub enum AttributeModifierOperation { AddValue, AddMultipliedBase, diff --git a/azalea-core/src/data_registry.rs b/azalea-core/src/data_registry.rs index 90628f47..bf1a9c1a 100644 --- a/azalea-core/src/data_registry.rs +++ b/azalea-core/src/data_registry.rs @@ -5,7 +5,7 @@ use azalea_registry::{ }; use simdnbt::owned::NbtCompound; -use crate::registry_holder::{self, RegistryDeserializesTo, RegistryHolder}; +use crate::registry_holder::{RegistryDeserializesTo, RegistryHolder}; pub trait DataRegistryWithKey: DataRegistry { fn key<'s, 'a: 's>( @@ -63,8 +63,8 @@ macro_rules! define_default_deserializes_to { } define_deserializes_to! { - data::DimensionKind => registry_holder::dimension_type::DimensionKindElement, - data::Enchantment => registry_holder::enchantment::EnchantmentData, + data::DimensionKind => crate::registry_holder::dimension_type::DimensionKindElement, + data::Enchantment => crate::registry_holder::enchantment::EnchantmentData, } define_default_deserializes_to! { diff --git a/azalea-core/src/direction.rs b/azalea-core/src/direction.rs index b53df448..5902ffea 100644 --- a/azalea-core/src/direction.rs +++ b/azalea-core/src/direction.rs @@ -2,9 +2,8 @@ use azalea_buf::AzBuf; use crate::position::{BlockPos, Vec3, Vec3i}; -#[derive( - Clone, Copy, Debug, AzBuf, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize, -)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[derive(Clone, Copy, Debug, AzBuf, Default, Eq, PartialEq)] pub enum Direction { #[default] Down = 0, @@ -91,7 +90,8 @@ impl Direction { /// /// Note that azalea_block has a similar enum named `FacingCardinal` that is /// used for block states. -#[derive(AzBuf, Clone, Copy, Debug, serde::Deserialize, Eq, Hash, PartialEq, serde::Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(AzBuf, Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum CardinalDirection { North, South, diff --git a/azalea-core/src/entity_id.rs b/azalea-core/src/entity_id.rs new file mode 100644 index 00000000..c66d3a2e --- /dev/null +++ b/azalea-core/src/entity_id.rs @@ -0,0 +1,78 @@ +use std::{ + fmt::{self, Display}, + hash::{Hash, Hasher}, + io::{self, Cursor}, +}; + +use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; +use derive_more::{Deref, DerefMut}; + +// note: this is here instead of in azalea-entity because azalea-world depends +// on it. and this isn't in azalea-world so azalea-protocol doesn't need to +// depend on azalea-world. + +/// An entity ID used by Minecraft. +/// +/// These IDs are picked by the server. Some server software (like Bungeecord) +/// may pick entity IDs per-player, so you should avoid relying on them for +/// identifying IDs (especially if you're using a shared world -- i.e. a swarm). +/// +/// You might find [`Entity`] more useful, since that's an ID decided by us that +/// is likely to be correct across shared worlds. You could also use the +/// `EntityUuid` from `azalea_entity`, that one is unlikely to change even +/// across server restarts. +/// +/// This serializes as a i32. Usually it's a VarInt in the protocol, but not +/// always. If you do need it to serialize as a VarInt, make sure to use use the +/// `#[var]` attribute. +/// +/// [`Entity`]: bevy_ecs::entity::Entity +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))] +#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Eq, PartialEq)] +pub struct MinecraftEntityId(pub i32); + +impl Hash for MinecraftEntityId { + fn hash<H: Hasher>(&self, hasher: &mut H) { + hasher.write_i32(self.0); + } +} +impl nohash_hasher::IsEnabled for MinecraftEntityId {} + +// we can't have the default be #[var] because mojang doesn't use varints for +// entities sometimes :( +impl AzaleaRead for MinecraftEntityId { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + i32::azalea_read(buf).map(MinecraftEntityId) + } +} +impl AzaleaWrite for MinecraftEntityId { + fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> { + i32::azalea_write(&self.0, buf) + } +} +impl AzaleaReadVar for MinecraftEntityId { + fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + i32::azalea_read_var(buf).map(MinecraftEntityId) + } +} +impl AzaleaWriteVar for MinecraftEntityId { + fn azalea_write_var(&self, buf: &mut impl io::Write) -> io::Result<()> { + i32::azalea_write_var(&self.0, buf) + } +} +impl Display for MinecraftEntityId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "eid({})", self.0) + } +} +impl From<i32> for MinecraftEntityId { + fn from(id: i32) -> Self { + Self(id) + } +} +impl From<u32> for MinecraftEntityId { + fn from(id: u32) -> Self { + Self(id as i32) + } +} diff --git a/azalea-core/src/filterable.rs b/azalea-core/src/filterable.rs index fe9a553d..1c9766b6 100644 --- a/azalea-core/src/filterable.rs +++ b/azalea-core/src/filterable.rs @@ -4,13 +4,12 @@ use std::{ }; use azalea_buf::{AzaleaRead, AzaleaReadLimited, AzaleaReadVar, AzaleaWrite}; -use serde::Serialize; /// Used for written books. -#[derive(Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct Filterable<T> { pub raw: T, - #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] pub filtered: Option<T>, } diff --git a/azalea-core/src/heightmap_kind.rs b/azalea-core/src/heightmap_kind.rs new file mode 100644 index 00000000..07100a77 --- /dev/null +++ b/azalea-core/src/heightmap_kind.rs @@ -0,0 +1,50 @@ +// (wg stands for worldgen) + +use std::{ + fmt::{self, Display}, + str::FromStr, +}; + +use azalea_buf::AzBuf; + +/// A type of world heightmap. +/// +/// See `azalea_world::heightmap` for more info. +#[derive(AzBuf, Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub enum HeightmapKind { + WorldSurfaceWg, + WorldSurface, + OceanFloorWg, + OceanFloor, + MotionBlocking, + MotionBlockingNoLeaves, +} + +impl FromStr for HeightmapKind { + type Err = (); + + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "WORLD_SURFACE_WG" => Ok(HeightmapKind::WorldSurfaceWg), + "WORLD_SURFACE" => Ok(HeightmapKind::WorldSurface), + "OCEAN_FLOOR_WG" => Ok(HeightmapKind::OceanFloorWg), + "OCEAN_FLOOR" => Ok(HeightmapKind::OceanFloor), + "MOTION_BLOCKING" => Ok(HeightmapKind::MotionBlocking), + "MOTION_BLOCKING_NO_LEAVES" => Ok(HeightmapKind::MotionBlockingNoLeaves), + _ => Err(()), + } + } +} + +impl Display for HeightmapKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + HeightmapKind::WorldSurfaceWg => write!(f, "WORLD_SURFACE_WG"), + HeightmapKind::WorldSurface => write!(f, "WORLD_SURFACE"), + HeightmapKind::OceanFloorWg => write!(f, "OCEAN_FLOOR_WG"), + HeightmapKind::OceanFloor => write!(f, "OCEAN_FLOOR"), + HeightmapKind::MotionBlocking => write!(f, "MOTION_BLOCKING"), + HeightmapKind::MotionBlockingNoLeaves => write!(f, "MOTION_BLOCKING_NO_LEAVES"), + } + } +} diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index d61690aa..e07f9d88 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -5,20 +5,26 @@ pub mod aabb; pub mod attribute_modifier_operation; pub mod bitset; +#[cfg(feature = "serde")] pub mod checksum; +#[cfg(feature = "serde")] pub mod codec_utils; pub mod color; pub mod cursor3d; +#[cfg(feature = "serde")] pub mod data_registry; pub mod delta; pub mod difficulty; pub mod direction; +pub mod entity_id; pub mod filterable; pub mod game_type; +pub mod heightmap_kind; pub mod hit_result; pub mod math; pub mod objectives; pub mod position; +#[cfg(feature = "serde")] pub mod registry_holder; #[doc(hidden)] pub mod resource_location { diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 80b189ce..6da037ff 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -14,10 +14,11 @@ use std::{ use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use azalea_registry::identifier::Identifier; -use serde::{Serialize, Serializer}; +#[cfg(feature = "serde")] +use serde::Serializer; use simdnbt::borrow::NbtTag; -use crate::{codec_utils::IntArray, direction::Direction, math}; +use crate::{direction::Direction, math}; macro_rules! vec3_impl { ($name:ident, $type:ty) => { @@ -305,7 +306,8 @@ macro_rules! vec3_impl { /// Used to represent an exact position in the world where an entity could be. /// /// For blocks, [`BlockPos`] is used instead. -#[derive(AzBuf, Clone, Copy, Debug, Default, serde::Deserialize, PartialEq, serde::Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)] pub struct Vec3 { pub x: f64, pub y: f64, @@ -484,15 +486,19 @@ impl BlockPos { (self - other).length() } } +#[cfg(feature = "serde")] impl serde::Serialize for BlockPos { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { // makes sure it gets serialized correctly for the checksum + + use crate::codec_utils::IntArray; IntArray([self.x, self.y, self.z]).serialize(serializer) } } +#[cfg(feature = "serde")] impl<'de> serde::Deserialize<'de> for BlockPos { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where @@ -753,7 +759,8 @@ impl From<ChunkSectionBlockPos> for u16 { impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {} /// A block pos with an attached world -#[derive(Clone, Debug, PartialEq, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[derive(Clone, Debug, PartialEq)] pub struct GlobalPos { // this is actually a ResourceKey in Minecraft, but i don't think it matters? pub dimension: Identifier, @@ -911,7 +918,8 @@ impl fmt::Display for Vec3 { } /// A 2D vector. -#[derive(AzBuf, Clone, Copy, Debug, Default, simdnbt::Deserialize, PartialEq, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)] pub struct Vec2 { pub x: f32, pub y: f32, diff --git a/azalea-core/src/sound.rs b/azalea-core/src/sound.rs index 418707b9..e7e0f207 100644 --- a/azalea-core/src/sound.rs +++ b/azalea-core/src/sound.rs @@ -1,8 +1,8 @@ use azalea_buf::AzBuf; use azalea_registry::identifier::Identifier; -use serde::Serialize; -#[derive(AzBuf, Clone, Debug, simdnbt::Deserialize, PartialEq, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[derive(AzBuf, Clone, Debug, simdnbt::Deserialize, PartialEq)] pub struct CustomSound { pub sound_id: Identifier, pub range: Option<f32>, |
