Jump to content

[1.15.2] Replacing ItemStack within container after it was taken.


Recommended Posts

Posted

Hi,

 

I would like to ask what is the best way to replace taken stack from container for a new one. I've tried to do it in 2 ways.

 

1. Implement it within removeStackFromSlot like this:

 

    @Override
    public ItemStack removeStackFromSlot(int index) {
        int maxPossibleItemStackSize = contents.getSlotLimit(index);

        ItemStack newStack = new ItemStack(CommonEvents.keysItem);
        CompoundNBT nbt = newStack.getOrCreateTag();
        if(!KeysItem.hasKeysGenerated(nbt)){
            KeysItem.generateKeys(nbt);
        }

        ItemStack takenStack = contents.extractItem(index, maxPossibleItemStackSize, false);
        this.setInventorySlotContents(index, newStack);
        return takenStack;
    }

 

But this doesn't work at all.

 

2. Implement it within markDirty like this:

 

    @Override
    public void markDirty() {
        markDirtyNotification.invoke();

        if(this.getStackInSlot(0) == ItemStack.EMPTY) {
            ItemStack newStack = new ItemStack(CommonEvents.keysItem);
            CompoundNBT nbt = newStack.getOrCreateTag(); 
            if (!KeysItem.hasKeysGenerated(nbt)) { 
                KeysItem.generateKeys(nbt);
            }
            this.setInventorySlotContents(0, newStack);
        }
    }

 

This works, but somehow inconsistently, that even the taken stack with generated NBT properties gets replaced for a new one and that is not exactly what I want. I need to extract exactly the Item Stack that is currently in the container and after it was taken away generate a new one instead. 

 

Thank you in advance

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.