How to loop through player inventory

Hi guys, I have problem with inventory. How can I loop through player inventory and get ItemStacks of each item there, so I can add them just to list. Anyone knows how to do it?

Few ways to do this. My personal favourite is this.

Set<ItemStack> set = new HashSet<>();
for(Inventory slot : inventory.slots()){
    slot.peek().ifPresent(i -> set.add(i));
}

Doing this from memory, so dont expect everything to be correct, however that is the basis.

@MoseMister Not working. It’s only looping 3 items.

Console logs:
[16:14:43] [Server thread/WARN] [scpcore]: Item: Optional[[email protected]] added to kit Example dept.
[16:14:43] [Server thread/ERROR] [scpcore]: Looped item is null!
[16:14:43] [Server thread/ERROR] [scpcore]: Looped item is null!

Code:

for(Inventory slot : player.getInventory())
    {
        if(slot.peek().isPresent())
        {
            kitItems.add(slot.peek().get());
            SCPCore.getInstance().getLogger().warn("Item: " + slot.peek().toString() + " added to kit " +lambdaClass.getName() + '.');
        }
        else
        {
            SCPCore.getInstance().getLogger().error("Looped item is null!");
        }
    }

and all 9 slots of inventory were filled with items

You have

for(Inventory slot : player.getInventory())

Which is looping through every child of the inventory (so a player inventory, it would loop through the armor slots, small crafting, main player inventory and the hotbar)

It needs to be

player.getInventory().slots()

Edit: 9 items in a player inventory? Do you only want the hotbar of the players inventory?