1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

fix clientboundsoundpacket

closes #89
This commit is contained in:
mat 2023-06-15 14:37:20 -05:00
parent 804a9fd800
commit fe687f9bdb
2 changed files with 40 additions and 2 deletions

View file

@ -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,

View file

@ -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 {