package org.cyclops.integrateddynamics.core.evaluate.variable;

import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import org.cyclops.cyclopscore.datastructure.DimPos;
import org.cyclops.cyclopscore.helper.TileHelpers;
import org.cyclops.cyclopscore.persist.nbt.INBTProvider;

/**
 * A list proxy for an inventory at a certain position.
 */
public class ValueTypeListProxyPositionedInventory extends ValueTypeListProxyPositioned<ValueObjectTypeItemStack, ValueObjectTypeItemStack.ValueItemStack> implements INBTProvider {

    public ValueTypeListProxyPositionedInventory(DimPos pos, EnumFacing side) {
        super(ValueTypeListProxyFactories.POSITIONED_INVENTORY.getName(), ValueTypes.OBJECT_ITEMSTACK, pos, side);
    }

    protected IItemHandler getInventory() {
        return TileHelpers.getCapability(getPos(), getSide(), CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
    }

    @Override
    public int getLength() {
        IItemHandler inventory = getInventory();
        if(inventory == null) {
            return 0;
        }
        return inventory.getSlots();
    }

    @Override
    public ValueObjectTypeItemStack.ValueItemStack get(int index) {
        return ValueObjectTypeItemStack.ValueItemStack.of(getInventory().getStackInSlot(index));
    }
}

@@ -358,16 +358,44 @@
 vider;%0A%0A
+import java.util.Iterator;%0A%0A
 /**%0A * A
@@ -1379,11 +1379,1229 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Iterator%3CValueObjectTypeItemStack.ValueItemStack%3E iterator() %7B%0A        // We use a custom iterator that retrieves the itemhandler capability only once.%0A        // Because for large inventories, the capability would have to be retrieved for every single slot,%0A        // which could result in a major performance problem.%0A        return new ValueTypeListProxyPositionedInventory.ListFactoryIterator(getInventory());%0A    %7D%0A%0A    public static class ListFactoryIterator implements Iterator%3CValueObjectTypeItemStack.ValueItemStack%3E %7B%0A%0A        private final IItemHandler itemHandler;%0A        private int index = 0;%0A%0A        public ListFactoryIterator(IItemHandler itemHandler) %7B%0A            this.itemHandler = itemHandler;%0A        %7D%0A%0A        @Override%0A        public boolean hasNext() %7B%0A            return itemHandler != null && index %3C itemHandler.getSlots();%0A        %7D%0A%0A        @Override%0A        public ValueObjectTypeItemStack.ValueItemStack next() %7B%0A            return ValueObjectTypeItemStack.ValueItemStack.of(this.itemHandler.getStackInSlot(index++));%0A        %7D%0A%0A        @Override%0A        public void remove() %7B%0A            throw new UnsupportedOperationException(%22remove%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Improve performance of inventory list proxy iteration