package com.five35.minecraft.deathbox;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class DeathBoxTileEntity extends TileEntity {
	private static void arrayToMap(final ItemStack[] array, final Map<Integer, ItemStack> map) {
		for (int i = 0; i < array.length; i++) {
			final ItemStack stack = array[i];

			if (stack != null) {
				map.put(i, stack.copy());
				array[i] = null;
			}
		}
	}

	private final Map<String, Map<Integer, ItemStack>> inventories = new HashMap<>();
	private String ownerName;

	public String getOwnerName() {
		return this.ownerName;
	}

	public void pop() {
		// drop contents on ground
	}

	public void recover(final EntityPlayer player) {
		// add items to player, dropping stacks for conflicting slots

		// destroy death box
	}

	public void store(final EntityPlayer player) {
		this.ownerName = player.getCommandSenderName();

		final Map<Integer, ItemStack> mainInventory = new HashMap<>();
		final Map<Integer, ItemStack> armorInventory = new HashMap<>();

		this.inventories.put("mainInventory", mainInventory);
		this.inventories.put("armorInventory", armorInventory);

		DeathBoxTileEntity.arrayToMap(player.inventory.mainInventory, mainInventory);
		DeathBoxTileEntity.arrayToMap(player.inventory.armorInventory, armorInventory);
	}

	@Override
	public void writeToNBT(final NBTTagCompound tag) {
		super.writeToNBT(tag);

		final NBTTagCompound inventoriesTag = new NBTTagCompound();

		for (final Entry<String, Map<Integer, ItemStack>> inventoryEntry : this.inventories.entrySet()) {
			final NBTTagList inventoryTag = new NBTTagList();

			for (final Entry<Integer, ItemStack> slotEntry : inventoryEntry.getValue().entrySet()) {
				final NBTTagCompound slotTag = new NBTTagCompound();

				slotEntry.getValue().writeToNBT(slotTag);
				slotTag.setByte("slot", slotEntry.getKey().byteValue());

				inventoryTag.appendTag(slotTag);
			}

			inventoriesTag.setTag(inventoryEntry.getKey(), inventoryTag);
		}

		tag.setTag("inventories", inventoriesTag);
		tag.setString("ownerName", this.ownerName);
	}
}

@@ -856,24 +856,843 @@
  ground%0A%09%7D%0A%0A
+%09@Override%0A%09public void readFromNBT(final NBTTagCompound tag) %7B%0A%09%09super.readFromNBT(tag);%0A%0A%09%09this.inventories.clear();%0A%0A%09%09final NBTTagCompound inventoriesTag = tag.getCompoundTag(%22inventories%22);%0A%0A%09%09for (final Object key : inventoriesTag.func_150296_c()) %7B%0A%09%09%09final String inventoryName = (String) key;%0A%09%09%09final NBTTagList inventoryTag = inventoriesTag.getTagList(inventoryName, tag.getId());%0A%09%09%09final Map%3CInteger, ItemStack%3E inventory = new HashMap%3C%3E();%0A%0A%09%09%09for (int i = 0; i %3C inventoryTag.tagCount(); i++) %7B%0A%09%09%09%09final NBTTagCompound slotTag = inventoryTag.getCompoundTagAt(i);%0A%09%09%09%09final ItemStack stack = ItemStack.loadItemStackFromNBT(slotTag);%0A%0A%09%09%09%09inventory.put((int) slotTag.getByte(%22slot%22), stack);%0A%09%09%09%7D%0A%0A%09%09%09this.inventories.put(inventoryName, inventory);%0A%09%09%7D%0A%0A%09%09this.ownerName = tag.getString(%22ownerName%22);%0A%09%7D%0A%0A
 %09public void

implement reading tile entity from NBT