package oceania.entity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import oceania.items.Items;
import oceania.util.BoatTypes;

public class EntityOceaniaBoatWithChest extends EntityOceaniaBoat implements IInventory {
	
    private ItemStack[] chestItems = new ItemStack[36];
	
	public EntityOceaniaBoatWithChest(World world) 
	{
		super(world);
	}

	public EntityOceaniaBoatWithChest(World world, double x, double y, double z) 
	{
		super(world, x, y, z);
	}
	
	public EntityOceaniaBoatWithChest(World world, BoatTypes type, double x, double y, double z) 
	{
		super(world, x, y, z);
		this.setBoatType(type);
	}

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

	@Override
	public ItemStack getStackInSlot(int slot) 
	{
		return this.chestItems[slot];
	}

	@Override
	public ItemStack decrStackSize(int slot, int amount) 
	{
		ItemStack returnStack;
		if(this.chestItems[slot] != null)
		{
			if(this.chestItems[slot].stackSize < amount)
			{
				returnStack = this.chestItems[slot];
				this.setInventorySlotContents(slot, null);
				return returnStack;
			} else
			{
				returnStack = this.chestItems[slot].splitStack(amount);
				
				if(this.chestItems[slot].stackSize == 0)
					this.setInventorySlotContents(slot, null);
				return returnStack;
			}
		}
		return null;
	}

	@Override
	public ItemStack getStackInSlotOnClosing(int slot) 
	{
		return this.getStackInSlot(slot);
	}

	@Override
	public void setInventorySlotContents(int slot, ItemStack stack) 
	{
		this.chestItems[slot] = stack;
		
		if(stack != null && stack.stackSize > 64)
			stack.stackSize = 64;
	}

	@Override
	public String getInvName() 
	{
		int strength = this.getDataWatcher().getWatchableObjectInt(22);
		for(int index = 0; index < BoatTypes.values().length; index++) {
			if(((Integer) BoatTypes.values()[index].strength).equals(strength)) {
				return BoatTypes.values()[index]._loc + " with Chest";
			}
		}
		return "";
	}

	@Override
	public boolean isInvNameLocalized() 
	{
		return true;
	}

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

	@Override
	public boolean isUseableByPlayer(EntityPlayer player) {
		return !this.isDead;
	}
	
	@Override
	public boolean isItemValidForSlot(int slot, ItemStack stack) 
	{
		return true;
	}
	
	@Override
    public void writeEntityToNBT(NBTTagCompound tag)
    {
		super.writeEntityToNBT(tag);
		for(int count = 0; count < this.chestItems.length; count++)
		{
			
		}
    }
    
	@Override
    public void readEntityFromNBT(NBTTagCompound tag)
    {
		super.readEntityFromNBT(tag);
    }
	
	@Override
	public void onInventoryChanged() 
	{}

	@Override
	public void openChest() 
	{}

	@Override
	public void closeChest() 
	{}

}

@@ -2529,16 +2529,71 @@
 T(tag);%0A
+%09%09NBTTagCompound chestItemsTag = new NBTTagCompound();%0A
 %09%09for(in
@@ -2657,126 +2657,738 @@
 %0A%09%09%09
-%0A%09%09%7D%0A    %7D%0A    %0A%09@Override%0A    public void readEntityFromNBT(NBTTagCompound tag)%0A    %7B%0A%09%09super.readEntityFromNBT(tag);
+if(this.chestItems%5Bcount%5D != null)%0A%09%09%09%7B%0A%09%09%09%09NBTTagCompound itemTag = new NBTTagCompound();%0A%09%09%09%09this.chestItems%5Bcount%5D.writeToNBT(itemTag);%0A%09%09%09%09chestItemsTag.setCompoundTag(%22item%22 + String.valueOf(count), itemTag);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09tag.setCompoundTag(%22chestItems%22, chestItemsTag);%0A    %7D%0A    %0A%09@Override%0A    public void readEntityFromNBT(NBTTagCompound tag)%0A    %7B%0A%09%09super.readEntityFromNBT(tag);%0A%09%09NBTTagCompound chestItemsTag = tag.getCompoundTag(%22chestItems%22);%0A%09%09for(int count = 0; count %3C this.chestItems.length; count++)%0A%09%09%7B%0A%09%09%09if(chestItemsTag.hasKey(%22item%22 + String.valueOf(count)))%0A%09%09%09%7B%0A%09%09%09%09this.setInventorySlotContents(count, ItemStack.loadItemStackFromNBT(chestItemsTag.getCompoundTag(%22item%22 + String.valueOf(count))));%0A%09%09%09%7D%0A%09%09%7D
 %0A   

Tag everything. tag. Tag. Tag.