Jump to content

[1.16.5] Problem with Item NBT


Triphion

Recommended Posts

I have created an item which ticks when it is in a player inventory. It has a timer that applies a potion effect to the player whenever a certain time has been exceeded. And so it loops. 

This item has 2 important variables, a timer, as well as a boolean which is used to determine the item that should be ticking whenever a player has more than one instance in the inventory. I determine this value everytime a player drops or picks this item up.

The problem now is that the data doesn't seem to be saving. Any help would be very much appreciated.

    @Override
    public void inventoryTick(ItemStack itemStack, World world, Entity entity, int p_77663_4_, boolean p_77663_5_)
    {
        super.inventoryTick(itemStack, world, entity, p_77663_4_, p_77663_5_);

        if (entity instanceof ServerPlayerEntity)
        {
            readItemStackInfo(itemStack);

            if (GetIsActiveStack(itemStack)){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) entity;
                incrementCurrentTime(itemStack);

                if (this.currentTime >= deterInterval + random.nextInt(50))
                {
                    resetCurrentTime(itemStack);
                    int deterValue = random.nextInt(5) + 1;

                    addPositivePotionEffect(positiveEffects, serverPlayer);

                    itemStack.hurt(deterValue, random, serverPlayer);

                    if (itemStack.getMaxDamage() - itemStack.getDamageValue() <= 0){
                        SetIsActiveStack(itemStack, false);

                        SetNewActiveStack(itemStack, serverPlayer);
                    }
                }
            }
        }
    }
    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        readItemStackInfo(itemStack);

        return this.isActiveStack;
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        this.isActiveStack = active;
        writeItemStackInfo(itemStack);
    }

    public void readItemStackInfo(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getTag();

        if (nbt == null){
            writeItemStackInfo(itemStack);
        }

        this.isActiveStack = nbt.getBoolean("active");
    }

    public CompoundNBT writeItemStackInfo(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("active", this.isActiveStack);
        itemStack.setTag(nbt);

        return nbt;
    }

 

Link to comment
Share on other sites

3 hours ago, diesieben07 said:

You cannot store data in your Item class like that. There is only one instance of your item class, as such you cannot store stack-specific data as instance fields.

Ah, right. So like this?

    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            SetIsActiveStack(itemStack, false);
        }

        return nbt.getBoolean("active");
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("active", active);
        itemStack.setTag(nbt);
    }

 

3 hours ago, diesieben07 said:

Additionally you should really avoid modifying the stack NBT every single tick. Instead of counting down a value in the stack, you can store a target "game time" (World#getGameTime) and then check if it has been reached. This was you do not need to modify the NBT.

Ahh, that is a very solid suggestion!

    private int getNextPotionTime(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            setNextPotionTime(itemStack, (int) itemStack.getEntityRepresentation().level.getGameTime() + deterInterval + random.nextInt(50));
        }

        return nbt.getInt("time");
    }

    private void setNextPotionTime(ItemStack itemStack, int time){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putInt("time", time);
        itemStack.setTag(nbt);
    }

This still doesn't seem to work. Through my logging, all the values are as default and doesn't seem to get saved.

Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

Yes, except you need to realize that the ItemStack tag is global ground. You should store your data either prefixed with your ModID or store it in a sub-compound named after your ModID.

So, like this?

    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            SetIsActiveStack(itemStack, false);
        }

        return nbt.getBoolean("tnw.active");
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("tnw.active", active);
        itemStack.setTag(nbt);
    }

I log this every tick, only for testing this specific occurence, this will be removed promptly after I have this working. 

            LogManager.getLogger().info(GetIsActiveStack(itemStack));
            LogManager.getLogger().info(getNextPotionTime(itemStack));

They both log false and 0 respectively.

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

Why is your ModID "tnw"? That is way too short, it should be something unique. Do not use abbrevations.

The ModID is not permanent in this scenario. I don't yet have a good name for the mod, so tnw is just temporary. 

    @Override
    public void inventoryTick(ItemStack itemStack, World world, Entity entity, int p_77663_4_, boolean p_77663_5_)
    {
        super.inventoryTick(itemStack, world, entity, p_77663_4_, p_77663_5_);

        if (entity instanceof ServerPlayerEntity)
        {
            LogManager.getLogger().info(GetIsActiveStack(itemStack));
            LogManager.getLogger().info(getNextPotionTime(itemStack));

            if (GetIsActiveStack(itemStack)){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) entity;

                if (hasTimeBeenReached(itemStack))
                {
                    setNextPotionTime(itemStack, (int) (world.getGameTime() + deterInterval + random.nextInt(50)));
                    int deterValue = random.nextInt(5) + 1;

                    addPositivePotionEffect(positiveEffects, serverPlayer);

                    itemStack.hurt(deterValue, random, serverPlayer);

                    if (itemStack.getMaxDamage() - itemStack.getDamageValue() <= 0){
                        SetIsActiveStack(itemStack, false);

                        SetNewActiveStack(itemStack, serverPlayer);
                    }
                }
            }
        }
    }
    private boolean hasTimeBeenReached(ItemStack itemStack){
        if (itemStack.getEntityRepresentation().level.getGameTime() >= getNextPotionTime(itemStack)){
            LogManager.getLogger().info(getNextPotionTime(itemStack));
            return true;
        }
        return false;
    }

 

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

Where do you set the stack as active?

In ItemPickupEvent.

    @SubscribeEvent
    public static void onItemPickup(PlayerEvent.ItemPickupEvent event){
        if (event.getPlayer() instanceof ServerPlayerEntity){
            if (event.getStack().getItem().getItem() == TnwItems.WISP_BOTTLE.get()){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) event.getPlayer();
                WispBottleItem item = (WispBottleItem) event.getStack().getItem();

                boolean flag = false;

                Iterator<ItemStack> iterator = WispBottleItem.getWispsOnPlayer(serverPlayer).iterator();

                for (Iterator<ItemStack> it = iterator; it.hasNext(); ) {
                    ItemStack stack = it.next();

                    if (((WispBottleItem)stack.getItem()).GetIsActiveStack(stack)){
                        flag = true;
                    }
                }

                if (!flag){
                    item.SetIsActiveStack(event.getStack(), true);
                    LogManager.getLogger().info("Picked up!" + item.GetIsActiveStack(event.getStack()));
                }
            }
        }
    }

The logger at the end always reports as true since the value never saves.

Edited by Triphion
Link to comment
Share on other sites

17 minutes ago, diesieben07 said:

ItemPickupEvent fires after the stack has already been picked up, you cannot modify it anymore.

You can use EntityItemPickupEvent, which fires before the stack has been added to the player's inventory and thus you can modify the stack. Note however that if the item is stackable, only parts may be picked up. And also note that EntityItemPickupEvent fires even if the item doesn't actually fit in the player's inventory. Check ItemEntity#playerTouch for details of how this works.

That seems to have done the trick! Thanks!

Link to comment
Share on other sites

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.