How to Edit the NBT Data of a Minecraft Forge Player

179 Views Asked by At

I am a beginner to Forge Modding and I'm having a problem adding a custom NBT Tag to a Player in an overridden Item.finishUsingItem method.

There seems to be nothing on the Forge API website on NBTs and anything about NBTs that I've found all seems to be outdated so i got to a bit of an impasse

Github here!

My current code in the finishUsingItem method looks like this:

if (pLivingEntity instanceof Player pPlayer)
{
    CompoundTag entityNBT = pPlayer.serializeNBT();
    if (!entityNBT.contains("necromancy_level"))
    {
        NLevelTag levelTag = new NLevelTag(1, 0);
        levelTag.levelUp();
        entityNBT.put("necromancy_level", levelTag);
        // looking for code here to set player's NBT to entityNBT
    } else
    {
        NLevelTag levelTag = (NLevelTag) entityNBT.get("necromancy_level");
        levelTag.levelUp(5);
    }
}

My NLevelTag has two ints:


private int level;
private int levelXP;

public NLevelTag(int _level, int _levelXP)
{
    this.level = _level;
    this.levelXP = _levelXP;
}

and a levelUp method:


public void levelUp()
{
    this.levelXP++;
    LOGGER.debug("XP leveled up to " + this.levelXP);
}

Note: levelUp has a separate overload which takes an int and calls the above method that number of times.

When I load Forge and eat the item I can get the Player NBT data from the item but it never has the field that it should have added.

Is there something I have missed about adding NBTs to entities?

Or can you just not do that?

0

There are 0 best solutions below