From 08958c2278b15ebeac8a964f392ebb792e479b61 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:31:40 -0600 Subject: Refactor azalea-protocol (#190) * start updating to 1.21.4 * fix block codegen and stop using block data from burger * rename packet related modules and structs to be simpler * ItemSlot -> ItemStack for more consistency with mojmap * .get() -> .into_packet() * simplify declare_state_packets by removing packet ids * rename read_from and write_into to azalea_read and azalea_write * rename McBufReadable and McBufWritable to AzaleaRead and AzaleaWrite * McBuf -> AzBuf * remove most uses of into_variant * update codegen and use resourcelocation names for packets * implement #[limit(i)] attribute for AzBuf derive macro * fixes for 1.21.4 * fix examples * update some physics code and fix ChatType * remove unused imports in codegen * re-add some things to migrate.py and update +mc version numbers automatically * downgrade to 1.21.3 lol --- azalea-entity/src/attributes.rs | 6 ++--- azalea-entity/src/data.rs | 54 ++++++++++++++++++++--------------------- azalea-entity/src/metadata.rs | 38 ++++++++++++++--------------- azalea-entity/src/particle.rs | 27 +++++++++++---------- 4 files changed, 62 insertions(+), 63 deletions(-) (limited to 'azalea-entity/src') diff --git a/azalea-entity/src/attributes.rs b/azalea-entity/src/attributes.rs index ddafaec3..797ea43c 100644 --- a/azalea-entity/src/attributes.rs +++ b/azalea-entity/src/attributes.rs @@ -2,7 +2,7 @@ use std::collections::{hash_map, HashMap}; -use azalea_buf::McBuf; +use azalea_buf::AzBuf; use azalea_core::resource_location::ResourceLocation; use bevy_ecs::component::Component; use thiserror::Error; @@ -71,14 +71,14 @@ impl AttributeInstance { } } -#[derive(Clone, Debug, McBuf)] +#[derive(Clone, Debug, AzBuf)] pub struct AttributeModifier { pub id: ResourceLocation, pub amount: f64, pub operation: AttributeModifierOperation, } -#[derive(Clone, Debug, Copy, McBuf)] +#[derive(Clone, Debug, Copy, AzBuf)] pub enum AttributeModifierOperation { Addition, MultiplyBase, diff --git a/azalea-entity/src/data.rs b/azalea-entity/src/data.rs index 050302ca..084fd26e 100755 --- a/azalea-entity/src/data.rs +++ b/azalea-entity/src/data.rs @@ -2,15 +2,13 @@ use std::io::{Cursor, Write}; -use azalea_buf::{ - BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, -}; +use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_chat::FormattedText; use azalea_core::{ direction::Direction, position::{BlockPos, GlobalPos, Vec3}, }; -use azalea_inventory::ItemSlot; +use azalea_inventory::ItemStack; use bevy_ecs::component::Component; use derive_more::Deref; use enum_as_inner::EnumAsInner; @@ -29,35 +27,35 @@ pub struct EntityDataItem { pub value: EntityDataValue, } -impl McBufReadable for EntityMetadataItems { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result { +impl AzaleaRead for EntityMetadataItems { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { let mut metadata = Vec::new(); loop { - let id = u8::read_from(buf)?; + let id = u8::azalea_read(buf)?; if id == 0xff { break; } - let value = EntityDataValue::read_from(buf)?; + let value = EntityDataValue::azalea_read(buf)?; metadata.push(EntityDataItem { index: id, value }); } Ok(EntityMetadataItems(metadata)) } } -impl McBufWritable for EntityMetadataItems { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for EntityMetadataItems { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { for item in &self.0 { - item.index.write_into(buf)?; - item.value.write_into(buf)?; + item.index.azalea_write(buf)?; + item.value.azalea_write(buf)?; } - 0xffu8.write_into(buf)?; + 0xffu8.azalea_write(buf)?; Ok(()) } } // Note: This enum is partially generated and parsed by // codegen/lib/code/entity.py -#[derive(Clone, Debug, EnumAsInner, McBuf)] +#[derive(Clone, Debug, EnumAsInner, AzBuf)] pub enum EntityDataValue { Byte(u8), Int(#[var] i32), @@ -66,7 +64,7 @@ pub enum EntityDataValue { String(String), FormattedText(FormattedText), OptionalFormattedText(Option), - ItemStack(ItemSlot), + ItemStack(ItemStack), Boolean(bool), Rotations(Rotations), BlockPos(BlockPos), @@ -97,7 +95,7 @@ pub enum EntityDataValue { #[derive(Clone, Debug)] pub struct OptionalUnsignedInt(pub Option); -#[derive(Clone, Debug, McBuf)] +#[derive(Clone, Debug, AzBuf)] pub struct Quaternion { pub x: f32, pub y: f32, @@ -107,7 +105,7 @@ pub struct Quaternion { // mojang just calls this ArmadilloState but i added "Kind" since otherwise it // collides with a name in metadata.rs -#[derive(Clone, Debug, Copy, Default, McBuf)] +#[derive(Clone, Debug, Copy, Default, AzBuf)] pub enum ArmadilloStateKind { #[default] Idle, @@ -115,9 +113,9 @@ pub enum ArmadilloStateKind { Scared, } -impl McBufReadable for OptionalUnsignedInt { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result { - let val = u32::var_read_from(buf)?; +impl AzaleaRead for OptionalUnsignedInt { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { + let val = u32::azalea_read_var(buf)?; Ok(OptionalUnsignedInt(if val == 0 { None } else { @@ -125,24 +123,24 @@ impl McBufReadable for OptionalUnsignedInt { })) } } -impl McBufWritable for OptionalUnsignedInt { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for OptionalUnsignedInt { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match self.0 { - Some(val) => (val + 1).var_write_into(buf), - None => 0u32.var_write_into(buf), + Some(val) => (val + 1).azalea_write_var(buf), + None => 0u32.azalea_write_var(buf), } } } /// A set of x, y, and z rotations. This is used for armor stands. -#[derive(Clone, Debug, McBuf, Default)] +#[derive(Clone, Debug, AzBuf, Default)] pub struct Rotations { pub x: f32, pub y: f32, pub z: f32, } -#[derive(Clone, Debug, Copy, McBuf, Default, Component, Eq, PartialEq)] +#[derive(Clone, Debug, Copy, AzBuf, Default, Component, Eq, PartialEq)] pub enum Pose { #[default] Standing = 0, @@ -155,7 +153,7 @@ pub enum Pose { Dying, } -#[derive(Debug, Clone, McBuf)] +#[derive(Debug, Clone, AzBuf)] pub struct VillagerData { pub kind: azalea_registry::VillagerKind, pub profession: azalea_registry::VillagerProfession, @@ -163,7 +161,7 @@ pub struct VillagerData { pub level: u32, } -#[derive(Debug, Copy, Clone, McBuf, Default)] +#[derive(Debug, Copy, Clone, AzBuf, Default)] pub enum SnifferState { #[default] Idling, diff --git a/azalea-entity/src/metadata.rs b/azalea-entity/src/metadata.rs index 2a6f996e..4054bfb3 100644 --- a/azalea-entity/src/metadata.rs +++ b/azalea-entity/src/metadata.rs @@ -8,7 +8,7 @@ use azalea_core::{ direction::Direction, position::{BlockPos, Vec3}, }; -use azalea_inventory::ItemSlot; +use azalea_inventory::ItemStack; use bevy_ecs::{bundle::Bundle, component::Component}; use derive_more::{Deref, DerefMut}; use thiserror::Error; @@ -3301,7 +3301,7 @@ impl Default for DrownedMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct EggItemStack(pub ItemSlot); +pub struct EggItemStack(pub ItemStack); #[derive(Component)] pub struct Egg; impl Egg { @@ -3558,7 +3558,7 @@ impl Default for EnderDragonMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct EnderPearlItemStack(pub ItemSlot); +pub struct EnderPearlItemStack(pub ItemStack); #[derive(Component)] pub struct EnderPearl; impl EnderPearl { @@ -3894,7 +3894,7 @@ impl Default for EvokerFangsMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct ExperienceBottleItemStack(pub ItemSlot); +pub struct ExperienceBottleItemStack(pub ItemStack); #[derive(Component)] pub struct ExperienceBottle; impl ExperienceBottle { @@ -3991,7 +3991,7 @@ impl Default for ExperienceOrbMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct EyeOfEnderItemStack(pub ItemSlot); +pub struct EyeOfEnderItemStack(pub ItemStack); #[derive(Component)] pub struct EyeOfEnder; impl EyeOfEnder { @@ -4095,7 +4095,7 @@ impl Default for FallingBlockMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct FireballItemStack(pub ItemSlot); +pub struct FireballItemStack(pub ItemStack); #[derive(Component)] pub struct Fireball; impl Fireball { @@ -4147,7 +4147,7 @@ impl Default for FireballMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct FireworksItem(pub ItemSlot); +pub struct FireworksItem(pub ItemStack); #[derive(Component, Deref, DerefMut, Clone)] pub struct AttachedToTarget(pub OptionalUnsignedInt); #[derive(Component, Deref, DerefMut, Clone)] @@ -4682,7 +4682,7 @@ impl Default for GiantMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct ItemFrameItem(pub ItemSlot); +pub struct ItemFrameItem(pub ItemStack); #[derive(Component, Deref, DerefMut, Clone)] pub struct Rotation(pub i32); #[derive(Component)] @@ -4728,7 +4728,7 @@ impl Default for GlowItemFrameMetadataBundle { pose: Pose::default(), ticks_frozen: TicksFrozen(Default::default()), }, - item_frame_item: ItemFrameItem(ItemSlot::Empty), + item_frame_item: ItemFrameItem(ItemStack::Empty), rotation: Rotation(0), }, } @@ -5521,7 +5521,7 @@ impl Default for IronGolemMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct ItemItem(pub ItemSlot); +pub struct ItemItem(pub ItemStack); #[derive(Component)] pub struct Item; impl Item { @@ -5567,7 +5567,7 @@ impl Default for ItemMetadataBundle { pose: Pose::default(), ticks_frozen: TicksFrozen(Default::default()), }, - item_item: ItemItem(ItemSlot::Empty), + item_item: ItemItem(ItemStack::Empty), } } } @@ -5603,7 +5603,7 @@ pub struct ItemDisplayHeight(pub f32); #[derive(Component, Deref, DerefMut, Clone)] pub struct ItemDisplayGlowColorOverride(pub i32); #[derive(Component, Deref, DerefMut, Clone)] -pub struct ItemDisplayItemStack(pub ItemSlot); +pub struct ItemDisplayItemStack(pub ItemStack); #[derive(Component, Deref, DerefMut, Clone)] pub struct ItemDisplayItemDisplay(pub u8); #[derive(Component)] @@ -5758,7 +5758,7 @@ impl Default for ItemDisplayMetadataBundle { item_display_width: ItemDisplayWidth(0.0), item_display_height: ItemDisplayHeight(0.0), item_display_glow_color_override: ItemDisplayGlowColorOverride(-1), - item_display_item_stack: ItemDisplayItemStack(ItemSlot::Empty), + item_display_item_stack: ItemDisplayItemStack(ItemStack::Empty), item_display_item_display: ItemDisplayItemDisplay(Default::default()), } } @@ -5813,7 +5813,7 @@ impl Default for ItemFrameMetadataBundle { pose: Pose::default(), ticks_frozen: TicksFrozen(Default::default()), }, - item_frame_item: ItemFrameItem(ItemSlot::Empty), + item_frame_item: ItemFrameItem(ItemStack::Empty), rotation: Rotation(0), } } @@ -7033,7 +7033,7 @@ impl Default for OcelotMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct OminousItemSpawnerItem(pub ItemSlot); +pub struct OminousItemSpawnerItem(pub ItemStack); #[derive(Component)] pub struct OminousItemSpawner; impl OminousItemSpawner { @@ -7079,7 +7079,7 @@ impl Default for OminousItemSpawnerMetadataBundle { pose: Pose::default(), ticks_frozen: TicksFrozen(Default::default()), }, - ominous_item_spawner_item: OminousItemSpawnerItem(ItemSlot::Empty), + ominous_item_spawner_item: OminousItemSpawnerItem(ItemStack::Empty), } } } @@ -8111,7 +8111,7 @@ impl Default for PolarBearMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct PotionItemStack(pub ItemSlot); +pub struct PotionItemStack(pub ItemStack); #[derive(Component)] pub struct Potion; impl Potion { @@ -8996,7 +8996,7 @@ impl Default for SlimeMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct SmallFireballItemStack(pub ItemSlot); +pub struct SmallFireballItemStack(pub ItemStack); #[derive(Component)] pub struct SmallFireball; impl SmallFireball { @@ -9207,7 +9207,7 @@ impl Default for SnowGolemMetadataBundle { } #[derive(Component, Deref, DerefMut, Clone)] -pub struct SnowballItemStack(pub ItemSlot); +pub struct SnowballItemStack(pub ItemStack); #[derive(Component)] pub struct Snowball; impl Snowball { diff --git a/azalea-entity/src/particle.rs b/azalea-entity/src/particle.rs index 6484b28f..fa948f85 100755 --- a/azalea-entity/src/particle.rs +++ b/azalea-entity/src/particle.rs @@ -1,13 +1,13 @@ -use azalea_buf::McBuf; +use azalea_buf::AzBuf; use azalea_core::position::BlockPos; -use azalea_inventory::ItemSlot; +use azalea_inventory::ItemStack; use azalea_registry::ParticleKind; use bevy_ecs::component::Component; -// the order of this enum must be kept in-sync with ParticleKind, otherwise +// the order of this enum must be kept in sync with ParticleKind, otherwise // we get errors parsing particles. /// A [`ParticleKind`] with data potentially attached to it. -#[derive(Component, Clone, Debug, McBuf, Default)] +#[derive(Component, Clone, Debug, AzBuf, Default)] pub enum Particle { AngryVillager, Block(BlockParticle), @@ -44,6 +44,7 @@ pub enum Particle { Flame, Infested, CherryLeaves, + PaleOakLeaves, SculkSoul, SculkCharge(SculkChargeParticle), SculkChargePop, @@ -128,7 +129,7 @@ impl From for Particle { /// attached (like block particles), then it's set to the default. fn from(kind: ParticleKind) -> Self { // this is mostly just here so it fails to compile when a new particle is added - // to ParticleKind, since ParticleData has to be updated manually + // to ParticleKind, since `Particle` has to be updated manually match kind { ParticleKind::AngryVillager => Self::AngryVillager, ParticleKind::Block => Self::Block(BlockParticle::default()), @@ -247,12 +248,12 @@ impl From for Particle { } } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct BlockParticle { #[var] pub block_state: i32, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct DustParticle { /// Red value, 0-1 pub red: f32, @@ -264,7 +265,7 @@ pub struct DustParticle { pub scale: f32, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct DustColorTransitionParticle { /// Red value, 0-1 pub from_red: f32, @@ -282,12 +283,12 @@ pub struct DustColorTransitionParticle { pub to_blue: f32, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct ItemParticle { - pub item: ItemSlot, + pub item: ItemStack, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct VibrationParticle { pub origin: BlockPos, pub position_type: String, @@ -298,12 +299,12 @@ pub struct VibrationParticle { pub ticks: u32, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct SculkChargeParticle { pub roll: f32, } -#[derive(Debug, Clone, McBuf, Default)] +#[derive(Debug, Clone, AzBuf, Default)] pub struct ShriekParticle { #[var] pub delay: i32, // The time in ticks before the particle is displayed -- cgit v1.2.3