Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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!

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.