Jump to content

[SOLVED][1.15.2] ItemStack tag not persisting after update during `itemInteractionForEntity` method


Recommended Posts

Posted

Hi there,

 

I have an item, that on clicked on an entity (called in itemInteractionForEntity), it would attempt to add some data to the stacks tag. When debugging, I can see that the method is called and the stack nbt is updated, but it seems like it's not persisted anywhere outside of that method call. Subsequent calls to that method show that the stack tag is empty. I've done some fiddling around, but I'm really not sure what's going on.

 

Here's my code for the method call:

 

    public boolean itemInteractionForEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity target, Hand hand) {
        if (target.world.isRemote || target instanceof PlayerEntity || hand != Hand.MAIN_HAND) {
            return false;
        }

        CompoundNBT nbt = stack.getOrCreateChildTag(VOID_POCKET_KEY);

        if (nbt.contains(HELD_ENTITY_KEY) || nbt.contains(HELD_INVENTORY_KEY)) {
            return false;
        }

        if (nbt.contains(LAST_INTERACT_TIME_KEY) && nbt.getLong(LAST_INTERACT_TIME_KEY) - target.world.getGameTime() <= MIN_INTERACT_TIME) {
            return false;
        }

        CompoundNBT entityNbt = new CompoundNBT();
        target.writeUnlessRemoved(entityNbt);

        nbt.put(HELD_ENTITY_KEY, entityNbt);
        nbt.putString(HELD_ENTITY_NAME_KEY, target.getDisplayName().getFormattedText());
        nbt.remove(LAST_INTERACT_TIME_KEY);
        nbt.putLong(LAST_INTERACT_TIME_KEY, target.world.getGameTime());

        stack.setTagInfo(VOID_POCKET_KEY, nbt);

        target.remove();

        return true;
    }

 

Is there some additional thing I need to do to ensure my code is synced? Or do I need to do something with the player?

 

Looking at some vanilla examples I can see that name tags and dyes don't do server checks, while shears do. None of them update the itemstack tag, but they do either damage the itemstack or reduce count.

No signature for you!

Posted

Alright worked it out. I'm pretty dumb. If you're in creative mode the item that gets used is a copy of the original item (to prevent durability loss and the like I suppose). In survival it works fine. So I guess I just need to replace the actual item when in creative.

No signature for you!

  • DARKHAWX changed the title to [SOLVED][1.15.2] ItemStack tag not persisting after update during `itemInteractionForEntity` method

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.