Skip to main content

azalea_entity/
data.rs

1//! Define some types needed for entity metadata.
2
3// TODO: this is here because of a bug in enum_as_inner. remove when it's fixed
4#![allow(clippy::double_parens)]
5
6use std::io::{self, Cursor, Write};
7
8use azalea_buf::{AzBuf, AzBufVar, BufReadError};
9use azalea_chat::FormattedText;
10use azalea_core::{
11    direction::Direction,
12    position::{BlockPos, GlobalPos, Vec3f32},
13};
14use azalea_registry::{
15    builtin::{VillagerKind, VillagerProfession},
16    holder::particle::Particle,
17    item::{
18        ItemStack,
19        components::{self, DyeColor},
20    },
21};
22use derive_more::Deref;
23use enum_as_inner::EnumAsInner;
24use tracing::trace;
25use uuid::Uuid;
26
27#[derive(Clone, Debug, Deref, PartialEq)]
28pub struct EntityMetadataItems(pub Vec<EntityDataItem>);
29
30#[derive(Clone, Debug, PartialEq)]
31pub struct EntityDataItem {
32    // we can't identify what the index is for here because we don't know the
33    // entity type
34    pub index: u8,
35    pub value: EntityDataValue,
36}
37
38impl AzBuf for EntityMetadataItems {
39    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
40        let mut metadata = Vec::new();
41        loop {
42            let id = u8::azalea_read(ctx, buf)?;
43            if id == 0xff {
44                break;
45            }
46            let value = EntityDataValue::azalea_read(ctx, buf)?;
47            trace!("{value:?}");
48            metadata.push(EntityDataItem { index: id, value });
49        }
50        Ok(EntityMetadataItems(metadata))
51    }
52    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
53        for item in &self.0 {
54            item.index.azalea_write(ctx, buf)?;
55            item.value.azalea_write(ctx, buf)?;
56        }
57        0xffu8.azalea_write(ctx, buf)?;
58        Ok(())
59    }
60}
61
62// Note: This enum is partially generated and parsed by
63// codegen/lib/code/entity.py
64#[derive(AzBuf, Clone, Debug, EnumAsInner, PartialEq)]
65pub enum EntityDataValue {
66    Byte(u8),
67    Int(#[var] i32),
68    Long(#[var] i64),
69    Float(f32),
70    String(Box<str>),
71    FormattedText(Box<FormattedText>),
72    OptionalFormattedText(Option<Box<FormattedText>>),
73    ItemStack(ItemStack),
74    Boolean(bool),
75    Rotations(Rotations),
76    BlockPos(BlockPos),
77    OptionalBlockPos(Option<BlockPos>),
78    Direction(Direction),
79    OptionalLivingEntityReference(Option<Uuid>),
80    BlockState(azalea_block::BlockState),
81    /// If this is air, that means it's absent,
82    OptionalBlockState(azalea_block::BlockState),
83    Particle(EntityParticle),
84    Particles(Box<[EntityParticle]>),
85    VillagerData(VillagerData),
86    // 0 for absent; 1 + actual value otherwise. Used for entity IDs.
87    OptionalUnsignedInt(OptionalUnsignedInt),
88    Pose(Pose),
89    CatVariant(azalea_registry::data::CatVariant),
90    CatSoundVariant(azalea_registry::data::CatSoundVariant),
91    CowVariant(azalea_registry::data::CowVariant),
92    CowSoundVariant(azalea_registry::data::CowSoundVariant),
93    WolfVariant(azalea_registry::data::WolfVariant),
94    WolfSoundVariant(azalea_registry::data::WolfSoundVariant),
95    FrogVariant(azalea_registry::data::FrogVariant),
96    PigVariant(azalea_registry::data::PigVariant),
97    PigSoundVariant(azalea_registry::data::PigSoundVariant),
98    ChickenVariant(azalea_registry::data::ChickenVariant),
99    ChickenSoundVariant(azalea_registry::data::ChickenSoundVariant),
100    ZombieNautilusVariant(azalea_registry::data::ZombieNautilusVariant),
101    OptionalGlobalPos(Option<Box<GlobalPos>>),
102    PaintingVariant(azalea_registry::data::PaintingVariant),
103    SnifferState(SnifferStateKind),
104    ArmadilloState(ArmadilloStateKind),
105    CopperGolemState(CopperGolemStateKind),
106    WeatheringCopperState(WeatheringCopperStateKind),
107    Vector3(Vec3f32),
108    Quaternion(Quaternion),
109    ResolvableProfile(components::Profile),
110    HumanoidArm(HumanoidArm),
111    DyeColor(DyeColor),
112}
113
114const _: () = assert!(size_of::<EntityDataValue>() == 24);
115
116#[derive(Clone, Debug, PartialEq, Default)]
117pub struct OptionalUnsignedInt(pub Option<u32>);
118
119#[derive(AzBuf, Clone, Debug, PartialEq, Default)]
120pub struct Quaternion {
121    pub x: f32,
122    pub y: f32,
123    pub z: f32,
124    pub w: f32,
125}
126
127// mojang just calls this ArmadilloState but i added "Kind" since otherwise it
128// collides with a name in metadata.rs
129#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
130pub enum ArmadilloStateKind {
131    #[default]
132    Idle,
133    Rolling,
134    Scared,
135}
136
137impl AzBuf for OptionalUnsignedInt {
138    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
139        let val = u32::azalea_read_var(ctx, buf)?;
140        Ok(OptionalUnsignedInt(if val == 0 {
141            None
142        } else {
143            Some(val - 1)
144        }))
145    }
146    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
147        match self.0 {
148            Some(val) => (val + 1).azalea_write_var(ctx, buf),
149            None => 0u32.azalea_write_var(ctx, buf),
150        }
151    }
152}
153
154/// A set of x, y, and z rotations. This is used for armor stands.
155#[derive(AzBuf, Clone, Debug, Default, PartialEq)]
156pub struct Rotations {
157    pub x: f32,
158    pub y: f32,
159    pub z: f32,
160}
161
162#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))]
163#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
164pub enum Pose {
165    #[default]
166    Standing = 0,
167    FallFlying,
168    Sleeping,
169    Swimming,
170    SpinAttack,
171    Crouching,
172    LongJumping,
173    Dying,
174    Croaking,
175    UsingTongue,
176    Sitting,
177    Roaring,
178    Sniffing,
179    Emerging,
180    Digging,
181    Sliding,
182    Shooting,
183    Inhaling,
184}
185
186#[derive(AzBuf, Clone, Debug, PartialEq)]
187pub struct VillagerData {
188    pub kind: VillagerKind,
189    pub profession: VillagerProfession,
190    #[var]
191    pub level: u32,
192}
193
194#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
195pub enum SnifferStateKind {
196    #[default]
197    Idling,
198    FeelingHappy,
199    Scenting,
200    Sniffing,
201    Searching,
202    Digging,
203    Rising,
204}
205
206#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
207pub enum CopperGolemStateKind {
208    #[default]
209    Idle,
210    GettingItem,
211    GettingNoItem,
212    DroppingItem,
213    DroppingNoItem,
214}
215#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
216pub enum WeatheringCopperStateKind {
217    #[default]
218    Unaffected,
219    Exposed,
220    Weathered,
221    Oxidized,
222}
223
224#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
225pub enum HumanoidArm {
226    Left = 0,
227    #[default]
228    Right = 1,
229}
230#[derive(AzBuf, Clone, Debug, Default, PartialEq)]
231pub struct EntityParticle(pub Particle);