Jump to content

Accessing Capabilities on Players


hammy3502

Recommended Posts

Hello!

I'm trying to create a capability that reads a player's CompoundTag, and extracts some custom mod data from it to assemble an object, along with being given one of those mod objects and writing it to a Player's nbt. I've hopefully created the capability correctly, but I'm not sure how to go about having a `Player` read/write this data during a world save, and how I can go about retrieving the data from a given `Player` object on world join (I know I'd need to subscribe to the event, but I have no idea what I'd do from there). Additionally, this code results in "Invalid player data" whenever I join a world, and I don't know what's in here causes that (EDIT: Figured out this was due to my own bad serialization code. Does this mean that the NBT is automatically loaded on world join and saved on world leave?). My full repository can be found here: https://github.com/hammy3502/elementalism , and the relevant code snippets are below:

 

Object with Custom Mod Data (PlayerProgressionData.java):

public class PlayerProgressionData implements INBTSerializable<CompoundTag> {

    public static final String outerTag = "elementalism_data";

    public ProgressionInfo.ElementType type;
    public ProgressionInfo.ElementType type2;
    public List<ProgressionInfo> powersUnlocked;

    public PlayerProgressionData() {
        this.type = ProgressionInfo.ElementType.NONE;
        this.type2 = ProgressionInfo.ElementType.NONE;
        this.powersUnlocked = new ArrayList<>();
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag toRet = new CompoundTag();
        CompoundTag data = new CompoundTag();

        StringBuilder powersList = new StringBuilder();
        for (int i = 0; i < powersUnlocked.size(); i++) {
            powersList.append(powersUnlocked.get(i).id);
            if (i != powersUnlocked.size() - 1) {
                powersList.append(",");
            }
        }
        data.putString("unlockedPowers", powersList.toString());

        data.putString("element1", this.type.id);
        data.putString("element2", this.type2.id);

        toRet.put(outerTag, data);
        return toRet;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        if (!nbt.contains(outerTag)) return;
        this.powersUnlocked.clear();
        CompoundTag data = nbt.getCompound(outerTag);
        this.type = ProgressionInfo.ElementType.getElementById(data.getString("element1"));
        this.type2 = ProgressionInfo.ElementType.getElementById(data.getString("element2"));

        String[] powerIds = data.getString("unlockedPowers").split(",");
        for (String id : powerIds) {
            ProgressionInfo info = Objects.requireNonNull(ProgressionInfoInit.ALL_PROGRESSION_INFOS.get(id));
            this.powersUnlocked.add(info);
        }


    }
}


Capability Registration (Elementalism.java):

public void registerCapabilities(RegisterCapabilitiesEvent event) {
	event.register(PlayerProgressionData.class);
}

 

Capability Attaching (CommonSubscriber.java):

@SubscribeEvent
    public void attachCapabilities(AttachCapabilitiesEvent<Entity> event) {
        if (!(event.getObject() instanceof Player)) return;

        PlayerProgressionData progressionData = new PlayerProgressionData();
        LazyOptional<PlayerProgressionData> optionalProgressionData = LazyOptional.of(() -> progressionData);
        ICapabilityProvider provider = new ICapabilitySerializable<CompoundTag>() {

            @Override
            public CompoundTag serializeNBT() {
                return progressionData.serializeNBT();
            }

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

            @Nonnull
            @Override
            public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
                if (cap == DATA_STORAGE_CAPABILITY) {
                    return optionalProgressionData.cast();
                }
                return LazyOptional.empty();
            }
        };

        event.addCapability(new ResourceLocation(Elementalism.MOD_ID, "data_storage"), provider);
        event.addListener(optionalProgressionData::invalidate);

    }

 

Thank you so much for any help!

Edited by hammy3502
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.