package de.impelon.disenchanter.inventory;

import de.impelon.disenchanter.DisenchantingUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class ContainerDisenchantmentManual extends ContainerDisenchantmentBase {
	
	/**
	 * Must <b>NOT</b> be initialized here, as that would override whatever was
	 * initialized during the calling of the super-constructor.
	 */
	protected DisenchantmentItemStackHandler tableContent;

	public ContainerDisenchantmentManual(InventoryPlayer playerInventory, World world, BlockPos position) {
		super(playerInventory, world, position);
	}

	@Override
	protected IDisenchantmentItemHandler getTableInventory() {
		if (this.tableContent == null) {
			this.tableContent = new DisenchantmentItemStackHandler() {
				@Override
				protected void onContentsChanged(int slot) {
					super.onContentsChanged(slot);
					ContainerDisenchantmentManual.this.onTableContentChanged(slot);
				};
			};
		}
		return this.tableContent;
	}

	@Override
	public void onContainerClosed(EntityPlayer p) {
		super.onContainerClosed(p);

		if (!this.world.isRemote)
			InventoryUtils.returnInventoryToPlayer(p, p.world, this.getTableInventory());
	}

	protected void onTableContentChanged(int slot) {
		if (slot != OUTPUT_SLOT)
			this.updateOutput(true);
		this.detectAndSendChanges();
	}

	public void updateOutput(boolean ignoreEnchantmentLoss) {
		if (!this.world.isRemote) {
			DisenchantmentItemStackHandler virtualContent = new DisenchantmentItemStackHandler();
			virtualContent.setSourceStack(this.getTableInventory().getSourceStack().copy());
			virtualContent.setReceiverStack(this.getTableInventory().getReceiverStack().copy());
			ItemStack output = DisenchantingUtils.simulateDisenchantingInInventory(virtualContent, false, ignoreEnchantmentLoss, this.world, this.position, this.random);
			if (!output.isEmpty()) {
				if (!(ItemStack.areItemStacksEqual(this.getTableInventory().getOutputStack(), output)))
					this.getTableInventory().setOutputStack(output);
			} else if (!this.getTableInventory().getOutputStack().isEmpty())
				this.getTableInventory().setOutputStack(ItemStack.EMPTY);
		}
	}

	@Override
	public ItemStack slotClick(int slot, int dragType, ClickType clickType, EntityPlayer player) {
		if (slot == OUTPUT_SLOT && clickType != ClickType.CLONE && !this.world.isRemote) {
			this.updateOutput(false);
			boolean wasEmpty = this.getTableInventory().getOutputStack().isEmpty();
			ItemStack result = super.slotClick(slot, dragType, clickType, player);
			if (this.getTableInventory().getOutputStack().isEmpty() && !wasEmpty)
				DisenchantingUtils.disenchantInInventory(this.getTableInventory(), false, this.world, this.position, this.random);
			this.updateOutput(true);
			if (player instanceof EntityPlayerMP)
				((EntityPlayerMP) player).sendContainerToPlayer(this);
			return result;
		}
		return super.slotClick(slot, dragType, clickType, player);
	}

}

@@ -2551,20 +2551,27 @@
 pe.CLONE
- && 
+) %7B%0A%09%09%09if (
 !this.wo
@@ -2582,24 +2582,25 @@
 isRemote) %7B%0A
+%09
 %09%09%09this.upda
@@ -2616,16 +2616,17 @@
 false);%0A
+%09
 %09%09%09boole
@@ -2691,24 +2691,25 @@
 Empty();%0A%09%09%09
+%09
 ItemStack re
@@ -2766,24 +2766,25 @@
 player);%0A%09%09%09
+%09
 if (this.get
@@ -2841,16 +2841,17 @@
 sEmpty)%0A
+%09
 %09%09%09%09Dise
@@ -2960,24 +2960,25 @@
 random);%0A%09%09%09
+%09
 this.updateO
@@ -2990,16 +2990,17 @@
 (true);%0A
+%09
 %09%09%09if (p
@@ -3032,16 +3032,17 @@
 ayerMP)%0A
+%09
 %09%09%09%09((En
@@ -3095,16 +3095,17 @@
 is);%0A%09%09%09
+%09
 return r
@@ -3111,16 +3111,93 @@
 result;%0A
+%09%09%09%7D else if (clickType == ClickType.QUICK_MOVE)%0A%09%09%09%09return ItemStack.EMPTY;%0A
 %09%09%7D%0A%09%09re

Fix minor ghost item bug