Change inventory archetype

So after a few tests it looks like when the inventory is open, it uses a MinecraftInventoryAdapter as the instance of the Inventory, which is why it comes out as InventoryArchetype.UNKNOWN. However looking at your code, you seem to just be checking the archetype and then overriding the original inventory with the new one?

npc.storage = e.getTargetInventory().root().first();

Wouldnt it just be better to update the original NPC inventory and not override it? Therefore the inventory achetype stays the same.

How would I do that? In bukkit you can loop over every slot and just copy it over. How would I do that here?

    Inventory inventoryToCopy;
    Inventory inventoryToCopyTo;
    inventoryToCopyTo.clear(); //just to make sure
    inventoryToCopy.slots().forEach(s -> {
        Slot toSlot = inventoryToCopyTo.query(QueryOperationTypes.INVENTORY_PROPERTY.of(s.getInventoryProperty(SlotIndex.class).get()));
        s.peek().ifPresent(i -> toSlot.set(i));
    });

Same as you described

1 Like

Oh? That looks easy… :thinking: How come I didn’t think of it… Thanks for your help!

Like you said you didnt know how to use QueryOperationType and I probably confused you even more thinking that InventoryArchetype was classed as a property.

That could’ve been that :sweat_smile: Thanks! :heart:

1 Like

Just a quick question, still kinda regarding this topic…

I have this before the inventory copy, but the item still stays in the inventory…

Optional<Slot> opSlot = e.getSlot();
if (!opSlot.isPresent()) {
    e.setCancelled(true);
    return;
}
Slot slot = opSlot.get();
slot.set(ItemStackSnapshot.NONE.createStack()); //clears the slot

Do I have to add that slot to it’s parent inventory?
Or how does this work anyways?

Just tried it myself and couldnt get it to work either. I managed to get it to remove the item if it was placed using

event.getTransactions().stream().forEach(st -> st.setCustom(ItemStack.empty()));

I even tried getting the same slot in the original inventory and modifing the slot there, but that didnt work. So im not sure whats going wrong, but thats how you would normally remove items from a slot.

P.S I also tried changing the CursorTransaction but that didnt work.

As you are coping the contents of the inventory and thats your main consern, you can still remove the item in the copied inventory after the copy and that in effect will stop it staying in the copied inventory.

I tried copying it over, ignoring the item that was in the transaction, and having only that item, nothing copied over, but item still stayed in, even tho I cleared and “wasn’t copied” over.

Can i see code?

Sure, I’ll DM you the updated code!

No the Archetypes have a default which is picked if you do not specify it.

Also to clear a Slot just use slot.clear() (Slots are just Inventory with size 1)

1 Like

Can not believe i didnt think of slot.clear() to clear the slot XD

Wow… Thanks for the helpful insight :stuck_out_tongue: