Solved Custom inventory on HashMap problem

Discussion in 'Plugin Development' started by men8, Apr 22, 2015.

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

    men8

    Hi, I've got problem with custom inventory on HashMap. I know how HashMaps works and yes I searched google so many times and I can't find solution to my problem yet. So my main problem is that when player joins server it put's him in HashMap<UUID, Inventory>. In player interact event when player right or left click emerald it gets this specific inventory and put inside pre maid ItemStack with player statistics in lore (yes statistics are also in HashMaps). I will explain my problem on two players Bob and Tom :p So if Bob open menu ItemStack shows Bob's statistics if he close and opens menu again everything works fine, but when Tom joins server and open menu it show Tom's stats (and at this point magic starts). So now when Bob open's menu it will show somehow Tom's statistics and I have no idea why this is not working properly when both players have different inventories?
    This is part of code form PlayerJoinEvent:
    Code:
    HashMaps.pinv.put(p.getUniqueId(), Bukkit.createInventory(null, 9, "Main Menu"));
    this is part of code from PlayerInteractEvent:
    Code:
    HashMaps.pinv.get(p.getUniqueId()).setItem(2, MainMenuPage.getInstance().playerStats);
    p.openInventory(HashMaps.pinv.get(p.getUniqueId()));
    Probably it's something totaly stupid but i can't find the problem :O
    Sorry for my english
     
  2. Offline

    nj2miami

    Show more code. You are not showing how you are building the inventories which is almost certainly where your problem is.

    My guess is you are not creating unique ItemStacks for each player inside each inventory but rather references to previously created ItemStacks.

    "put inside pre maid ItemStack with player statistics in lore" - this is your issue. Update the lore when a Player opens an inventory with their stats. Make sure when you add ItemStacks to the inventories when you create them that you are creating NEW ItemStacks for EACH item you place inside.

    Otherwise you are just passing references to an ItemStack and it will change for all inventories if you do this.
     
    Last edited: Apr 22, 2015
    men8 likes this.
  3. Offline

    men8

    @nj2miami Ok, thanks I'll check that but it means i will need to change a lot with ItemStacks :p I think i will never serch for problems in itemstacks :p

    @nj2miami Thanks a lot it works now :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page