Inventory to Serializable

Hey, I send data between servers (linked together with BungeeCord) using sockets. So I use ObjectInputStreams and ObjectOutputStreams. That means that every object which will be sent must implement Serializable.

Now I want to send an inventory. How can I convert it to Serializable or to a String (which also implements Serializable)?


So yes and no. There is no way to convert a inventory directly to a String. However you can convert all items to ItemStackSnapshots and then that to String.

You could probably convert all the items to strings and then have a character divider between all items and then also include the slot index next to the serialised item from there you could construct the inventory back on the other server.

Im not sure how to directly convert items to strings, but i know it is possible thanks to TypeTokens.ITEMSTACK_SNAPSHOT that is used to convert itemstack snapshots to strings in config use

I saw that I can serialize it to a JSON String with ItemStack#toContainer() and StringDataFormater#write(DataContainer). But is that really the best way?

Probably. Due to how Sponge works, most game objects can’t be Serializable directly - if they were, objects would easily break when moving them between different servers. Using a DataFormat is probably the best option.

If you’re worried about the size of the data, you could use DataFormats.NBT instead, which makes the saved data a bit smaller. However, that would probably require more code than DataFormats.JSON.

1 Like

Alright. Thanks for the help.