package com.five35.minecraft.deathbox;

import java.util.HashMap;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
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);
	}
}

@@ -77,24 +77,52 @@
 a.util.Map;%0A
+import java.util.Map.Entry;%0A
 import net.m
@@ -195,16 +195,94 @@
 mStack;%0A
+import net.minecraft.nbt.NBTTagCompound;%0Aimport net.minecraft.nbt.NBTTagList;%0A
 import n
@@ -1512,11 +1512,783 @@
 ory);%0A%09%7D
+%0A%0A%09@Override%0A%09public void writeToNBT(final NBTTagCompound tag) %7B%0A%09%09super.writeToNBT(tag);%0A%0A%09%09final NBTTagCompound inventoriesTag = new NBTTagCompound();%0A%0A%09%09for (final Entry%3CString, Map%3CInteger, ItemStack%3E%3E inventoryEntry : this.inventories.entrySet()) %7B%0A%09%09%09final NBTTagList inventoryTag = new NBTTagList();%0A%0A%09%09%09for (final Entry%3CInteger, ItemStack%3E slotEntry : inventoryEntry.getValue().entrySet()) %7B%0A%09%09%09%09final NBTTagCompound slotTag = new NBTTagCompound();%0A%0A%09%09%09%09slotEntry.getValue().writeToNBT(slotTag);%0A%09%09%09%09slotTag.setByte(%22slot%22, slotEntry.getKey().byteValue());%0A%0A%09%09%09%09inventoryTag.appendTag(slotTag);%0A%09%09%09%7D%0A%0A%09%09%09inventoriesTag.setTag(inventoryEntry.getKey(), inventoryTag);%0A%09%09%7D%0A%0A%09%09tag.setTag(%22inventories%22, inventoriesTag);%0A%09%09tag.setString(%22ownerName%22, this.ownerName);%0A%09%7D
 %0A%7D%0A

implement writing tile entity data to NBT