package com.jaquadro.minecraft.storagedrawers.item;

import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

public class ItemCustomDrawers extends ItemDrawers
{
    public ItemCustomDrawers (Block block) {
        super(block);
    }

    @Override
    public boolean placeBlockAt (ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) {
        if (!super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata))
            return false;

        TileEntityDrawers tile = (TileEntityDrawers) world.getTileEntity(x, y, z);
        if (tile != null && stack.hasTagCompound() && !stack.getTagCompound().hasKey("tile")) {
            if (stack.getTagCompound().hasKey("MatS"))
                tile.setMaterialSide(ItemStack.loadItemStackFromNBT(stack.getTagCompound().getCompoundTag("MatS")));
            if (stack.getTagCompound().hasKey("MatT"))
                tile.setMaterialTrim(ItemStack.loadItemStackFromNBT(stack.getTagCompound().getCompoundTag("MatT")));
            if (stack.getTagCompound().hasKey("MatF"))
                tile.setMaterialFront(ItemStack.loadItemStackFromNBT(stack.getTagCompound().getCompoundTag("MatF")));
        }

        return true;
    }

    public static ItemStack makeItemStack (Block block, int count, ItemStack matSide, ItemStack matTrim, ItemStack matFront) {
        Item item = Item.getItemFromBlock(block);
        if (!(item instanceof ItemCustomDrawers))
            return null;

        NBTTagCompound tag = new NBTTagCompound();

        if (matSide != null) {
            NBTTagCompound itag = new NBTTagCompound();
            matSide.writeToNBT(itag);
            tag.setTag("MatS", itag);
        }

        if (matTrim != null) {
            NBTTagCompound itag = new NBTTagCompound();
            matTrim.writeToNBT(itag);
            tag.setTag("MatT", itag);
        }

        if (matFront != null) {
            NBTTagCompound itag = new NBTTagCompound();
            matFront.writeToNBT(itag);
            tag.setTag("MatF", itag);
        }

        ItemStack stack = new ItemStack(item, count, 0);
        if (!tag.hasNoTags())
            stack.setTagCompound(tag);

        return stack;
    }
}

@@ -1842,34 +1842,32 @@
 matSide != null)
- %7B
 %0A            NBT
@@ -1867,89 +1867,82 @@
     
-NBTTagCompound itag = new NBTTagCompound();%0A            matSide.writeToNBT(itag);
+tag.setTag(%22MatS%22, getMaterialTag(matSide));%0A%0A        if (matTrim != null)
 %0A   
@@ -1969,28 +1969,37 @@
 %22Mat
-S%22, itag);%0A        %7D
+T%22, getMaterialTag(matTrim));
 %0A%0A  
@@ -2003,36 +2003,37 @@
 %0A        if (mat
-Trim
+Front
  != null) %7B%0A    
@@ -2017,34 +2017,32 @@
 atFront != null)
- %7B
 %0A            NBT
@@ -2042,89 +2042,141 @@
     
-NBTTagCompound itag = new NBTTagCompound();%0A            matTrim.writeToNBT(itag);
+tag.setTag(%22MatF%22, getMaterialTag(matFront));%0A%0A        ItemStack stack = new ItemStack(item, count, 0);%0A        if (!tag.hasNoTags())
 %0A   
@@ -2188,88 +2188,83 @@
     
+s
 ta
-g
+ck
 .setTag
-(%22MatT%22, itag);%0A        %7D%0A%0A        if (matFront != null) %7B
+Compound(tag);%0A%0A        return stack;
 %0A    
-  
+%7D%0A%0A
     
- 
+private static
  NBT
@@ -2279,245 +2279,171 @@
 und 
-itag = new NBTTagCompound();%0A            matFront.writeToNBT(itag);%0A            tag.setTag(%22MatF%22, itag);%0A        %7D%0A%0A        ItemStack stack = new ItemStack(item, count, 0);%0A        if (!tag.hasNoTags())%0A            stack.setTagCompound(
+getMaterialTag (ItemStack mat) %7B%0A        mat = mat.copy();%0A        mat.stackSize = 1;%0A%0A        NBTTagCompound itag = new NBTTagCompound();%0A        mat.writeToNBT(i
 tag)
@@ -2452,31 +2452,30 @@
 %0A        return 
-stack
+itag
 ;%0A    %7D%0A%7D%0A

Fix custom drawers not stacking if crafted from different material "stack sizes"