Having separate inventories for each world

Discussion in 'Plugin Development' started by Shadow_tingBRO, Dec 2, 2019.

Thread Status:
Not open for further replies.
  1. Offline


    I was just wondering if there was anyway you could have seperate player inventories per world. I have looked at plugins like Multiverse-inventories and looked at some of the code but its API makes it hard to find out how. What would be the most effective way to do this? I could map a player to a map of worlds and to a inventory but the problem is how would I save the inventories after the server shutsdown, serializing an inventory isnt the best idea
  2. Offline


    Serializing an inventory is not a bad idea, save them to a config. The only issue then would be that it's very much tamper prone. Any incorrect adjustment could reset everyones inventories.

    A solution to that problem is to have a per account custom config, with the name of the config of course being their UUID. Then save their inventories to that config.

    What is the issue with serializing an inventory?
  3. Offline


    Thats a really good idea I am going to do that thanks! Once done, ill post some of the code for people wondering how I achieved this.

    The problem would be having to save all the itemstacks to a config but It shouldnt be too hard.
  4. Offline


    You can write a serialization method that takes in an inventory, and outputs the serialization of that inventory.

    Hope it all goes well.
  5. Offline


    Bukkit provides a method to store an ItemStack in a config, right? When using that, the serializing should become much easier.
  6. Offline


    @knokko I already have a method for serializing an ItemStack, serialzing an Inventory shouldnt be hard, I just need to record all the item stacks and their corresponding slots.

    @CraftCreeper6 Do I use getContents or getStorageContents? Also, when looping though all of the inventory items, are the indexes in the ItemStack array returned from getting the contents of the inventory corresponding to the slot in the player's inventory? I dont know how I would record the slots and also about the armor slots and inventory slots and hotbar slots and which ones each one of them are for.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Dec 2, 2019
  7. Offline



    getContents should be fine.
    1. Use a for i loop, from i = 0 to i <= the size of contents
    2. Index the return value of getContents at i
    3. Ensure the returned stack is valid
    4. Serialize the returned item stack and the i (the slot that it is in)
    5. Save to config
  8. Offline


    Thats what I thought, Ill give it a go now!
    CraftCreeper6 likes this.
  9. Offline


    @Shadow_tingBRO the armor contents and the off hand are not included in the inventory contents. You will have to save it one by one
  10. Offline


    @Shadow_tingBRO Remember to save the empty slots as well, otherwise the inventory won't be the same.
  11. Offline


    @KarimAKL good point ill do that. One more thing, lets say the player has a banner or some other item in their inventory, I will have to check if its a banner then serialize it and deserialize it different because of the bannermeta etc. So wouldnt I have to do that for every item and check if its a banner of a special item
  12. Offline


    Yeah just check if it's a banner and store banner meta too.
  13. Offline


    Ok, but are there other items like banners which have different meta's?
  14. Offline


    I think the most important thing would be shulker boxes, when you have one of them it may be the case that you have to serialize an inventory within that inventory which could be complicated. I've not actually looked into how shulker boxes save their items, but I imagine it's just another inventory.

    Either way, just make sure you check for everything, enchantments, other inventories, banners, durability etc... it may take some time but it could be worth it.
  15. Offline


    There must be an easier way than this, did multiverse inventories do all of this?
  16. Offline


    Last edited: Dec 3, 2019
Thread Status:
Not open for further replies.

Share This Page