package net.mcft.copy.backpacks.api;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public class BackpackProperties implements IExtendedEntityProperties {
	
	// Used by EntityUtils.getIdentifier().
	public static final String IDENTIFIER = "WearableBackpack";
	
	public ItemStack backpackStack = null;
	public IBackpackData backpackData = null;
	
	public IBackpack lastBackpackType = null;
	public int playersUsing = 0;
	
	// IPropertiesBackpack implementation
	
	private EntityLivingBase entity;
	
	@Override
	public void init(Entity entity, World world) {
		this.entity = (EntityLivingBase)entity;
	}
	
	@Override
	public void saveNBTData(NBTTagCompound compound) {
		if (backpackStack != null)
			compound.setTag("stack", backpackStack.writeToNBT(new NBTTagCompound()));
		if (backpackData != null) {
			NBTTagCompound dataCompound = new NBTTagCompound();
			backpackData.writeToNBT(dataCompound);
			compound.setTag("data", dataCompound);
		}
	}
	
	@Override
	public void loadNBTData(NBTTagCompound compound) {
		if (compound.hasKey("stack"))
			backpackStack = ItemStack.loadItemStackFromNBT(compound.getCompoundTag("stack"));
		
		ItemStack backpack = BackpackHelper.getEquippedBackpack(entity);
		lastBackpackType = BackpackHelper.getBackpackType(backpack);
		
		if (compound.hasKey("data") && (lastBackpackType != null))
			(backpackData = lastBackpackType.createBackpackData())
					.readFromNBT(compound.getCompoundTag("data"));
	}
	
}

@@ -459,24 +459,122 @@
 ackpack%22;%0A%09%0A
+%09public static final String TAG_STACK = %22stack%22;%0A%09public static final String TAG_DATA = %22data%22;%0A%09%0A
 %09public Item
@@ -963,24 +963,76 @@
 compound) %7B%0A
+%09%09NBTTagCompound properties = new NBTTagCompound();%0A
 %09%09if (backpa
@@ -1055,31 +1055,35 @@
 %0A%09%09%09
-compound.setTag(%22stack%22
+properties.setTag(TAG_STACK
 , ba
@@ -1263,148 +1263,306 @@
 %0A%09%09%09
-compound.setTag(%22data%22, dataCompound);%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Override%0A%09public void loadNBTData(NBTTagCompound compound) %7B%0A%09%09if (compound.hasKey(%22stack%22
+properties.setTag(TAG_DATA, dataCompound);%0A%09%09%7D%0A%09%09compound.setTag(IDENTIFIER, properties);%0A%09%7D%0A%09%0A%09@Override%0A%09public void loadNBTData(NBTTagCompound compound) %7B%0A%09%09%0A%09%09NBTTagCompound properties = compound.getCompoundTag(IDENTIFIER);%0A%09%09if (properties == null) return;%0A%09%09%0A%09%09if (properties.hasKey(TAG_STACK
 ))%0A%09
@@ -1606,32 +1606,34 @@
 tackFromNBT(
-compound
+properties
 .getCompound
@@ -1636,23 +1636,25 @@
 oundTag(
-%22stack%22
+TAG_STACK
 ));%0A%09%09%0A%09
@@ -1795,30 +1795,34 @@
 if (
-compound.hasKey(%22data%22
+properties.hasKey(TAG_DATA
 ) &&
@@ -1926,24 +1926,26 @@
 FromNBT(
-compound
+properties
 .getComp
@@ -1952,22 +1952,24 @@
 oundTag(
-%22data%22
+TAG_DATA
 ));%0A%09%7D%0A%09

Change how backpack properties get saved