aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-06-23 15:12:17 -0500
committermat <github@matdoes.dev>2022-06-23 15:12:17 -0500
commit5ca49e680ed8519456dc9a9af84321d4b69dcbb3 (patch)
tree0f727c3e862f60eb227db69c87946a0f629a397d /azalea-core/src
parentc7b0c51274b5d8548c8a2f829b75dfbec4038be2 (diff)
downloadazalea-drasl-5ca49e680ed8519456dc9a9af84321d4b69dcbb3.tar.xz
azalea-buf
Diffstat (limited to 'azalea-core/src')
-rw-r--r--azalea-core/src/delta.rs26
-rwxr-xr-xazalea-core/src/difficulty.rs12
-rw-r--r--azalea-core/src/direction.rs2
-rwxr-xr-xazalea-core/src/game_type.rs24
-rwxr-xr-xazalea-core/src/lib.rs6
-rw-r--r--azalea-core/src/particle/mod.rs259
-rw-r--r--azalea-core/src/position.rs59
-rwxr-xr-xazalea-core/src/resource_location.rs26
-rwxr-xr-xazalea-core/src/serializable_uuid.rs22
-rw-r--r--azalea-core/src/slot.rs28
10 files changed, 463 insertions, 1 deletions
diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs
new file mode 100644
index 00000000..32517e0d
--- /dev/null
+++ b/azalea-core/src/delta.rs
@@ -0,0 +1,26 @@
+/// Only works for up to 8 blocks
+#[derive(Clone, Debug, McBuf)]
+pub struct PositionDelta {
+ xa: i16,
+ ya: i16,
+ za: i16,
+}
+
+impl PositionDelta {
+ pub fn float(&self) -> (f64, f64, f64) {
+ (
+ (self.xa as f64) / 4096.0,
+ (self.ya as f64) / 4096.0,
+ (self.za as f64) / 4096.0,
+ )
+ }
+}
+
+impl EntityPos {
+ pub fn apply_delta(&mut self, delta: &PositionDelta) {
+ let (x, y, z) = delta.float();
+ self.x += x;
+ self.y += y;
+ self.z += z;
+ }
+}
diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs
index 5d869325..7568f8e2 100755
--- a/azalea-core/src/difficulty.rs
+++ b/azalea-core/src/difficulty.rs
@@ -61,6 +61,18 @@ impl Difficulty {
}
}
+impl McBufReadable for Difficulty {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ Ok(Difficulty::by_id(u8::read_into(buf)?))
+ }
+}
+
+impl McBufWritable for Difficulty {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ u8::write_into(&self.id(), buf)
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/azalea-core/src/direction.rs b/azalea-core/src/direction.rs
index 721f21a0..bb655bdb 100644
--- a/azalea-core/src/direction.rs
+++ b/azalea-core/src/direction.rs
@@ -1,4 +1,4 @@
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy, Debug, McBuf)]
pub enum Direction {
Down = 0,
Up = 1,
diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs
index f5b9fb38..963048dd 100755
--- a/azalea-core/src/game_type.rs
+++ b/azalea-core/src/game_type.rs
@@ -71,3 +71,27 @@ impl GameType {
}
}
}
+
+impl McBufReadable for GameType {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ GameType::from_id(buf.read_byte()?)
+ }
+}
+
+impl McBufReadable for Option<GameType> {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ GameType::from_optional_id(buf.read_byte()? as i8)
+ }
+}
+
+impl McBufWritable for GameType {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ u8::write_into(&self.to_id(), buf)
+ }
+}
+
+impl McBufWritable for Option<GameType> {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ buf.write_byte(GameType::to_optional_id(self) as u8)
+ }
+}
diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs
index a2632871..41c901c8 100755
--- a/azalea-core/src/lib.rs
+++ b/azalea-core/src/lib.rs
@@ -15,3 +15,9 @@ pub use position::*;
mod direction;
pub use direction::Direction;
+
+mod delta;
+pub use delta::*;
+
+mod particle;
+pub use particle::*;
diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs
new file mode 100644
index 00000000..fc815a0b
--- /dev/null
+++ b/azalea-core/src/particle/mod.rs
@@ -0,0 +1,259 @@
+
+#[derive(Debug, Clone, McBuf)]
+pub struct Particle {
+ #[var]
+ pub id: i32,
+ pub data: ParticleData,
+}
+
+#[derive(Clone, Debug)]
+pub enum ParticleData {
+ AmbientEntityEffect,
+ AngryVillager,
+ Block(BlockParticle),
+ BlockMarker(BlockParticle),
+ Bubble,
+ Cloud,
+ Crit,
+ DamageIndicator,
+ DragonBreath,
+ DrippingLava,
+ FallingLava,
+ LandingLava,
+ DrippingWater,
+ FallingWater,
+ Dust(DustParticle),
+ DustColorTransition(DustColorTransitionParticle),
+ Effect,
+ ElderGuardian,
+ EnchantedHit,
+ Enchant,
+ EndRod,
+ EntityEffect,
+ ExplosionEmitter,
+ Explosion,
+ FallingDust(BlockParticle),
+ Firework,
+ Fishing,
+ Flame,
+ SoulFireFlame,
+ Soul,
+ Flash,
+ HappyVillager,
+ Composter,
+ Heart,
+ InstantEffect,
+ Item(ItemParticle),
+ Vibration(VibrationParticle),
+ ItemSlime,
+ ItemSnowball,
+ LargeSmoke,
+ Lava,
+ Mycelium,
+ Note,
+ Poof,
+ Portal,
+ Rain,
+ Smoke,
+ Sneeze,
+ Spit,
+ SquidInk,
+ SweepAttack,
+ TotemOfUndying,
+ Underwater,
+ Splash,
+ Witch,
+ BubblePop,
+ CurrentDown,
+ BubbleColumnUp,
+ Nautilus,
+ Dolphin,
+ CampfireCozySmoke,
+ CampfireSignalSmoke,
+ DrippingHoney,
+ FallingHoney,
+ LandingHoney,
+ FallingNectar,
+ FallingSporeBlossom,
+ Ash,
+ CrimsonSpore,
+ WarpedSpore,
+ SporeBlossomAir,
+ DrippingObsidianTear,
+ FallingObsidianTear,
+ LandingObsidianTear,
+ ReversePortal,
+ WhiteAsh,
+ SmallFlame,
+ Snowflake,
+ DrippingDripstoneLava,
+ FallingDripstoneLava,
+ DrippingDripstoneWater,
+ FallingDripstoneWater,
+ GlowSquidInk,
+ Glow,
+ WaxOn,
+ WaxOff,
+ ElectricSpark,
+ Scrape,
+}
+
+#[derive(Debug, Clone, McBuf)]
+pub struct BlockParticle {
+ #[var]
+ pub block_state: i32,
+}
+#[derive(Debug, Clone, McBuf)]
+pub struct DustParticle {
+ /// Red value, 0-1
+ pub red: f32,
+ /// Green value, 0-1
+ pub green: f32,
+ /// Blue value, 0-1
+ pub blue: f32,
+ /// The scale, will be clamped between 0.01 and 4.
+ pub scale: f32,
+}
+
+#[derive(Debug, Clone, McBuf)]
+pub struct DustColorTransitionParticle {
+ /// Red value, 0-1
+ pub from_red: f32,
+ /// Green value, 0-1
+ pub from_green: f32,
+ /// Blue value, 0-1
+ pub from_blue: f32,
+ /// The scale, will be clamped between 0.01 and 4.
+ pub scale: f32,
+ /// Red value, 0-1
+ pub to_red: f32,
+ /// Green value, 0-1
+ pub to_green: f32,
+ /// Blue value, 0-1
+ pub to_blue: f32,
+}
+
+#[derive(Debug, Clone, McBuf)]
+pub struct ItemParticle {
+ pub item: Slot,
+}
+
+#[derive(Debug, Clone, McBuf)]
+pub struct VibrationParticle {
+ pub origin: BlockPos,
+ pub position_type: String,
+ pub block_position: BlockPos,
+ #[var]
+ pub entity_id: u32,
+ #[var]
+ pub ticks: u32,
+}
+
+impl ParticleData {
+ pub fn read_from_particle_id(buf: &mut impl Read, id: u32) -> Result<Self, String> {
+ Ok(match id {
+ 0 => ParticleData::AmbientEntityEffect,
+ 1 => ParticleData::AngryVillager,
+ 2 => ParticleData::Block(BlockParticle::read_into(buf)?),
+ 3 => ParticleData::BlockMarker(BlockParticle::read_into(buf)?),
+ 4 => ParticleData::Bubble,
+ 5 => ParticleData::Cloud,
+ 6 => ParticleData::Crit,
+ 7 => ParticleData::DamageIndicator,
+ 8 => ParticleData::DragonBreath,
+ 9 => ParticleData::DrippingLava,
+ 10 => ParticleData::FallingLava,
+ 11 => ParticleData::LandingLava,
+ 12 => ParticleData::DrippingWater,
+ 13 => ParticleData::FallingWater,
+ 14 => ParticleData::Dust(DustParticle::read_into(buf)?),
+ 15 => ParticleData::DustColorTransition(DustColorTransitionParticle::read_into(buf)?),
+ 16 => ParticleData::Effect,
+ 17 => ParticleData::ElderGuardian,
+ 18 => ParticleData::EnchantedHit,
+ 19 => ParticleData::Enchant,
+ 20 => ParticleData::EndRod,
+ 21 => ParticleData::EntityEffect,
+ 22 => ParticleData::ExplosionEmitter,
+ 23 => ParticleData::Explosion,
+ 24 => ParticleData::FallingDust(BlockParticle::read_into(buf)?),
+ 25 => ParticleData::Firework,
+ 26 => ParticleData::Fishing,
+ 27 => ParticleData::Flame,
+ 28 => ParticleData::SoulFireFlame,
+ 29 => ParticleData::Soul,
+ 30 => ParticleData::Flash,
+ 31 => ParticleData::HappyVillager,
+ 32 => ParticleData::Composter,
+ 33 => ParticleData::Heart,
+ 34 => ParticleData::InstantEffect,
+ 35 => ParticleData::Item(ItemParticle::read_into(buf)?),
+ 36 => ParticleData::Vibration(VibrationParticle::read_into(buf)?),
+ 37 => ParticleData::ItemSlime,
+ 38 => ParticleData::ItemSnowball,
+ 39 => ParticleData::LargeSmoke,
+ 40 => ParticleData::Lava,
+ 41 => ParticleData::Mycelium,
+ 42 => ParticleData::Note,
+ 43 => ParticleData::Poof,
+ 44 => ParticleData::Portal,
+ 45 => ParticleData::Rain,
+ 46 => ParticleData::Smoke,
+ 47 => ParticleData::Sneeze,
+ 48 => ParticleData::Spit,
+ 49 => ParticleData::SquidInk,
+ 50 => ParticleData::SweepAttack,
+ 51 => ParticleData::TotemOfUndying,
+ 52 => ParticleData::Underwater,
+ 53 => ParticleData::Splash,
+ 54 => ParticleData::Witch,
+ 55 => ParticleData::BubblePop,
+ 56 => ParticleData::CurrentDown,
+ 57 => ParticleData::BubbleColumnUp,
+ 58 => ParticleData::Nautilus,
+ 59 => ParticleData::Dolphin,
+ 60 => ParticleData::CampfireCozySmoke,
+ 61 => ParticleData::CampfireSignalSmoke,
+ 62 => ParticleData::DrippingHoney,
+ 63 => ParticleData::FallingHoney,
+ 64 => ParticleData::LandingHoney,
+ 65 => ParticleData::FallingNectar,
+ 66 => ParticleData::FallingSporeBlossom,
+ 67 => ParticleData::Ash,
+ 68 => ParticleData::CrimsonSpore,
+ 69 => ParticleData::WarpedSpore,
+ 70 => ParticleData::SporeBlossomAir,
+ 71 => ParticleData::DrippingObsidianTear,
+ 72 => ParticleData::FallingObsidianTear,
+ 73 => ParticleData::LandingObsidianTear,
+ 74 => ParticleData::ReversePortal,
+ 75 => ParticleData::WhiteAsh,
+ 76 => ParticleData::SmallFlame,
+ 77 => ParticleData::Snowflake,
+ 78 => ParticleData::DrippingDripstoneLava,
+ 79 => ParticleData::FallingDripstoneLava,
+ 80 => ParticleData::DrippingDripstoneWater,
+ 81 => ParticleData::FallingDripstoneWater,
+ 82 => ParticleData::GlowSquidInk,
+ 83 => ParticleData::Glow,
+ 84 => ParticleData::WaxOn,
+ 85 => ParticleData::WaxOff,
+ 86 => ParticleData::ElectricSpark,
+ 87 => ParticleData::Scrape,
+ _ => return Err(format!("Unknown particle id: {}", id)),
+ })
+ }
+}
+
+impl McBufReadable for ParticleData {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let id = u32::var_read_into(buf)?;
+ ParticleData::read_from_particle_id(buf, id)
+ }
+}
+
+impl McBufWritable for ParticleData {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ todo!()
+ }
+}
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index 7b2c01da..9e7aca20 100644
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -170,6 +170,65 @@ impl From<&EntityPos> for ChunkPos {
}
}
+impl McBufReadable for BlockPos {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let val = u64::read_into(buf)?;
+ let x = (val >> 38) as i32;
+ let y = (val & 0xFFF) as i32;
+ let z = ((val >> 12) & 0x3FFFFFF) as i32;
+ Ok(BlockPos { x, y, z })
+ }
+}
+
+impl McBufReadable for GlobalPos {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ Ok(GlobalPos {
+ dimension: ResourceLocation::read_into(buf)?,
+ pos: BlockPos::read_into(buf)?,
+ })
+ }
+}
+
+impl McBufReadable for ChunkSectionPos {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let long = i64::read_into(buf)?;
+ Ok(ChunkSectionPos {
+ x: (long >> 42) as i32,
+ y: (long << 44 >> 44) as i32,
+ z: (long << 22 >> 42) as i32,
+ })
+ }
+}
+
+impl McBufWritable for BlockPos {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ buf.write_long(
+ (((self.x & 0x3FFFFFF) as i64) << 38)
+ | (((self.z & 0x3FFFFFF) as i64) << 12)
+ | ((self.y & 0xFFF) as i64),
+ )
+ }
+}
+
+impl McBufWritable for GlobalPos {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ ResourceLocation::write_into(&self.dimension, buf)?;
+ BlockPos::write_into(&self.pos, buf)?;
+
+ Ok(())
+ }
+}
+
+impl McBufWritable for ChunkSectionPos {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ let long = (((self.x & 0x3FFFFF) as i64) << 42)
+ | (self.y & 0xFFFFF) as i64
+ | (((self.z & 0x3FFFFF) as i64) << 20);
+ long.write_into(buf)?;
+ Ok(())
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs
index cdf8f381..6807714b 100755
--- a/azalea-core/src/resource_location.rs
+++ b/azalea-core/src/resource_location.rs
@@ -42,6 +42,18 @@ impl std::fmt::Debug for ResourceLocation {
}
}
+impl McBufReadable for ResourceLocation {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let location_string = self.read_utf()?;
+ ResourceLocation::new(&location_string)
+ }
+}
+impl McBufWritable for ResourceLocation {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ buf.write_utf(&self.to_string())
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -70,4 +82,18 @@ mod tests {
assert_eq!(r.namespace, "azalea");
assert_eq!(r.path, "");
}
+
+ #[test]
+ fn mcbuf_resource_location() {
+ let mut buf = Vec::new();
+ buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap())
+ .unwrap();
+
+ let mut buf = Cursor::new(buf);
+
+ assert_eq!(
+ buf.read_resource_location().unwrap(),
+ ResourceLocation::new("minecraft:dirt").unwrap()
+ );
+ }
}
diff --git a/azalea-core/src/serializable_uuid.rs b/azalea-core/src/serializable_uuid.rs
index f8c03b60..2c7128ff 100755
--- a/azalea-core/src/serializable_uuid.rs
+++ b/azalea-core/src/serializable_uuid.rs
@@ -30,6 +30,28 @@ impl SerializableUuid for Uuid {
}
}
+impl McBufReadable for Uuid {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ Ok(Uuid::from_int_array([
+ Readable::read_int(self)? as u32,
+ Readable::read_int(self)? as u32,
+ Readable::read_int(self)? as u32,
+ Readable::read_int(self)? as u32,
+ ]))
+ }
+}
+
+impl McBufWritable for Uuid {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ let [a, b, c, d] = self.to_int_array();
+ a.write_into(buf)?;
+ b.write_into(buf)?;
+ c.write_into(buf)?;
+ d.write_into(buf)?;
+ Ok(())
+ }
+}
+
#[cfg(tests)]
mod tests {
use super::*;
diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs
index 5e42f558..e3b78289 100644
--- a/azalea-core/src/slot.rs
+++ b/azalea-core/src/slot.rs
@@ -12,3 +12,31 @@ pub struct SlotData {
pub count: u8,
pub nbt: azalea_nbt::Tag,
}
+
+impl McBufReadable for Slot {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let present = buf.read_boolean()?;
+ if !present {
+ return Ok(Slot::Empty);
+ }
+ let id = buf.read_varint()?;
+ let count = buf.read_byte()?;
+ let nbt = buf.read_nbt()?;
+ Ok(Slot::Present(SlotData { id, count, nbt }))
+ }
+}
+
+impl McBufWritable for Slot {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ match self {
+ Slot::Empty => buf.write_byte(0)?,
+ Slot::Present(i) => {
+ buf.write_varint(i.id)?;
+ buf.write_byte(i.count)?;
+ buf.write_nbt(&i.nbt)?;
+ }
+ }
+
+ Ok(())
+ }
+}