Skip to content

Inventory desync when updating using set_slot #692

Open
@AwesomeQubic

Description

@AwesomeQubic

Valence Version

2e4bc6e

What You Did

I was editing NBT data inside player's inventory and it did not update on player's side
On server item was present cause it showed up after trying to interact with it

Code

pub fn arcade_enchant(mut query: Query<(&mut Inventory)>, mut reader: EventReader<ClickSlotEvent>) {
    for ele in reader.read() {

        if ele.mode != ClickMode::Click {
            continue;
        }

        let mut inventory = query.get_mut(ele.client).unwrap();


        let book_item = inventory.slot(ele.slot_id as u16);

        if book_item.item != ItemKind::EnchantedBook {
            continue;
        }

        let Some(ref enchantment_book) = book_item.nbt else {
            continue;
        };

        let Some(book_enchants) = enchantments_iter(enchantment_book) else {
            continue;
        };

        let held_item = &ele.carried_item;

        let enchants: Vec<Enchantments> = match held_item.nbt.as_ref().and_then(|x| enchantments_iter(x)) {
            Some(x) => {
                merge_enchantments(x, book_enchants).filter(|x| x.applies_to(&held_item.item.item_type())).collect()
            },
            None => book_enchants.filter(|x| x.applies_to(&held_item.item.item_type())).collect(),
        };

        if enchants.is_empty() {
            continue;
        }

        let new_tag = to_compound(enchants.into_iter());

        let stack = ItemStack {
            item: held_item.item,
            count: held_item.count,
            nbt: Some(new_tag),
        };

        inventory.set_slot(ele.slot_id as u16, stack);
        inventory.into_inner().changed |= 1u64 << (ele.slot_id as u64);
    }
}

What Went Wrong

Player's view of inventory would update after changing NBT data

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageBug has not been confirmed as real issue yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions