Appropriate way of saving player's inventory in database
Hello, I'm looking for an efficient way as well as other people perspective about saving player's inventories.
I'm working on creating an RPG SMP server, and I'm working on my OWN plugins for the RPG, and lately i found out a concerning problem about players inventories, I know there some ways of saving them i have tried using the base64 format of saving but found out that armor was not saving appropriate, And right now I'm Using JSON for saving player inventory in the database and save the inventory, armor and offhand in separate columns and after some headache about the metadata not working correctly I finally made it work.
Now my concern is that is JSON the most optimal ? My main idea was using JSON so it's gonna be easier for me to Fetch people items and UPDATE/BUFF/NERF them much easier than having the Inventory class encoded in base64 and saving it in the database it like that.
Could someone who worked on stuff like that give me a path or a tip about it ? Mainly if i should keep json and how could i make Updating/nerfing/buffing items more easily for all the player's
38 Replies
Thanks for asking your question!
Make sure to provide as much helpful information as possible such as logs/what you tried and what your exact issue is
Make sure to mark solved when issue is solved!!!
/close
!close
!solved
!answered
Requested by anonim_c#0
Try looking into MySQL Inventory Bridge. It saved inv to a database on top of the regular .dat files. Looking at this plugin may help you with saving the items how you would like them.
https://www.spigotmc.org/resources/mysql-player-data-bridge.8117/
(A bit outdated but it may help you out)
The thing is that if they're going to be switching around servers constantly you can't afford to run something like MySQL
You'd also need to update the rows every single time the player's inventory is updated
Saving the items is the easy part, you can just save it as item slot, item (minecraft:
xxxx
) and then SNBT (which is just json)
The harder part would be efficiency and a proper database
Just do this, it'll work fine
If they'll be switching around constantly you will 100% need some sort of cache layer in frontOne of the bigger issues imo with saving inventories to a database is that when you transfer to another server on bungee/velocity, you actually log into the other server before the current server is even aware that the client will log out
That's not a problem. If he's doing an RPG server he'll definitely need some sort of pub-sub implementation, this is easily solvable
The choosing a proper database and making it not explode during pressure is the hard part
Realistically though, if you just want to be able to rebalance items, just give the items a tag with the item’s persistent data container to identify what item it is, then update the item’s metadata on next login
That's actually a good point, checking item meta on login and updating it for every item that has it and logs in the server
What even is the problem you're trying to solve here?
I thought you wanted to sync inventories cross server?
I mean this can be avoid by having some checks between server i think
Also, if you save the player’s player.dat, you can just store it as a blob/bytea in the database. That would be the most “efficient” way
Well, that contains more than the inventory
Coordinates and potion effects etc. pretty sure
Not exactly a problem to solve more like a question about how to do some things, i basically wanted to learn a better way other than json and base64 to save inventory as well as a way to be able to update players items in case of a rebalance
Just, literally:
Item slot (every slot in the inventory has an assigned slot), item type, and then item SNBT which is just json
You can compress it too, if you feel like it
Coordinates can be solved by teleporting them on login. Potion effects presumably should persist
Yeah, I'm just saying it'd be hard to handle all the edge cases
It also means statistics and advancements persist too
That's a fair point
Is there any other library for serialization that you guys know ?
Beside the base64 method
nbt doesn’t have a “defined” structure, so you can’t properly map all the data in sql tables
Where are you getting base64 from?
Well, there's SNBT, which again is just JSON
You can store it as JSON and deserialize it
I mean there is a github code about base64 serialization
That shouldn't be a problem
for minecraft inventories
That’s not properly storing the data in an sql table
Well, to be honest, so is taking the raw bytes of the player.dat and slapping that into the database
For MySQL, you can’t find all items that have like sharpness 2
Making it "proper" would require so much work
If you used a database that supports documents, then you’d be able to do queries like that (mongodb/postgresql)
I'm not saying it's perfect, but it's really easy to do
Doing it "perfect"ly is really hard
Mongo would absolutely implode under the stress of a few thousands players
But yeah, if you want to not have compatibility issues, then just storing the player.dat directly would ensure that item meta is stored correctly and efficiently
I mean to make database not explode could help make something like redis or memory and save stuff to database once in a while
instead of saving on every inventory update of every player
Mongo and Postgres both support storing arbitrary json documents and also allows qurying them
You could only save on quit, probably
You’d just have to watch out for proxy transfers
^ yeah
the worst one tbh
its abesloutely awful
compared to husksync etc
i havent tried husksync
lemme test it out
post closed!
The post/thread has been closed!
Requested by anonim_c#0