Paper
Paper copied to clipboard
Add offline PDC API
Closes https://github.com/PaperMC/Paper/issues/7288
Old description
Plugin can now gets the offline persistent data of a player. ~The current behavior is like the Machine's Advancement PR: All setters re-save the data directly into the disk~ Note also that if spigot config disablePlayerDataSaving is enabled, the data will not be writtenPlugin can now gets the offline persistent data of a player with a read-only view.
why wouldn't this just be setPersistentDataContainer and getPersis... like on itemMeta, that way you can write all your data before writing back to the file?
In fact it's not just for this all methods in this class just save after write operation (see statistics)
This is inheriently unsafe imho, and really needs to be backed by a proper async player data system (and proper flow control for plugin devs to be able to safely mess with this stuff without screwing over other data consumers)
That's right, i see your point https://github.com/PaperMC/Paper/pull/5529#issuecomment-830155819, do you prefer that i make this system immutable waiting your async/thread safe implementation. Because this PR was just covered this feature so rewriting this part is out of this PR. Also i agree with the autocloseable part.
I have fixed a bug when player data was already created using the offline PDC API and the that same player join for its first time (Thanks LoaiDev). Also the interface is now AutoCloseable (no need to resave after each write op) but sadly need a cast due to how Player and OfflinePlayer are linked
OfflinePlayer p = Bukkit.getOfflinePlayer("Lulu13022002");
try(OfflinePersistentDataContainer pdc = (OfflinePersistentDataContainer) p.getPersistentDataContainer()) {
pdc.set(KEY, PersistentDataType.INTEGER, 1);
}
I can't really see good usage of supporting offline PDC for player that has never join the server before. So if LoaiDev or anyone see a good use case then i will keep otherwise i will just drop it and document + ignore the data.
I'd probably be in favour of this 👍 If this feature is required later on, more thought can be put into it but it is just way to ugly to hack in there.
Question is how we want to indicate whether or not the offline player has played before. Do we wanna throw an exception when closing the PDC ? Or just log a WARN into console ? Just quietly failing might be unfortunate.
For now i have throw an exception when the pdc is closed because this can cause data loss and possible NPE in future. And these things are not tiny bug and shouldn't be ignored.