diff options
| -rwxr-xr-x | azalea-protocol/src/packets/game/clientbound_sound_packet.rs | 10 | ||||
| -rwxr-xr-x | azalea-registry/src/lib.rs | 32 |
2 files changed, 40 insertions, 2 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_sound_packet.rs b/azalea-protocol/src/packets/game/clientbound_sound_packet.rs index 39dd39c8..2b071f2e 100755 --- a/azalea-protocol/src/packets/game/clientbound_sound_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_sound_packet.rs @@ -1,10 +1,10 @@ use azalea_buf::McBuf; +use azalea_core::ResourceLocation; use azalea_protocol_macros::ClientboundGamePacket; -use azalea_registry::OptionalRegistry; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] pub struct ClientboundSoundPacket { - pub sound: OptionalRegistry<azalea_registry::SoundEvent>, + pub sound: azalea_registry::CustomRegistry<azalea_registry::SoundEvent, CustomSoundEvent>, pub source: SoundSource, pub x: i32, pub y: i32, @@ -14,6 +14,12 @@ pub struct ClientboundSoundPacket { pub seed: u64, } +#[derive(McBuf, Clone, Debug)] +pub struct CustomSoundEvent { + pub location: ResourceLocation, + pub range: Option<f32>, +} + #[derive(McBuf, Clone, Copy, Debug)] pub enum SoundSource { Master = 0, diff --git a/azalea-registry/src/lib.rs b/azalea-registry/src/lib.rs index e1188216..081b3c3e 100755 --- a/azalea-registry/src/lib.rs +++ b/azalea-registry/src/lib.rs @@ -43,6 +43,38 @@ impl<T: Registry> McBufWritable for OptionalRegistry<T> { } } +/// A registry that will either take an ID or a resource location. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum CustomRegistry<D: Registry, C: McBufReadable + McBufWritable> { + Direct(D), + Custom(C), +} + +impl<D: Registry, C: McBufReadable + McBufWritable> McBufReadable for CustomRegistry<D, C> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let direct_registry = OptionalRegistry::<D>::read_from(buf)?; + if let Some(direct_registry) = direct_registry.0 { + return Ok(CustomRegistry::Direct(direct_registry)); + } + Ok(CustomRegistry::Custom(C::read_from(buf)?)) + } +} +impl<D: Registry, C: McBufReadable + McBufWritable> McBufWritable for CustomRegistry<D, C> { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + match self { + CustomRegistry::Direct(direct_registry) => { + // write the id + 1 + (direct_registry.to_u32() + 1).var_write_into(buf) + } + CustomRegistry::Custom(custom_registry) => { + // write 0, then the custom registry + 0u32.var_write_into(buf)?; + custom_registry.write_into(buf) + } + } + } +} + registry! { /// The AI code that's currently being executed for the entity. enum Activity { |
