package org.cyclops.cyclopscore.tileentity;

import com.google.common.collect.Maps;
import net.minecraft.util.EnumFacing;
import org.cyclops.cyclopscore.helper.DirectionHelpers;
import org.cyclops.cyclopscore.inventory.SimpleInventory;

import java.util.Collection;
import java.util.Map;

/**
 * A TileEntity with a static internal inventory.
 * @author rubensworks
 *
 */
public abstract class InventoryTileEntity extends InventoryTileEntityBase {
    
    protected SimpleInventory inventory;
    protected Map<EnumFacing, int[]> slotSides;
    protected Map<EnumFacing, Integer> slotSidesSize;
    
    /**
     * Make new tile with an inventory.
     * @param inventorySize Amount of slots in the inventory.
     * @param inventoryName Internal name of the inventory.
     * @param stackSize The maximum stacksize each slot can have
     */
    public InventoryTileEntity(int inventorySize, String inventoryName, int stackSize) {
        inventory = new SimpleInventory(inventorySize , inventoryName, stackSize);
        slotSides = Maps.newHashMap();
        slotSidesSize = Maps.newHashMap();
        for(EnumFacing side : DirectionHelpers.DIRECTIONS) {
            // Init each side to it can theoretically hold all possible slots,
            // Integer lists are not option because Java allows to autoboxing
            // and that would be required in the getter methods below.
            int array[] = new int[inventorySize];
            for(int i = 0; i < inventorySize; i++) array[i] = -1;
            slotSides.put(side, array);
            slotSidesSize.put(side, 0); 
        }
    }
    
    /**
     * Make new tile with an inventory.
     * @param inventorySize Amount of slots in the inventory.
     * @param inventoryName Internal name of the inventory.
     */
    public InventoryTileEntity(int inventorySize, String inventoryName) {
        this(inventorySize , inventoryName, 64);
    }
    
    /**
     * Add mappings to slots to a certain (normalized) side of this TileEntity.
     * @param side The side to map this slots to.
     * @param slots The numerical representations of the slots to map.
     */
    protected void addSlotsToSide(EnumFacing side, Collection<Integer> slots) {
        int[] currentSlots = slotSides.get(side);
        int offset = slotSidesSize.get(side);
        int i = 0;
        for(int slot : slots) {
            currentSlots[offset + i] = slot;
            i++;
        }
        slotSidesSize.put(side, offset + i);
    }
    
    /**
     * Get the internal inventory.
     * @return The SimpleInventory.
     */
    public SimpleInventory getInventory() {
        return inventory;
    }

    @Override
    public int[] getSlotsForFace(EnumFacing side) {
        return slotSides.get(side);
    }
    
}

@@ -540,62 +540,8 @@
 es;%0A
-    protected Map%3CEnumFacing, Integer%3E slotSidesSize;%0A
     
@@ -999,51 +999,8 @@
 ();%0A
-        slotSidesSize = Maps.newHashMap();%0A
     
@@ -1072,420 +1072,40 @@
     
-// Init each side to it can theoretically hold all possible slots,%0A            // Integer lists are not option because Java allows to autoboxing%0A            // and that would be required in the getter methods below.%0A            int array%5B%5D = new int%5BinventorySize%5D;%0A            for(int i = 0; i %3C inventorySize; i++) array%5Bi%5D = -1;%0A            slotSides.put(side, array);%0A            slotSidesSize.put(side, 0
+slotSides.put(side, new int%5B0%5D
 );
- 
 %0A   
@@ -1796,60 +1796,184 @@
  int
- offset = slotSidesSize.get(side);%0A        int i = 0
+%5B%5D newSlots = new int%5BcurrentSlots.length + slots.size()%5D;%0A        System.arraycopy(currentSlots, 0, newSlots, 0, currentSlots.length);%0A        int offset = currentSlots.length
 ;%0A  
@@ -2014,23 +2014,19 @@
         
-current
+new
 Slots%5Bof
@@ -2029,20 +2029,18 @@
 s%5Boffset
- + i
+++
 %5D = slot
@@ -2045,25 +2045,8 @@
 ot;%0A
-            i++;%0A
     
@@ -2068,20 +2068,16 @@
 lotSides
-Size
 .put(sid
@@ -2083,18 +2083,16 @@
 de, 
-offset + i
+newSlots
 );%0A 

Rewrite derpy tile inventory slot assignment to sides No idea how the old code could've ever worked, but it does now.