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
Admincraft Meta
Admincraft Meta4mo ago
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
Butter
Butter4mo ago
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)
Jenkins
Jenkins4mo ago
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 front
Snow Kit
Snow Kit4mo ago
One 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
Jenkins
Jenkins4mo ago
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
Snow Kit
Snow Kit4mo ago
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
Anonim
AnonimOP4mo ago
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
Jenkins
Jenkins4mo ago
What even is the problem you're trying to solve here? I thought you wanted to sync inventories cross server?
Anonim
AnonimOP4mo ago
I mean this can be avoid by having some checks between server i think
Snow Kit
Snow Kit4mo ago
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
Jenkins
Jenkins4mo ago
Well, that contains more than the inventory Coordinates and potion effects etc. pretty sure
Anonim
AnonimOP4mo ago
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
Jenkins
Jenkins4mo ago
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
Snow Kit
Snow Kit4mo ago
Coordinates can be solved by teleporting them on login. Potion effects presumably should persist
Jenkins
Jenkins4mo ago
Yeah, I'm just saying it'd be hard to handle all the edge cases
Snow Kit
Snow Kit4mo ago
It also means statistics and advancements persist too
Jenkins
Jenkins4mo ago
That's a fair point
Anonim
AnonimOP4mo ago
Is there any other library for serialization that you guys know ? Beside the base64 method
Snow Kit
Snow Kit4mo ago
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?
Jenkins
Jenkins4mo ago
Well, there's SNBT, which again is just JSON You can store it as JSON and deserialize it
Anonim
AnonimOP4mo ago
I mean there is a github code about base64 serialization
Jenkins
Jenkins4mo ago
That shouldn't be a problem
Anonim
AnonimOP4mo ago
for minecraft inventories
Snow Kit
Snow Kit4mo ago
That’s not properly storing the data in an sql table
Jenkins
Jenkins4mo ago
Well, to be honest, so is taking the raw bytes of the player.dat and slapping that into the database
Snow Kit
Snow Kit4mo ago
For MySQL, you can’t find all items that have like sharpness 2
Jenkins
Jenkins4mo ago
Making it "proper" would require so much work
Snow Kit
Snow Kit4mo ago
If you used a database that supports documents, then you’d be able to do queries like that (mongodb/postgresql)
Jenkins
Jenkins4mo ago
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
Snow Kit
Snow Kit4mo ago
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
Anonim
AnonimOP4mo ago
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
Snow Kit
Snow Kit4mo ago
Mongo and Postgres both support storing arbitrary json documents and also allows qurying them
Jenkins
Jenkins4mo ago
You could only save on quit, probably
Snow Kit
Snow Kit4mo ago
You’d just have to watch out for proxy transfers
Jenkins
Jenkins4mo ago
^ yeah
ProGamingDk
ProGamingDk4mo ago
the worst one tbh its abesloutely awful compared to husksync etc
Butter
Butter4mo ago
i havent tried husksync lemme test it out
Admincraft Meta
Admincraft Meta4mo ago
post closed!
The post/thread has been closed!
Requested by anonim_c#0

Did you find this page helpful?