From 65fe07215149ab81d0862ab7edac71d6a8417ef8 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 13 Mar 2025 20:46:12 +0000 Subject: [PATCH] fix wrong Consumable component implementation and add set_equipment test --- azalea-core/src/lib.rs | 1 + azalea-core/src/sound.rs | 9 +++++ azalea-inventory/src/components.rs | 7 ++-- azalea-inventory/src/item/consume_effect.rs | 37 +++++++++++++++++++ azalea-inventory/src/item/mod.rs | 2 + .../src/packets/game/c_set_equipment.rs | 19 ++++++++++ azalea-protocol/src/packets/game/c_sound.rs | 8 +--- .../src/packets/game/c_sound_entity.rs | 3 +- azalea-registry/src/lib.rs | 15 ++++++++ 9 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 azalea-core/src/sound.rs create mode 100644 azalea-inventory/src/item/consume_effect.rs diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index ba6d61ef..a539dbd4 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -16,6 +16,7 @@ pub mod objectives; pub mod position; pub mod registry_holder; pub mod resource_location; +pub mod sound; #[cfg(feature = "bevy_ecs")] pub mod tick; pub mod tier; diff --git a/azalea-core/src/sound.rs b/azalea-core/src/sound.rs new file mode 100644 index 00000000..f12205e0 --- /dev/null +++ b/azalea-core/src/sound.rs @@ -0,0 +1,9 @@ +use azalea_buf::AzBuf; + +use crate::resource_location::ResourceLocation; + +#[derive(Clone, Debug, PartialEq, AzBuf)] +pub struct CustomSound { + pub location: ResourceLocation, + pub fixed_range: Option, +} diff --git a/azalea-inventory/src/components.rs b/azalea-inventory/src/components.rs index b299664f..2ff00d5b 100644 --- a/azalea-inventory/src/components.rs +++ b/azalea-inventory/src/components.rs @@ -5,6 +5,7 @@ use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use azalea_chat::FormattedText; use azalea_core::{ filterable::Filterable, position::GlobalPos, resource_location::ResourceLocation, + sound::CustomSound, }; use azalea_registry::{ Attribute, Block, ConsumeEffectKind, DataComponentKind, Enchantment, EntityKind, HolderSet, @@ -14,7 +15,7 @@ use simdnbt::owned::{Nbt, NbtCompound}; use tracing::trace; use uuid::Uuid; -use crate::ItemStack; +use crate::{ItemStack, item::consume_effect::ConsumeEffect}; pub trait DataComponent: Send + Sync + Any { const KIND: DataComponentKind; @@ -800,9 +801,9 @@ impl DataComponent for JukeboxPlayable { pub struct Consumable { pub consume_seconds: f32, pub animation: ItemUseAnimation, - pub sound: SoundEvent, + pub sound: azalea_registry::Holder, pub has_consume_particles: bool, - pub on_consuime_effects: Vec, + pub on_consume_effects: Vec, } impl DataComponent for Consumable { const KIND: DataComponentKind = DataComponentKind::Consumable; diff --git a/azalea-inventory/src/item/consume_effect.rs b/azalea-inventory/src/item/consume_effect.rs new file mode 100644 index 00000000..898f64ff --- /dev/null +++ b/azalea-inventory/src/item/consume_effect.rs @@ -0,0 +1,37 @@ +use azalea_buf::AzBuf; +use azalea_core::resource_location::ResourceLocation; +use azalea_registry::{HolderSet, MobEffect, SoundEvent}; + +use crate::components::MobEffectInstance; + +#[derive(Clone, PartialEq, AzBuf)] +pub enum ConsumeEffect { + ApplyEffects { + effects: Vec, + probability: f32, + }, + RemoveEffects(HolderSet), + ClearAllEffects, + TeleportRandomly { + diameter: f32, + }, + PlaySound { + sound: SoundEvent, + }, +} + +impl From for azalea_registry::ConsumeEffectKind { + fn from(effect: ConsumeEffect) -> Self { + match effect { + ConsumeEffect::ApplyEffects { .. } => azalea_registry::ConsumeEffectKind::ApplyEffects, + ConsumeEffect::RemoveEffects { .. } => { + azalea_registry::ConsumeEffectKind::RemoveEffects + } + ConsumeEffect::ClearAllEffects => azalea_registry::ConsumeEffectKind::ClearAllEffects, + ConsumeEffect::TeleportRandomly { .. } => { + azalea_registry::ConsumeEffectKind::TeleportRandomly + } + ConsumeEffect::PlaySound { .. } => azalea_registry::ConsumeEffectKind::PlaySound, + } + } +} diff --git a/azalea-inventory/src/item/mod.rs b/azalea-inventory/src/item/mod.rs index 7f077c6a..bbff2d50 100644 --- a/azalea-inventory/src/item/mod.rs +++ b/azalea-inventory/src/item/mod.rs @@ -1,3 +1,5 @@ +pub mod consume_effect; + pub trait MaxStackSizeExt { /// Get the maximum stack size for this item. /// diff --git a/azalea-protocol/src/packets/game/c_set_equipment.rs b/azalea-protocol/src/packets/game/c_set_equipment.rs index ad691d66..234de5d9 100755 --- a/azalea-protocol/src/packets/game/c_set_equipment.rs +++ b/azalea-protocol/src/packets/game/c_set_equipment.rs @@ -80,3 +80,22 @@ impl EquipmentSlot { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hypixel_set_equipment() { + let mut cursor = Cursor::new( + [ + 230, 25, 0, 1, 224, 6, 2, 0, 3, 0, 22, 79, 0, 0, 0, 3, 0, 0, 0, 0, 0, + ] + .as_slice(), + ); + + let packet = ClientboundSetEquipment::azalea_read(&mut cursor).unwrap(); + println!("packet {packet:?}"); + assert_eq!(cursor.position(), cursor.get_ref().len() as u64); + } +} diff --git a/azalea-protocol/src/packets/game/c_sound.rs b/azalea-protocol/src/packets/game/c_sound.rs index 8ec028a7..1a54c7c8 100755 --- a/azalea-protocol/src/packets/game/c_sound.rs +++ b/azalea-protocol/src/packets/game/c_sound.rs @@ -1,5 +1,5 @@ use azalea_buf::AzBuf; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::sound::CustomSound; use azalea_protocol_macros::ClientboundGamePacket; use azalea_registry::SoundEvent; @@ -16,12 +16,6 @@ pub struct ClientboundSound { pub seed: u64, } -#[derive(Clone, Debug, AzBuf)] -pub struct CustomSound { - pub location: ResourceLocation, - pub fixed_range: Option, -} - #[derive(AzBuf, Clone, Copy, Debug)] pub enum SoundSource { Master = 0, diff --git a/azalea-protocol/src/packets/game/c_sound_entity.rs b/azalea-protocol/src/packets/game/c_sound_entity.rs index 37ce1f16..a6d17b72 100755 --- a/azalea-protocol/src/packets/game/c_sound_entity.rs +++ b/azalea-protocol/src/packets/game/c_sound_entity.rs @@ -1,9 +1,10 @@ use azalea_buf::AzBuf; +use azalea_core::sound::CustomSound; use azalea_protocol_macros::ClientboundGamePacket; use azalea_registry::SoundEvent; use azalea_world::MinecraftEntityId; -use super::c_sound::{CustomSound, SoundSource}; +use super::c_sound::SoundSource; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSoundEntity { diff --git a/azalea-registry/src/lib.rs b/azalea-registry/src/lib.rs index 4f72689f..59b8101f 100755 --- a/azalea-registry/src/lib.rs +++ b/azalea-registry/src/lib.rs @@ -194,6 +194,17 @@ impl Clone for Ho } } } +impl PartialEq + for Holder +{ + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Reference(a), Self::Reference(b)) => a == b, + (Self::Direct(a), Self::Direct(b)) => a == b, + _ => false, + } + } +} registry! { /// The AI code that's currently being executed for the entity. @@ -3832,6 +3843,10 @@ enum SensorKind { } registry! { +/// A known type of sound in Minecraft. +/// +/// If you need to support custom sounds from resource packs, you should use +/// `azalea_registry::Holder` instead. enum SoundEvent { EntityAllayAmbientWithItem => "minecraft:entity.allay.ambient_with_item", EntityAllayAmbientWithoutItem => "minecraft:entity.allay.ambient_without_item",