Solved Creating custom inventories per player

Discussion in 'Plugin Development' started by Jesian97, Jul 27, 2015.

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

    Jesian97

    I made a plugin SellShop that allows players to /sell and open up an inventory to put the items they want to sell in, then when they close the inventory it sells the items. The plugin is completely finished except for one bug, the /sell inventory can be seen and modified by multiple players at the same time, meaning nobody could sell safely, as it is a public inventory. I want to know how I can make each inventory private to the specific player
    Possibly changing something here would help?
    Code:
            inv = Bukkit.getServer().createInventory(null,36, DARK_GREEN + "[" + GREEN + "Redemption Market" + DARK_GREEN + "]");
     
  2. Offline

    567legodude

    @Jesian97 Don't use the same inventory object when someone does /sell create a new inventory each time and give that one to them.
     
  3. Offline

    Jesian97

    so when they type /sell it creates a new inventory, how do i implement that into my code?
     
  4. Offline

    cfil360

    I recommend using hashmaps. Have each hashmap store the player uuid and a new inventory.
     
  5. Offline

    Jesian97

    would that create a temporary inventory that disappears after use?
     
  6. Offline

    cfil360

    @Jesian97 no, this inventory is not temporary, but instead it creates an inventory per user. Every time the player attempts to sell again the old inventory will be overwritten by a new one.
     
  7. Offline

    Jesian97

    wont that take up a lot of space?
     
  8. Offline

    cfil360

    @Jesian97 Personally I don't know too much about efficiency of this, but it only stays on the server until the server is reloaded or shutdown. I have never had any space issues using them and as long as you only store the uuid, NOT the player object you won't have any memory leaks.
     
  9. Offline

    Jesian97

    well i guess i have to learn hashmaps now, this should set me a day back, thanks for the help
     
  10. Offline

    SantaClawz69

    Just make sure that you save the hashmap or else once you reload or restart your server the inventories will be gone for all players. I also don't recommend using HashMaps for it might make a lot of lag depending on your player count.
     
  11. Offline

    cfil360

    @Jesian97 They aren't that hard. Essentially they are a list of items(inventories) that can be accessed by an identifier(uuid).

    He doesn't want them saved. That is his entire point. It is just a temporary storage.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
    Jesian97 likes this.
  12. Offline

    SantaClawz69

    Aah okay, I guess I didn't read the whole thing xD But I still don't recommend the use of HashMaps. It could make a bit of lag and use a lot of memory at times.
     
  13. Offline

    Eggspurt

    Make it a command and each command creates a new instance of the Inventory and if you do save to hashmap
     
  14. Offline

    Jesian97

    Ill look into hashmaps and get back to you guys tomorrow, getting late here, thanks for the feedback
     
  15. Offline

    567legodude

    @Jesian97 I would recommend creating a Trade object, and this object is per player, and has its own inventory.
     
  16. Offline

    JBoss925

    Just create a new instance of the inventory with a different title (perhaps their name?) every time you want the inventory to open, so that way it's creating multiple inventories that are all different in that they are player specific but the same in that they are all created from a template.

    For example, let's say "bobby" wants to sell. He opens up the inventory and you take the previously made "sell" inventory and use its attributes to create a new inventory entitled "bobby". Then present that inventory to "bobby". Now, if someone else, let's say "jimmy" wants to sell, do the same but make the inventory named "jimmy". Now "jimmy" can't mess with "bobby"'s inventory because while they are the same inventory, they have different names and are not altering the same instance.
     
  17. Offline

    Jesian97

    solved with hashmaps, thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page