[{"data":1,"prerenderedAt":157},["ShallowReactive",2],{"project--18-07-2024-dev-blog-3-build-a-skyblock-plugin":3},{"id":4,"title":5,"body":6,"description":139,"extension":140,"meta":141,"navigation":152,"path":153,"seo":154,"stem":155,"__hash__":156},"blog\u002Fblog\u002F18-07-2024-dev-blog-3-build-a-skyblock-plugin.md","Build a Skyblock Plugin (part 3): Inventory Sync System",{"type":7,"value":8,"toc":130},"minimark",[9,14,18,23,42,46,49,52,55,58,64,72,77,88,92,95,98,101,104,107,111,114,117,120,124,127],[10,11,13],"h1",{"id":12},"dev-blog-3-build-a-skyblock-plugin-part-3-inventory-sync-system","Dev Blog #3: Build a Skyblock Plugin (part 3): Inventory Sync System",[15,16,17],"p",{},"In the last article, we talked about the island system. In this article, we will create the inventory sync system that\nwill permit to synchronize the inventory of the player between the servers.",[19,20,22],"h2",{"id":21},"considerations-requirements","Considerations \u002F Requirements",[24,25,26,30,33,36,39],"ul",{},[27,28,29],"li",{},"Should be fast, resilient and reliable",[27,31,32],{},"Optimized for performance",[27,34,35],{},"Should be able to handle a large number of players",[27,37,38],{},"Should be compatible with ItemAdder items",[27,40,41],{},"The inventories should be stored on mysql for persistent storage and on redis for caching",[19,43,45],{"id":44},"inventory-sync-system","Inventory Sync System",[15,47,48],{},"There are many ways to synchronize the inventory of the player between the servers. The most common way is to use a\nclass utility that will handle the convert the item into json and store it in the database.",[15,50,51],{},"I could also use the ItemStack.serialize() method that will convert the item into a json map and store it in the\ndatabase.",[15,53,54],{},"All of that is not compatible with item adder items. Item adder items are items that have a custom model data and a\ncustom texture. They have a special thing inside that can't be serialized easily.",[15,56,57],{},"So I decided to go for a Base64 serialization. This will permit to store the item in a string format that can be\nconverted back to an item. This is compatible with item adder items.",[15,59,60],{},[61,62,63],"strong",{},"Disadvantages:",[24,65,66,69],{},[27,67,68],{},"Generate huge strings",[27,70,71],{},"Not human readable",[15,73,74],{},[61,75,76],{},"Advantages:",[24,78,79,82,85],{},[27,80,81],{},"Compatible with item adder items",[27,83,84],{},"Fast to serialize and deserialize",[27,86,87],{},"No need to make a utility class with 500 if or getOrDefault",[19,89,91],{"id":90},"performance","Performance",[15,93,94],{},"To improve the performance, I will use a cache system: Redis for now with Jedis client.",[15,96,97],{},"When a player joins the server, the plugin will check if the player has an inventory in the cache. If not, it will\nload the inventory from the database and store it in the cache.",[15,99,100],{},"On join, the thing that takes the most time is going back to the main server thread to ensure thread safety.\nI tried full async, it was working but unsure about the thread safety. Nothing on the documentation about that.",[15,102,103],{},"Just in case, I go back to the main thread to apply inventory changes.\nThis causes between 10ms to 100ms of delay before the inventory is loaded.",[15,105,106],{},"How fast can it load an inventory? I did with a full inventory of random items and it took between 15ms to 50ms.\nThis is acceptable for me. I don't want to go below 10ms because it's not necessary and would lead to more complex\ncode.",[19,108,110],{"id":109},"experience-health-food-and-more","Experience, health, food and more",[15,112,113],{},"I decided to store everything in a json for the cache and in a table for the database.",[15,115,116],{},"To get experience (total experience the player has), I will use the calculateTotalExperiencePoints() method.\nFor the health and food, I will use the getHealth(), getFoodLevel(), getSaturation() methods.",[15,118,119],{},"I also have some custom stats that could be added in the future like current mana left, etc.",[19,121,123],{"id":122},"conclusion","Conclusion",[15,125,126],{},"We talked about the inventory sync system.\nIf you're interested in the code, I can make a more detailed article about it, just send me a\nmessage on discord (Farmeurimmo), via the contact form or by email.",[15,128,129],{},"Next time, I will talk about the Cross Server auction system.",{"title":131,"searchDepth":132,"depth":132,"links":133},"",2,[134,135,136,137,138],{"id":21,"depth":132,"text":22},{"id":44,"depth":132,"text":45},{"id":90,"depth":132,"text":91},{"id":109,"depth":132,"text":110},{"id":122,"depth":132,"text":123},"This article explores the Skyblock plugin's inventory synchronization system, detailing how player inventories are managed across servers.","md",{"image":142,"date":143,"tags":144,"author":149},"https:\u002F\u002Fcdn.farmeurimmo.fr\u002Fimg\u002Fblog\u002F18-07-2024-dev-blog-3-build-a-skyblock-plugin.jpeg","2024-07-18T11:00:00.000Z",[145,146,147,148],"Minecraft","Spigot","Plugin Development","Skyblock",{"name":150,"picture":151},"Farmeurimmo","https:\u002F\u002Fcdn.farmeurimmo.fr\u002Fimg\u002Flogo.png",true,"\u002Fblog\u002F18-07-2024-dev-blog-3-build-a-skyblock-plugin",{"title":5,"description":139},"blog\u002F18-07-2024-dev-blog-3-build-a-skyblock-plugin","1b4fO6ezVpGfGszdmsZNrYEvxLup5-oBX2OI4PLPUEY",1794558958090]