Jump to content

[SOLVED] I lose my player's capability data everytime I exit an rejoin a world


Tsuk1

Recommended Posts

Hello!
So, I'm having a problem where I always lose the data when I exit and rejoin a world, I think my problem is with the deserialization and serialization of the NBT, but i'm not sure and i don't know how to fix it.

 

I think my error is somewhere here:

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerGodParentProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {
    public static Capability<PlayerGodParent> PLAYER_GOD_PARENT = CapabilityManager.get(new CapabilityToken<PlayerGodParent>() { });

    private PlayerGodParent godParent = null;
    private final LazyOptional<PlayerGodParent> optional = LazyOptional.of(this::createPlayerGodParent);

    private PlayerGodParent createPlayerGodParent() {
        if(this.godParent == null) {
            this.godParent = new PlayerGodParent();
        }

        return this.godParent;
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        if(cap == PLAYER_GOD_PARENT) {
            return optional.cast();
        }

        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();
        createPlayerGodParent().saveNBTData(nbt);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        createPlayerGodParent().loadNBTData(nbt);
    }
}

 

The whole repo is here: https://github.com/P3terD/myths-mod/tree/main

Edited by Tsuk1
Link to comment
Share on other sites

  • Tsuk1 changed the title to [SOLVED] I lose my player's capability data everytime I exit an rejoin a world

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.



×
×
  • Create New...

Important Information

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