Paper icon indicating copy to clipboard operation
Paper copied to clipboard

Add offline PDC API

Open Lulu13022002 opened this issue 3 years ago • 8 comments

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 written

Plugin can now gets the offline persistent data of a player with a read-only view.

Lulu13022002 avatar Jul 09 '22 15:07 Lulu13022002

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?

u9g avatar Jul 09 '22 18:07 u9g

In fact it's not just for this all methods in this class just save after write operation (see statistics)

Lulu13022002 avatar Jul 09 '22 18:07 Lulu13022002

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)

electronicboy avatar Aug 13 '22 12:08 electronicboy

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.

Lulu13022002 avatar Aug 13 '22 14:08 Lulu13022002

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);
}

Lulu13022002 avatar Aug 14 '22 09:08 Lulu13022002

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.

Lulu13022002 avatar Sep 06 '22 14:09 Lulu13022002

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.

lynxplay avatar Sep 06 '22 14:09 lynxplay

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.

Lulu13022002 avatar Sep 11 '22 13:09 Lulu13022002