package net.mcft.copy.backpacks.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;

import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.mcft.copy.backpacks.api.IBackpackType;

// TODO: Open backpack when interacting with slot?
// FIXME: In 1.11, is it possible to do without this hack?
/** Replaces the player's regular chest armor slot to
 *  prevent them from taking out equipped backpacks. */
public class SlotArmorBackpack extends Slot {
	
	public SlotArmorBackpack(IInventory inventory, int slot, int x, int y) {
		super(inventory, slot, x, y);
	}
	
	/** Replaces the chest armor slot of the player with one that
	 *  prevents backpacks from being taken out, if necessary. */
	public static void replace(EntityPlayer player) {
		Slot slot = player.inventoryContainer.getSlot(6);
		if (slot instanceof SlotArmorBackpack) return;
		Slot newSlot = new SlotArmorBackpack(slot.inventory, slot.getSlotIndex(), slot.xPos, slot.yPos);
		newSlot.slotNumber = slot.slotNumber;
		player.inventoryContainer.inventorySlots.set(slot.slotNumber, newSlot);
	}
	
	
	@Override
	@SideOnly(Side.CLIENT)
	public String getSlotTexture() {
		return ItemArmor.EMPTY_SLOT_NAMES[EntityEquipmentSlot.CHEST.getIndex()];
	}
	
	@Override
	public int getSlotStackLimit() { return 1; }
	
	@Override
	public boolean isItemValid(ItemStack stack) {
		return stack.getItem().isValidArmor(stack, EntityEquipmentSlot.CHEST, ((InventoryPlayer)inventory).player);
	}
	
	@Override
	public boolean canTakeStack(EntityPlayer player) { 
		ItemStack backpack = getStack();
		return ((backpack == null) || !(backpack.getItem() instanceof IBackpackType));
	}
	
}
@@ -37,16 +37,68 @@
 ainer;%0A%0A
+import net.minecraft.enchantment.EnchantmentHelper;%0A
 import n
@@ -605,28 +605,22 @@
 %0A// 
-FIXME: In 1.11, is i
+TODO: Still no
 t po
@@ -649,16 +649,54 @@
 his hack
+ in Vanilla 1.11. Leave it up to Forge
 ?%0A/** Re
@@ -1913,21 +1913,18 @@
 emStack 
-backp
+st
 ack = ge
@@ -1947,37 +1947,12 @@
 rn (
-(backpack == null) %7C%7C !(backp
+!(st
 ack.
@@ -1986,17 +1986,160 @@
 ackType)
-);
+ &&%0A%09%09%09%09(player.isCreative() %7C%7C !EnchantmentHelper.hasBindingCurse(stack)));%0A%09%09// Why, Mojang, whhhyyyyy??! Still no Item.canEquip / canUnequip!?
 %0A%09%7D%0A%09%0A%7D

Fix SlotArmorBackpack to respect binding curse