package org.cyclops.cyclopscore.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import org.cyclops.cyclopscore.helper.InventoryHelpers;

/**
 * A simple inventory for a currently held item by a player that can be stored in NBT.
 * @author rubensworks
 *
 */
public class NBTSimpleInventory extends SimpleInventory {

private static final String NBT_TAG_ROOT = "NBTSimpleInventory";
	
	protected EntityPlayer player;
	protected int itemIndex;
	
	/**
     * Make a new instance.
	 * @param player The player holding the item.
	 * @param itemIndex The index of the item in use inside the player inventory.
     * @param size The amount of slots in the inventory.
     * @param stackLimit The stack limit for each slot.
     */
	public NBTSimpleInventory(EntityPlayer player, int itemIndex, int size, int stackLimit) {
		super(size, NBT_TAG_ROOT, stackLimit);
		this.player = player;
		this.itemIndex = itemIndex;
		InventoryHelpers.validateNBTStorage(this, InventoryHelpers.getItemFromIndex(player, itemIndex), NBT_TAG_ROOT);
	}
	
	@Override
	public void markDirty() {
		NBTTagCompound tag = InventoryHelpers.getItemFromIndex(player, itemIndex).getTagCompound();
		writeToNBT(tag, NBT_TAG_ROOT);
		InventoryHelpers.getItemFromIndex(player, itemIndex).setTagCompound(tag);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound data, String tagName) {
        InventoryHelpers.readFromNBT(this, data, tagName);
    }
	
	@Override
	public void writeToNBT(NBTTagCompound data, String tagName) {
        InventoryHelpers.writeToNBT(this, data, tagName);
    }

}

@@ -86,16 +86,53 @@
 Player;%0A
+import net.minecraft.item.ItemStack;%0A
 import n
@@ -1156,26 +1156,27 @@
 %7B%0A%09%09
-NBTTagCompound tag
+ItemStack itemStack
  = I
@@ -1230,26 +1230,149 @@
 dex)
-.getTagCompound();
+;%0A%09%09NBTTagCompound tag = itemStack.getTagCompound();%0A%09%09if(tag == null) %7B%0A%09%09%09tag = new NBTTagCompound();%0A%09%09%09itemStack.setTagCompound(tag);%0A%09%09%7D
 %0A%09%09w

Fix calls to null tags on NBT inventories