Solved Getting/Setting player inventory

Discussion in 'Plugin Development' started by torpkev, Jan 11, 2019.

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

    torpkev

    I'm testing an inventory GUI and it is displaying fine, but I'd like to not have the players inventory items show while it is open.

    So I have a hashmap with the players uuid and ItemStack[] and I populate that when I open the gui

    Code:
    ItemStack[] inv = player.getInventory().getContents();
       Main.PlayerInventory.put(player.getUniqueId().toString(), inv);
        for (ItemStack i : inv) // Looping through just for my own sanity
       {
          if (i != null)
          {
             Alert.Log("InventoryTest", i.getItemMeta().getDisplayName());
          } else {
             Alert.Log("InventoryTest", "No item");
          }
       }
       player.getInventory().clear(); // Clearing the inventory
       for (int i = 0; i < 36; i++) {
          player.getInventory().setItem(i, new ItemStack(Material.BLACK_STAINED_GLASS_PANE, 1)); // Looping through and replacing it all with black stained glass
       }
    
    Then when the inventory closes, I'm checking the hashmap to see if the UUID is there, if it is, then I'm trying to restore the items

    Code:
    @EventHandler
        public void onInventoryClose(InventoryCloseEvent evt) {
            HashMap<String, ItemStack[]> hmInv = Main.PlayerInventory;
            if (hmInv != null)
            {
                Alert.Log("InventoryTest", "Getting player inventory back from hashmap");
                ItemStack[] inv = hmInv.get(evt.getPlayer().getUniqueId().toString());
                if (inv != null)
                {
                    int j = 0;
                    for (ItemStack i : inv)
                    {
                        if (i != null)
                        {
                            Alert.Log("GUI Restore: ", "Setting " + i.toString() + " to position " + Integer.toString(j));
                            evt.getPlayer().getInventory().setItem(j, i);
                        } else {
                            Alert.Log("GUI Restore: ", "ItemStack is null - Setting AIR to position " + Integer.toString(j));
                            evt.getPlayer().getInventory().setItem(j, new ItemStack(Material.AIR, 1));
                        }
                        j++;
                    }
                    Main.PlayerInventory.remove(evt.getPlayer().getUniqueId().toString());
                } else {
                    Alert.Log("InventoryTest", "No such entry in hashmap");
                }
            }
        }
    
    Except I'm only getting the items in my hotbar back, the main inventory is staying as black stained glass.

    The log to console (Alert.Log) is displaying the items in the main inventory that should be restored, but it just isn't happening.

    Any clue to what I'm doing wrong?
     
  2. @torpkev

    I don't know why your current code doesn't work, but you could try to use player.getInventory().setContents(inv) instead of looping over everything.

    You could also try to restore the inventory in a delayed task and see what happens.
     
  3. Offline

    CraftCreeper6

    @torpkev
    try player#updateInventory() after the loop has finished.
     
  4. Offline

    torpkev

    Thanks!

    The updateInventory() seems to have done the trick!
     
    CraftCreeper6 likes this.
Thread Status:
Not open for further replies.

Share This Page