How do you serialize a player's data?

How do you serialize a player’s data into a SQL database? In Bukkit I had to convert all of the data in JSON format as a string and saved it. Do I have to get all of the player’s data and item’s data individually and save it or does Sponge have a better alternative?

What kind of data are you trying to store remotely?

Everything about a player including it’s inventory, it’s equipment, it’s fireticks, it’s location, it’s active potion effects, etc. The only thing I really need to serialize is the player’s inventory including the equipment.

Not to imply that you shouldn’t, but could I ask in what scenario you need to store all this? Surely there’s a better way?

Multiple character system per player. It’s for a RPG server I am working on. I already made it work on Spigot 1.8

multiple character system

1 Like

I know some of the main goals of sponge is to make everything storable. A player inherits from What has to method toContainer(). I am thinking thats the method your looking for. But I could be mistaken!


Thanks again Thomas. I will be sure to check it out.

Is there a way I can serialize the whole inventory data on a String?

I’m also extremely interested in this, but would really prefer to be able to assign offline UUID’s to authenticated players.

Having all plugins treat the player as a completely new player would be amazing, allowing us to finally separate out worlds as universes per player, and have ModMode where staff can play the game and be left alone, and swap out into a moderator role when they need to without having a second minecraft account.

Without the UUID changing it would also support restoring player data over multiple servers by serializing it out to a third party service, allowing adventure style servers to load balance easier.

1 Like

Exactly! I also don’t want to create multiple keys on the SQL just to save each and every data. I would prefer to store a whole inventory in a string instead. Even if Sponge doesn’t have that functionality, it can still be possible using JSON but it’s pretty time consuming making Data Storage system serializing an entire player’s inventory including equipment into a string(item meta, item lore, enchantments, skulls, books, other nbt data).

It really isn’t time consuming if all you need is inventory out as a string. write one DataTranslator and it will be able to handle anything the DataAPI can throw at it including inventories. When the DataAPI is finished it will be possible to serialize all the modifyable data that a player holds.

To be honest, you should be able to just use Player#toContainer() which will give you all known data about the Player in a lovely DataContainer.

Sorry, misread. So what it sounds like is you want to be able to serialize a Player to a specific format, of which I present the DataSource. DataSource is an interface to represent a literal data source for storing and retrieving data. It is up to you how to actually implement it, all you need to do is contractually be able to store an appropriate DataContainer and retrieve it in almost an exact same state as one you sent.


Can I convert it to an array to convert it to JSON array?

new GsonBuilder().create().toJson(event.getPlayer().toContainer().getValues(true));

Like this :smile:. But sadly enough sponge doesn’t have much information in the players container yet. I tried it and the only thing it gives is this:


What is also is implemented like that:


I’m not entirely sure that would work with nested DataContainers containing internal views. Let alone rebuilding a DataContainer from Gson would likely fail. If you wanted to, you could provide some JsonDataTranslator that translates a DataView/Container to and from Json strings much like how the ConfigurateTranslator or NbtTranslator works.


Thomas bud, you are such a help. That gives me ideas as well as what gabizou said. I will try it out and see if I can use JsonDataTranslator to translate DataContainer to a string. This data system is pretty powerful and is going to help a lot in serialization which was a pain on the butt with Bukkit(took like a week to just make a system to save a whole player inventory on a string). Thanks again guys.

Why not add the yml and gson ConfigurationLoaders from that library? Looks like ConfigurateTranslator can be used for other stuff than hocon tho?

Would it be possible to use the DataContainer to set a player’s data on join? for example can you use Player#toContainer() to store the data, and then something like Player#load(container) or whatever…

You would have to use the SerializationService.getBuilder(Player.class).get().build(DataContainer). The only problem is that this will not really work 100% as there has to be a connected player instance to rebuild a player. If you want to create a User instance, use the User builder.

So it’s possible to create a Player instance, and then set that to the User?