/*
 * Copyright (c) 2015 Jerrell Fang
 *
 * This project is Open Source and distributed under The MIT License (MIT)
 * (http://opensource.org/licenses/MIT)
 *
 * You should have received a copy of the The MIT License along with
 * this project.   If not, see <http://opensource.org/licenses/MIT>.
 */

package com.meowinnovation.neitest.common.block.tile;

import com.meowinnovation.neitest.api.recipe.RecipeMeowMachine;
import com.meowinnovation.neitest.api.recipe.Recipes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.Constants;

/**
 * Created by Meow J on 8/30/2015.
 *
 * @author Meow J
 */
public class TileMeowMachine extends TileEntity implements ISidedInventory, IUpdatePlayerListBox {
    private ItemStack[] inventory = new ItemStack[3];

    @Override
    public void update() {
        if (worldObj.isRemote)
            return;
        for (RecipeMeowMachine recipe : Recipes.meowMachineRecipes) {
            if (recipe.matches(this)) {
                ItemStack result = recipe.getOutput().copy();
                if (inventory[2] == null) {
                    inventory[2] = result;
                    inventory[0] = null;
                    if (--inventory[1].stackSize < 1)
                        inventory[1] = null;
                    worldObj.playSoundEffect(pos.getX() + 0.5D, pos.getY() + 0.1D, pos.getZ() + 0.5D, "mob.cat.meow", 1.0F, 1.0F);
                } else if (inventory[2].isItemEqual(result) && inventory[2].stackSize + result.stackSize <= inventory[2].getMaxStackSize()) {
                    inventory[2].stackSize += result.stackSize;
                    inventory[0] = null;
                    if (--inventory[1].stackSize < 1)
                        inventory[1] = null;
                    worldObj.playSoundEffect(pos.getX() + 0.5D, pos.getY() + 0.1D, pos.getZ() + 0.5D, "mob.cat.meow", 1.0F, 1.0F);
                } else return;
            }
            if (inventory[0] == null || inventory[1] == null)
                return;
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < inventory.length; ++i) {
            ItemStack itemStack = inventory[i];
            if (itemStack != null) {
                NBTTagCompound tagItem = new NBTTagCompound();
                tagItem.setByte("Slot", (byte) i);
                itemStack.writeToNBT(tagItem);
                itemList.appendTag(tagItem);
            }
        }
        compound.setTag("Inventory", itemList);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        NBTTagList tagList = compound.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
        for (int i = 0; i < tagList.tagCount(); ++i) {
            NBTTagCompound tagItem = tagList.getCompoundTagAt(i);
            byte slot = tagItem.getByte("Slot");
            if (slot >= 0 && slot < inventory.length) {
                inventory[slot] = ItemStack.loadItemStackFromNBT(tagItem);
            }
        }
    }

    @Override
    public int[] getSlotsForFace(EnumFacing side) {
        return new int[0];
    }

    @Override
    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
        return isItemValidForSlot(index, itemStackIn);
    }

    @Override
    public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
        return false;
    }

    @Override
    public int getSizeInventory() {
        return inventory.length;
    }

    @Override
    public ItemStack getStackInSlot(int index) {
        return inventory[index];
    }

    @Override
    public ItemStack decrStackSize(int index, int count) {
        ItemStack stack = getStackInSlot(index);
        if (stack != null) {
            if (stack.stackSize <= count) {
                setInventorySlotContents(index, null);
            } else {
                stack = stack.splitStack(count);
                if (stack.stackSize == 0) {
                    setInventorySlotContents(index, null);
                }
            }
        }
        return stack;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int index) {
        return inventory[index];
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        inventory[index] = stack;
        if (stack != null && stack.stackSize > getInventoryStackLimit()) {
            stack.stackSize = getInventoryStackLimit();
        }
        markDirty();
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return worldObj.getTileEntity(pos) == this && player.getDistanceSq(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5) < 64;
    }

    @Override
    public void openInventory(EntityPlayer player) {

    }

    @Override
    public void closeInventory(EntityPlayer player) {

    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return true;
    }

    @Override
    public int getField(int id) {
        return 0;
    }

    @Override
    public void setField(int id, int value) {

    }

    @Override
    public int getFieldCount() {
        return 0;
    }

    @Override
    public void clear() {
        for (int i = 0; i < inventory.length; ++i)
            inventory[i] = null;
    }

    @Override
    public String getCommandSenderName() {
        return "TileMeowMachine";
    }

    @Override
    public boolean hasCustomName() {
        return false;
    }

    @Override
    public IChatComponent getDisplayName() {
        return new ChatComponentText("Meow Machine");
    }

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        writeToNBT(nbttagcompound);
        return new S35PacketUpdateTileEntity(pos, -999, nbttagcompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
        super.onDataPacket(net, packet);
        readFromNBT(packet.getNbtCompound());
    }

}

@@ -1405,32 +1405,110 @@
         return;%0A
+        if (inventory%5B0%5D == null %7C%7C inventory%5B1%5D == null)%0A            return;%0A
         for (Rec

Fix a NPE