package net.mcft.copy.backpacks.entity;

import net.mcft.copy.backpacks.api.BackpackHelper;
import net.mcft.copy.backpacks.api.IBackpack;
import net.mcft.copy.backpacks.api.IBackpackData;
import net.mcft.copy.backpacks.api.IBackpackProperties;
import net.mcft.copy.core.misc.SyncedEntityProperties;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

public class BackpackProperties extends SyncedEntityProperties implements IBackpackProperties {
	
	// Used by EntityUtils.getIdentifier().
	public static final String IDENTIFIER = "WearableBackpack";
	
	public static final String TAG_STACK = "stack";
	public static final String TAG_DATA = "data";
	
	public int playersUsing = 0;
	
	private ItemStack backpackStack = null;
	private IBackpackData backpackData = null;
	private IBackpack lastBackpackType = null;
	
	// SyncedEntityProperties methods
	
	@Override
	public EntityLivingBase getEntity() { return (EntityLivingBase)super.getEntity(); }
	
	@Override
	public boolean isWrittenToEntity() { return ((getBackpackStack() != null) || (backpackData != null)); }
	@Override
	public boolean requiresSyncing() { return (getBackpackStack() != null); }
	
	@Override
	public void write(NBTTagCompound compound) {
		if (getBackpackStack() != null)
			compound.setTag(TAG_STACK, getBackpackStack().writeToNBT(new NBTTagCompound()));
	}
	@Override
	public void read(NBTTagCompound compound) {
		if (compound.hasKey(TAG_STACK))
			setBackpackStack(ItemStack.loadItemStackFromNBT(compound.getCompoundTag(TAG_STACK)));
	}
	
	@Override
	public void writeToEntity(NBTTagCompound compound) {
		if (getBackpackData() != null) {
			System.out.println("Write");
			NBTTagCompound dataCompound = new NBTTagCompound();
			getBackpackData().writeToNBT(dataCompound);
			compound.setTag(TAG_DATA, dataCompound);
		}
	}
	@Override
	public void readFromEntity(NBTTagCompound compound) {
		System.out.println("Read");
		ItemStack backpack = BackpackHelper.getEquippedBackpack(getEntity());
		setLastBackpackType(BackpackHelper.getBackpackType(backpack));
		
		if (compound.hasKey(TAG_DATA) && (getLastBackpackType() != null)) {
			IBackpackData data = getLastBackpackType().createBackpackData();
			data.readFromNBT(compound.getCompoundTag(TAG_DATA));
			setBackpackData(data);
		}
	}
	
	// IBackpackProperties implementation
	
	@Override
	public ItemStack getBackpackStack() { return backpackStack; }
	@Override
	public void setBackpackStack(ItemStack stack) { backpackStack = stack; }
	@Override
	public IBackpackData getBackpackData() { return backpackData; }
	@Override
	public void setBackpackData(IBackpackData data) { backpackData = data; }
	@Override
	public IBackpack getLastBackpackType() { return lastBackpackType; }
	@Override
	public void setLastBackpackType(IBackpack type) { lastBackpackType = type; }
	
}

@@ -338,16 +338,48 @@
 ngBase;%0A
+import net.minecraft.item.Item;%0A
 import n
@@ -403,24 +403,24 @@
 .ItemStack;%0A
-
 import net.m
@@ -747,16 +747,63 @@
 %22data%22;%0A
+%09public static final String TAG_TYPE = %22type%22;%0A
 %09%0A%09publi
@@ -1767,34 +1767,224 @@
 %0A%09%09%09
-System.out.println(%22Write%22
+ItemStack backpack = BackpackHelper.getEquippedBackpack(getEntity());%0A%09%09%09if ((getBackpackStack() == null) && (backpack != null))%0A%09%09%09%09compound.setString(TAG_TYPE, Item.itemRegistry.getNameForObject(backpack.getItem())
 );%0A%09
@@ -2205,38 +2205,8 @@
 ) %7B%0A
-%09%09System.out.println(%22Read%22);%0A
 %09%09It
@@ -2275,31 +2275,25 @@
 y());%0A%09%09
-setLast
+I
 Backpack
 Type(Bac
@@ -2288,53 +2288,202 @@
 pack
-Type(BackpackHelper.getBackpackType(backpack)
+ backpackType = ((backpack != null)%0A%09%09%09%09? BackpackHelper.getBackpackType(backpack)%0A%09%09%09%09: (IBackpack)Item.itemRegistry.getObject(compound.getString(TAG_TYPE)));%0A%09%09setLastBackpackType(backpackType
 );%0A%09

Save last backpack type with properties