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.

Featured Replies

Posted

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

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.