Skip to content

Commit

Permalink
fix bugs with decoding/encoding DataComponentPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jan 12, 2025
1 parent 093c99a commit a1435b3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions azalea-inventory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ azalea-chat = { path = "../azalea-chat", version = "0.11.0", features = [
azalea-core = { path = "../azalea-core", version = "0.11.0" }
azalea-inventory-macros = { path = "./azalea-inventory-macros", version = "0.11.0" }
azalea-registry = { path = "../azalea-registry", version = "0.11.0" }
indexmap = "2.7.0"

simdnbt = { workspace = true }
uuid = { workspace = true }
11 changes: 7 additions & 4 deletions azalea-inventory/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ pub enum AttributeModifierOperation {
// circular dependency)
#[derive(Clone, PartialEq, AzBuf)]
pub struct AttributeModifier {
pub uuid: Uuid,
pub name: String,
pub id: ResourceLocation,
pub amount: f64,
pub operation: AttributeModifierOperation,
}
Expand Down Expand Up @@ -877,8 +876,12 @@ impl DataComponent for DamageResistant {
pub struct Equippable {
pub slot: EquipmentSlot,
pub equip_sound: SoundEvent,
pub model: Option<ResourceLocation>,
pub allowed_entities: HolderSet<EntityKind, ResourceLocation>,
pub asset_id: Option<ResourceLocation>,
pub camera_overlay: Option<ResourceLocation>,
pub allowed_entities: Option<HolderSet<EntityKind, ResourceLocation>>,
pub dispensable: bool,
pub swappable: bool,
pub damage_on_hurt: bool,
}
impl DataComponent for Equippable {
const KIND: DataComponentKind = DataComponentKind::Equippable;
Expand Down
20 changes: 11 additions & 9 deletions azalea-inventory/src/slot.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{
any::Any,
collections::HashMap,
fmt,
io::{Cursor, Write},
};

use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError};
use azalea_registry::DataComponentKind;
use indexmap::IndexMap;

use crate::components::{self};

Expand Down Expand Up @@ -178,7 +178,7 @@ impl AzaleaWrite for ItemStack {
/// and Azalea does not implement that yet.
#[derive(Default)]
pub struct DataComponentPatch {
components: HashMap<DataComponentKind, Option<Box<dyn components::EncodableDataComponent>>>,
components: IndexMap<DataComponentKind, Option<Box<dyn components::EncodableDataComponent>>>,
}

impl DataComponentPatch {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl AzaleaRead for DataComponentPatch {
return Ok(DataComponentPatch::default());
}

let mut components = HashMap::new();
let mut components = IndexMap::new();
for _ in 0..components_with_data_count {
let component_kind = DataComponentKind::azalea_read(buf)?;
let component_data = components::from_kind(component_kind, buf)?;
Expand All @@ -256,8 +256,8 @@ impl AzaleaRead for DataComponentPatch {

impl AzaleaWrite for DataComponentPatch {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut components_with_data_count = 0;
let mut components_without_data_count = 0;
let mut components_with_data_count: u32 = 0;
let mut components_without_data_count: u32 = 0;
for component in self.components.values() {
if component.is_some() {
components_with_data_count += 1;
Expand All @@ -269,12 +269,14 @@ impl AzaleaWrite for DataComponentPatch {
components_with_data_count.azalea_write_var(buf)?;
components_without_data_count.azalea_write_var(buf)?;

let mut component_buf = Vec::new();
for (kind, component) in &self.components {
if let Some(component) = component {
kind.azalea_write(buf)?;
let mut component_buf = Vec::new();
component.encode(&mut component_buf).unwrap();
component_buf.azalea_write(buf)?;

component_buf.clear();
component.encode(&mut component_buf)?;
buf.write_all(&component_buf)?;
}
}

Expand All @@ -290,7 +292,7 @@ impl AzaleaWrite for DataComponentPatch {

impl Clone for DataComponentPatch {
fn clone(&self) -> Self {
let mut components = HashMap::with_capacity(self.components.len());
let mut components = IndexMap::with_capacity(self.components.len());
for (kind, component) in &self.components {
components.insert(*kind, component.as_ref().map(|c| (*c).clone()));
}
Expand Down
32 changes: 31 additions & 1 deletion azalea-protocol/src/packets/game/c_container_set_content.rs

Large diffs are not rendered by default.

0 comments on commit a1435b3

Please sign in to comment.