Cuberite Forum
Saving per-player data - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: Saving per-player data (/thread-2377.html)



Saving per-player data - Schwertspize - 02-18-2016

Hello, my first question here. 

How can I set and get random per-player (cPlayer) data. I want to use it eg to store current effects of a player (when overriding them) or a position or something. A bit like just a random space to save data tied to a player

Maybe there is an obvious solution, but thanks, anyway


RE: Saving per-player data - NiLSPACE - 02-18-2016

Perhaps you can open a player's json file to add/change stuff. Every player has one in the players folder. Since we just got a JSON serializer and deserializer it shouldn't be too hard.


RE: Saving per-player data - LogicParrot - 02-18-2016

(02-18-2016, 07:56 AM)NiLSPACE Wrote: Perhaps you can open a player's json file to add/change stuff. Every player has one in the players folder. Since we just got a JSON serializer and deserializer it shouldn't be too hard.

Rather than editing server files, wouldn't it be cleaner if the plugin has its own json file? Each object in that file would have a player name for identification, and that player's plugin data. This is better because if I ever delete the plugin's folder, it won't leave junk behind. Also, all you have to do for resetting the plugin is deleting the plugin's data files, instead of editing the server's player files.


RE: Saving per-player data - Schwertspize - 02-18-2016

I would prefer writing a separate file for each player, but how to store any data just in memory? (and accessing it, with the corresponding cPlayer object)


RE: Saving per-player data - LogicParrot - 02-18-2016

(02-18-2016, 05:14 PM)Schwertspize Wrote: I would prefer writing a separate file for each player, but how to store any data just in memory? (and accessing it, with the corresponding cPlayer object)

-- Global storage table
PlayerStorage = {}  

PlayerUUID = my_cPlayer_object:GetUUID()

-- Create new player storage, if not created already
if PlayerStorage[PlayerUUID] == nil then
	PlayerStorage[PlayerUUID] = {}
end

-- Save player data
PlayerStorage[PlayerUUID].effect = "whatever";
PlayerStorage[PlayerUUID].etc = 42814

-- Check and read player data
if PlayerStorage[PlayerUUID].effect ~= nil then
	-- Data exists
else
	-- Data does not exist
end

-- Delete specific player data
PlayerStorage[PlayerUUID].effect = nil

-- Delete all player data
PlayerStorage[PlayerUUID] = nil



RE: Saving per-player data - NiLSPACE - 02-18-2016

Another possibility is to use a class to store per-player data. We use it in for example WorldEdit: https://github.com/cuberite/WorldEdit/blob/master/Classes/PlayerState.lua


RE: Saving per-player data - xoft - 02-19-2016

Rather than addressing by playername, you should address by UUID, other than that, this is most likely the best approach so far. Also, it allows you to serialize the data to a DB when you decide to make it permanent at a later point in the development.


Updated to UUID - LogicParrot - 02-19-2016

@xoft I updated this accordingly.


RE: Saving per-player data - Schwertspize - 02-20-2016

Zzzzzz playsrName = GetUUID .... But thanks, I will use it


RE: Saving per-player data - LogicParrot - 02-20-2016

And that's how bugs are bornTongue
Thanks. Updated.