package com.choonster.testmod3.inventory.itemhandler;

import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.items.ItemStackHandler;

/**
 * An inventory that can be named.
 *
 * @author Choonster
 */
public class ItemHandlerNameable extends ItemStackHandler implements IItemHandlerNameable {
	/**
	 * The default name of this inventory.
	 */
	private final ITextComponent defaultName;

	/**
	 * The custom name of this inventory, if any.
	 */
	private ITextComponent displayName;

	public ItemHandlerNameable(ITextComponent defaultName) {
		this.defaultName = defaultName.createCopy();
	}

	public ItemHandlerNameable(int size, ITextComponent defaultName) {
		super(size);
		this.defaultName = defaultName.createCopy();
	}

	public ItemHandlerNameable(ItemStack[] stacks, ITextComponent defaultName) {
		super(stacks);
		this.defaultName = defaultName.createCopy();
	}

	/**
	 * Get the name of this object. For players this returns their username
	 */
	@Override
	public String getName() {
		return getDisplayName().getUnformattedText();
	}

	/**
	 * Returns true if this thing is named
	 */
	@Override
	public boolean hasCustomName() {
		return displayName != null;
	}

	/**
	 * Get the formatted ChatComponent that will be used for the sender's username in chat
	 */
	@Override
	public ITextComponent getDisplayName() {
		return hasCustomName() ? displayName : defaultName;
	}

	/**
	 * Set the display name of this inventory.
	 *
	 * @param displayName The display name
	 */
	public void setDisplayName(ITextComponent displayName) {
		this.displayName = displayName.createCopy();
	}
}

@@ -85,16 +85,57 @@
 mStack;%0A
+import net.minecraft.nbt.NBTTagCompound;%0A
 import n
@@ -1677,11 +1677,506 @@
 py();%0A%09%7D
+%0A%0A%09@Override%0A%09public NBTTagCompound serializeNBT() %7B%0A%09%09final NBTTagCompound tagCompound = super.serializeNBT();%0A%0A%09%09if (hasCustomName()) %7B%0A%09%09%09tagCompound.setString(%22DisplayName%22, ITextComponent.Serializer.componentToJson(getDisplayName()));%0A%09%09%7D%0A%0A%09%09return tagCompound;%0A%09%7D%0A%0A%09@Override%0A%09public void deserializeNBT(NBTTagCompound nbt) %7B%0A%09%09super.deserializeNBT(nbt);%0A%0A%09%09if (nbt.hasKey(%22DisplayName%22)) %7B%0A%09%09%09setDisplayName(ITextComponent.Serializer.jsonToComponent(nbt.getString(%22DisplayName%22)));%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Save custom name in NBT