package net.darkhax.darkutils.features.charms;

import net.darkhax.bookshelf.inventory.SlotFake;
import net.darkhax.bookshelf.item.ItemInventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerFilter extends Container {

    public ContainerFilter (InventoryPlayer playerInv, ItemInventory inventory) {

        for (int invIndex = 0; invIndex < 5; invIndex++) {
            this.addSlotToContainer(new SlotFake(inventory, invIndex, 44 + invIndex * 18, 20));
        }

        for (int playerInvX = 0; playerInvX < 3; ++playerInvX) {
            for (int playerInvY = 0; playerInvY < 9; ++playerInvY) {
                this.addSlotToContainer(new Slot(playerInv, playerInvY + playerInvX * 9 + 9, 8 + playerInvY * 18, playerInvX * 18 + 51));
            }
        }

        for (int hotbarIndex = 0; hotbarIndex < 9; ++hotbarIndex) {
            this.addSlotToContainer(new Slot(playerInv, hotbarIndex, 8 + hotbarIndex * 18, 109));
        }
    }

    @Override
    public boolean canDragIntoSlot (Slot slot) {

        return !(slot instanceof SlotFake);
    }

    @Override
    public boolean canInteractWith (EntityPlayer player) {

        return true;
    }

    @Override
    public ItemStack transferStackInSlot (EntityPlayer player, int index) {

        ItemStack itemstack = null;
        final Slot slot = this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) {

            itemstack = slot.getStack().copy();
            for (int fakeSlotID = 0; fakeSlotID < 5; fakeSlotID++) {

                final Slot fakeSlot = this.getSlot(fakeSlotID);

                if (fakeSlot instanceof SlotFake && !fakeSlot.getHasStack()) {

                    fakeSlot.putStack(itemstack);
                    return itemstack;
                }
            }

            final Slot firstFakeSlot = this.getSlot(0);

            if (firstFakeSlot instanceof SlotFake) {
                firstFakeSlot.putStack(itemstack);
            }
        }

        return itemstack;
    }

    @Override
    public ItemStack slotClick (int slot, int dragType, ClickType clickTypeIn, EntityPlayer player) {

        if (slot >= 0 && this.getSlot(slot) != null && this.getSlot(slot).getStack() == player.getHeldItemMainhand()) {
            return null;
        }

        return super.slotClick(slot, dragType, clickTypeIn, player);
    }
}
@@ -244,50 +244,8 @@
 er;%0A
-import net.minecraft.inventory.ClickType;%0A
 impo
@@ -1448,44 +1448,8 @@
  %7B%0A%0A
-        ItemStack itemstack = null;%0A
     
@@ -1553,37 +1553,13 @@
 ck()
-) %7B%0A%0A            itemstack = 
+ && !
 slot
@@ -1574,15 +1574,21 @@
 k().
-copy();
+isEmpty()) %7B%0A
 %0A   
@@ -1739,193 +1739,59 @@
     
-if (fakeSlot instanceof SlotFake && !fakeSlot.getHasStack()) %7B%0A%0A                    fakeSlot.putStack(itemstack);%0A                    return itemstack;%0A                %7D%0A   
+// Slot doesn't have an item. Add the copy.%0A
        
-  %7D%0A%0A
     
@@ -1799,131 +1799,61 @@
     
-   final Slot firstFakeSlot = this.getSlot(0);%0A%0A            if (firstFakeSlot instanceof SlotFake) %7B%0A                firstF
+if (!fakeSlot.getHasStack()) %7B%0A%0A                    f
 akeS
@@ -1865,25 +1865,31 @@
 utStack(
-items
+slot.getS
 tack
+()
 );%0A     
@@ -1891,37 +1891,24 @@
             
-%7D%0A        %7D%0A%0A
         retu
@@ -1914,262 +1914,42 @@
 urn 
-itemstack;%0A    %7D%0A%0A    @Override%0A    public ItemStack slotClick (int slot, int dragType, ClickType clickTypeIn, EntityPlayer player) %7B%0A%0A        if (slot %3E= 0 && this.getSlot(slot) != null && this.getSlot(slot).getStack() == player.getHeldItemMainhand()) %7B
+ItemStack.EMPTY;%0A                %7D
 %0A   
@@ -1961,20 +1961,9 @@
     
-return null;
+%7D
 %0A   
@@ -1989,60 +1989,23 @@
 urn 
-super.slotClick(slot, dragType, clickTypeIn, player)
+ItemStack.EMPTY
 ;%0A  

Fix client freeze when shift clicking into the null charm. #121