I have implemented custom data, and assigned it to a player. I can successfully retrieve the data, but when the player disconnects and reconnects, the player loses that data. Is this supposed to happen?
Unable to reproduce in single player dev environment. I’ve successfully stored and retrieved custom data that’s not only persisted across restarts, but also the random player name changing that happens in dev.
Can you post your code / Versions tested on / Testing methodology?
When you turn a dataholder such as an entity into a container, it puts the custom (plugin-made) data into a list of sub containers so it doesn’t interfere with vanilla data. You can see that here.
So when you try to get your value from that DataContainer - it’s simply not there.
Instead, you’ll want to get your JobData from the entity directly, just like you would normally.
You can then merge that with merge(copy(), oldData) - the default behaviour is always to use oldData if it exists, otherwise to use the current data (the copy).
Your JobInfo isn’t DataSerializable and you’re not even calling container.getSerializable(Keys.JOB.getQuery(), JobInfo.class) to deserialize your JobInfo instance. DataViews don’t magically deserialize a Map into an Object instance unless you tell it to.
I’ve updated my project to reflect the FakeData example that gabizou provided. However, when I try to offer the player data, it fails. (player.offer(data).isSuccessful() returns false and the data can not be read)
If you copied FakeData exactly you have also copied it’s errors. createValue as I commented, has it’s element and default parameters backwards when it’s being called.
I have some exciting news! I created a test plugin that offers and retrieves that data. Offering the data returns true , but retrieving the data doesn’t work
I made everything into a mini plugin, so that it is easier to work with. The code can be found here.
So first thing - if you are not extending AbstractSingleData you must call registerGettersAndSetters() once you’ve finished initialising any values. Without this, you are unable to use any of the Key-based methods in the data API.
Secondly, I copied/pasted just your data code into a clean workspace, and was able to successfully set and get the JobData object. Can you please tell us the full version string (ie jar name) of Sponge and Forge - AFAIK SpongeForge is only up to version #1045. Also tell us what version of SpongeAPI you’re using in your IDE.
Thank you so much! Everything works perfectly! The data persists through player reconnections and server restarts. All I had to do was call the registerGettersAndSetters() method!
For anyone dropping by in the future, here is the working plugin.