Solved Best way to add items from a HashMap to a player's Inventory

Discussion in 'Plugin Development' started by justin_393, Apr 1, 2015.

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

    justin_393

    Basically what the title says, what's the best way to add what's in a HashMap to a player's inventory.

    Here's my code:

    Code:
    List<UUID> respawnPlayer = new ArrayList<>();
        HashMap<ItemStack[], UUID> in = new HashMap<ItemStack[], UUID>();
        HashMap<Integer, UUID> level = new HashMap<Integer, UUID>();
        HashMap<ItemStack[], UUID> armor = new HashMap<ItemStack[], UUID>();
    
    
    UUID uuid = p.getUniqueId();
                          
    
                            in.put(p.getInventory().getContents(), uuid);
                            level.put(p.getLevel(), uuid);
                            armor.put(p.getInventory().getArmorContents(), uuid);
                            respawnPlayer.add(uuid);
    
    @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {
            Player p = event.getPlayer();
            UUID uuid = p.getUniqueId();
            if (respawnPlayer.contains(uuid)) {
        //where I'm trying to figure out the best way        p.getInventory().addItem(in);
            }
        }
     
  2. Offline

    Skionz

    Iterate through the array and add each ItemStack to their inventory.
     
  3. Offline

    justin_393

    Mind giving a small example? I'm not entirely sure how to do that..
     
  4. Offline

    Skionz

    FileConfiguration#set(String, Object)
     
  5. Offline

    justin_393

    I'm not even using a file..
     
  6. Offline

    Skionz

    Sorry, I thought I was looking at another thread. Use an enhanced for loop to iterate through each ItemStack in the array.
     
  7. Offline

    justin_393

    Like this?
    Code:
    for (ItemStack[] inv : in.values()) {
                  
                }
    Only problem is I had to switch my HashMap from HashMap<ItemStack[], UUID> in = new HashMap<ItemStack[], UUID>(); to HashMap<UUID, ItemStack[]> in = new HashMap<UUID, ItemStack[]>();

    Does it matter which one goes first? I've always been confused on that..

    Sorry, but I'm not that used to HashMaps :/
     
    Last edited: Apr 1, 2015
  8. Offline

    Skionz

    The first class represents the key's type. The second represents the value's.
     
  9. Offline

    justin_393

    So I want the key to be the player's UUID and the values their Inventory, correct?
     
  10. Offline

    RenditionsRule

    @justin_393

    Yes you do. That way, you can use <hashmap>.get(<key>) which would return the value, in your case, the ItemStack[]. From there, just have it loop through that array and add the items. I used a similar method in my recent plugin.
     
  11. Offline

    justin_393

    So this should work, right?
    Code:
    Player p = event.getPlayer();
            UUID uuid = p.getUniqueId();
            if (respawnPlayer.contains(uuid)) {
               
                for (ItemStack[] inv : in.values()) {
                    p.getInventory().addItem(inv);
                   
                }
    and I make the HashMap here: HashMap<UUID, ItemStack[]> in = new HashMap<UUID, ItemStack>();
    I add to the HashMap here: in.put(uuid, p.getInventory().getContents());

    Will it return the correct HashMap for the player or am I missing something here?
     
  12. Offline

    RenditionsRule

    That code would not work the way you planned. Try this for the for loop:

    Code:
    for(ItemStack item : in.get(<key here>)) {
        p.getInventory().addItem(item);
    }
    
     
  13. Offline

    justin_393

    Yep, there we go. I thought I needed to call the correct HashMap somewhere, but I wasn't sure. Thanks!

    Just out of curiosity. Are Armor Contents saved in the Inventory with Player#getInventory(); ?
     
Thread Status:
Not open for further replies.

Share This Page