package org.cyclops.cyclopscore.inventory;

import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;

/**
 * Iterate over a player's inventory and any other attached inventory like baubles.
 * @author rubensworks
 */
public class PlayerExtendedInventoryIterator implements Iterator<ItemStack> {

    public static final List<IInventoryExtender> INVENTORY_EXTENDERS = Lists.newArrayList();

    private final Queue<InventoryIterator> iterators;

    /**
     * Create a new HotbarIterator.
     * @param player The player to iterate the hotbar from.
     */
    public PlayerExtendedInventoryIterator(PlayerEntity player) {
        this.iterators = Queues.newArrayDeque();
        iterators.add(new PlayerInventoryIterator(player));
        for (IInventoryExtender inventoryExtender : PlayerExtendedInventoryIterator.INVENTORY_EXTENDERS) {
            IItemHandlerModifiable inv = inventoryExtender.getInventory(player);
            if (inv != null) {
                iterators.add(new InventoryIterator(inv));
            }
        }
    }

    @Override
    public boolean hasNext() {
        return iterators.size() > 0 && iterators.peek().hasNext();
    }

    @Override
    public ItemStack next() {
        if (!iterators.peek().hasNext()) {
            iterators.poll();
        }
        if (iterators.peek().hasNext()) {
            return iterators.peek().next();
        }
        throw new IndexOutOfBoundsException();
    }

    @Override
    public void remove() {
        throw new RuntimeException("Not implemented.");
    }

    /**
     * Replaces the itemstack on the position of the last returned itemstack.
     * @param itemStack The itemstack to place.
     */
    public void replace(ItemStack itemStack) {
        iterators.peek().replace(itemStack);
    }

    /**
     * A registerable inventory extender for iterating over.
     */
    public static interface IInventoryExtender {

        @Nullable
        public IItemHandlerModifiable getInventory(PlayerEntity player);

    }

}

@@ -1484,17 +1484,16 @@
     if (
-!
 iterator
@@ -1517,32 +1517,49 @@
 )) %7B%0A           
+ ItemStack next =
  iterators.poll(
@@ -1558,19 +1558,26 @@
 rs.p
-oll
+eek().next
 ();%0A    
     
@@ -1564,38 +1564,32 @@
 k().next();%0A    
-    %7D%0A
         if (iter
@@ -1576,32 +1576,33 @@
             if (
+!
 iterators.peek()
@@ -1619,38 +1619,35 @@
 ) %7B%0A            
-return
+   
  iterators.peek(
@@ -1638,36 +1638,68 @@
  iterators.p
-eek().
+oll();%0A            %7D%0A            return 
 next
-()
 ;%0A        %7D%0A

Fix PlayerExtendedInventoryIterator only checking first inventory