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 static final String TAG_STACK = "stack";
	public static final String TAG_DATA = "data";
	
	public ItemStack backpackStack = null;
	public IBackpackData backpackData = null;
	
	public IBackpack lastBackpackType = null;
	public int playersUsing = 0;
	
	private EntityLivingBase entity;
	
	@Override
	public void init(Entity entity, World world) {
		this.entity = (EntityLivingBase)entity;
	}
	
	@Override
	public void saveNBTData(NBTTagCompound compound) {
		NBTTagCompound properties = new NBTTagCompound();
		if (backpackStack != null)
			properties.setTag(TAG_STACK, backpackStack.writeToNBT(new NBTTagCompound()));
		if (backpackData != null) {
			NBTTagCompound dataCompound = new NBTTagCompound();
			backpackData.writeToNBT(dataCompound);
			properties.setTag(TAG_DATA, dataCompound);
		}
		compound.setTag(IDENTIFIER, properties);
	}
	
	@Override
	public void loadNBTData(NBTTagCompound compound) {
		
		NBTTagCompound properties = compound.getCompoundTag(IDENTIFIER);
		if (properties == null) return;
		
		if (properties.hasKey(TAG_STACK))
			backpackStack = ItemStack.loadItemStackFromNBT(properties.getCompoundTag(TAG_STACK));
		
		ItemStack backpack = BackpackHelper.getEquippedBackpack(entity);
		lastBackpackType = BackpackHelper.getBackpackType(backpack);
		
		if (properties.hasKey(TAG_DATA) && (lastBackpackType != null))
			(backpackData = lastBackpackType.createBackpackData())
					.readFromNBT(properties.getCompoundTag(TAG_DATA));
	}
	
}

@@ -43,38 +43,57 @@
 rt net.m
-inecraft.entity.Entity
+cft.copy.core.misc.SyncedEntityProperties
 ;%0Aimport
@@ -213,102 +213,8 @@
 und;
-%0Aimport net.minecraft.world.World;%0Aimport net.minecraftforge.common.IExtendedEntityProperties;
 %0A%0Apu
@@ -247,26 +247,20 @@
 ies 
-implements IExtend
+extends Sync
 edEn
@@ -645,145 +645,295 @@
 %0A%09%0A%09
-private EntityLivingBase entity;%0A%09%0A%09@Override%0A%09public void init(Entity entity, World world) %7B%0A%09%09this.entity = (EntityLivingBase)entity;%0A%09
+@Override%0A%09public EntityLivingBase getEntity() %7B return (EntityLivingBase)super.getEntity(); %7D%0A%09%0A%09@Override%0A%09public boolean isWrittenToEntity() %7B return ((backpackStack != null) %7C%7C (backpackData != null)); %7D%0A%09@Override%0A%09public boolean requiresSyncing() %7B return (backpackStack != null); 
 %7D%0A%09%0A
@@ -960,19 +960,13 @@
 oid 
-saveNBTData
+write
 (NBT
@@ -995,64 +995,210 @@
 %7B%0A%09%09
-NBTTagCompound properties = new NBTTagCompound();%0A%09%09if (
+if (backpackStack != null)%0A%09%09%09compound.setTag(TAG_STACK, backpackStack.writeToNBT(new NBTTagCompound()));%0A%09%7D%0A%09@Override%0A%09public void read(NBTTagCompound compound) %7B%0A%09%09if (compound.hasKey(TAG_STACK))%0A%09%09%09
 back
@@ -1211,78 +1211,121 @@
 ack 
-!
 = 
-null)%0A%09%09%09properties.setTag(TAG_STACK, backpackStack.writeToNBT(new 
+ItemStack.loadItemStackFromNBT(compound.getCompoundTag(TAG_STACK));%0A%09%7D%0A%09%0A%09@Override%0A%09public void writeToEntity(
 NBTT
@@ -1334,21 +1334,28 @@
 Compound
-()));
+ compound) %7B
 %0A%09%09if (b
@@ -1477,26 +1477,24 @@
 nd);%0A%09%09%09
-properties
+compound
 .setTag(
@@ -1526,56 +1526,11 @@
 %09%09%7D%0A
-%09%09compound.setTag(IDENTIFIER, properties);%0A
 %09%7D%0A
-%09%0A
 %09@Ov
@@ -1553,19 +1553,22 @@
 oid 
-loadNBTData
+readFromEntity
 (NBT
@@ -1595,243 +1595,8 @@
 ) %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%09%09backpackStack = ItemStack.loadItemStackFromNBT(properties.getCompoundTag(TAG_STACK));%0A%09%09%0A
 %09%09It
@@ -1649,22 +1649,27 @@
 ackpack(
-e
+getE
 ntity
+()
 );%0A%09%09las
@@ -1735,26 +1735,24 @@
 %09%0A%09%09if (
-properties
+compound
 .hasKey(
@@ -1872,18 +1872,16 @@
 NBT(
-properties
+compound
 .get

Update backpack properties to synced properties