Development Assistance Odd problem with hashmap

Discussion in 'Plugin Help/Development/Requests' started by BizarrePlatinum, Jun 1, 2015.

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

    BizarrePlatinum

    Code:
    public void onDisable() {
            for(Player p : storedInv.keySet()) {
                saveInventoryToFile(storedInv.get(p), this.getDataFolder(), p.getName());
            }
    }
    
    Code:
    Inventory playerInventory = this.getInventoryFromFile(new File(this.getDataFolder(), p.getName() + ".logged-inv"));
            if (playerInventory != null) {
                storedInv.put(p, playerInventory);
               
            }
           
            if(!(storedInv.containsKey(p))) {
                storedInv.put(p, p.getInventory());
            }
           
            if(p.getExp() > 0 && !storedXp.containsKey(p)) {
                storedXp.put(p, p.getExp());
            }
           
            for(int i = 0; i < p.getInventory().getSize(); i++) {
                p.getInventory().setItem(i, new ItemStack(Material.BARRIER));
            }
    
    I'm not sure exactly what I'm doing wrong here, but when I save the inventory to the file, all of the items are barriers. I do not see why this code shouldn't be working, as I am saving the inventory, then setting the contents to barriers. If I could get some insight on why this isn't working, that would be great.
     
  2. Offline

    I Al Istannen

    @BizarrePlatinum I'm not quite sure, but isn't "p.getInventory()" just a reference. So your "storedXp" and the actual Inventory would link to the same object?
     
  3. Offline

    BizarrePlatinum

  4. Offline

    I Al Istannen

    @BizarrePlatinum Try not putting "p.getInventory()" but a copy of it. So loop through all contents of the player Inventory and add them to another, new one. Then add the new one.
     
Thread Status:
Not open for further replies.

Share This Page