package com.lionzxy.mysticalisland.common.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;

/**
 * Created by lionzxy on 16.07.15.
 */
public class InventoryGun implements IInventory {

        /**
         * Defining your inventory size this way is handy
         */
        public static final int INV_SIZE = 3;

        /**
         * Inventory's size must be same as number of slots you add to the Container class
         */
        private ItemStack[] inventory = new ItemStack[INV_SIZE];

        /**
         * Provides NBT Tag Compound to reference
         */
        private final ItemStack invItem;

        public InventoryGun(ItemStack stack) {
            this.invItem = stack;
            // Just in case the itemstack doesn't yet have an NBT Tag Compound:
            if (!stack.hasTagCompound()) {
                stack.setTagCompound(new NBTTagCompound());
            }
            // note that it's okay to use stack instead of invItem right there
            // both reference the same memory location, so whatever you change using
            // either reference will change in the other

            // Read the inventory contents from NBT
            readFromNBT(stack.getTagCompound());

        }

        public ItemStack getInvItem() {
            return invItem;
        }

        @Override
        public int getSizeInventory() {
            return inventory.length;
        }

        @Override
        public ItemStack getStackInSlot(int slot) {
            return inventory[slot];
        }

        @Override
        public ItemStack decrStackSize(int slot, int amount) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                if (stack.stackSize > amount) {
                    stack = stack.splitStack(amount);
                    markDirty();
                } else {
                    setInventorySlotContents(slot, null);
                }
            }
            return stack;
        }

        @Override
        public ItemStack getStackInSlotOnClosing(int slot) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                setInventorySlotContents(slot, null);
            }
            return stack;
        }

        @Override
        public void setInventorySlotContents(int slot, ItemStack itemStack) {
            this.inventory[slot] = itemStack;

            if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) {
                itemStack.stackSize = this.getInventoryStackLimit();
            }
            markDirty();
        }

        @Override
        public String getInventoryName() {
            return "Bag Inventory";
        }

        @Override
        public boolean hasCustomInventoryName() {
            return false;
        }

        @Override
        public int getInventoryStackLimit() {
            return 64;
        }

        @Override
        public void markDirty() {
            for (int i = 0; i < getSizeInventory(); i++) {
                if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0)
                    inventory[i] = null;
            }

            writeToNBT(invItem.getTagCompound());
        }

        @Override
        public boolean isUseableByPlayer(EntityPlayer player) {
            return true;
        }

        @Override
        public void openInventory() {

        }

        @Override
        public void closeInventory() {

            writeToNBT(invItem.getTagCompound());
        }

        @Override
        public boolean isItemValidForSlot(int slot, ItemStack itemstack) {
            return true;
        }

        /**
         * A custom method to read our inventory from an ItemStack's NBT compound
         */
        public void readFromNBT(NBTTagCompound tagcompound) {
            // Gets the custom taglist we wrote to this compound.
            NBTTagList items = tagcompound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);

            for (int i = 0; i < items.tagCount(); ++i) {
                NBTTagCompound item = /*(NBTTagCompound)*/ items.getCompoundTagAt(i);
                byte slot = item.getByte("Slot");

                // Just double-checking that the saved slot index is within our inventory array bounds
                if (slot >= 0 && slot < getSizeInventory()) {
                    inventory[slot] = ItemStack.loadItemStackFromNBT(item);
                }
            }
        }

        /**
         * A custom method to write our inventory to an ItemStack's NBT compound
         */
        public void writeToNBT(NBTTagCompound tagcompound) {
            // Create a new NBT Tag List to store itemstacks as NBT Tags
            NBTTagList items = new NBTTagList();

            for (int i = 0; i < getSizeInventory(); ++i) {
               if(inventory[i]!=null && inventory[i].stackSize != 0){
                   NBTTagCompound tag = items.getCompoundTagAt(i);
                   tag.setByte("Slot",(byte)i);
                   inventory[i].writeToNBT(tag);
                   items.appendTag(tag);
               }
            }
            // Add the TagList to the ItemStack's Tag Compound with the name "ItemInventory"
            tagcompound.setTag("ItemInventory", items);
        }

}

@@ -5051,61 +5051,96 @@
     
-if(inventory%5Bi%5D!=null && inventory%5Bi%5D.stackSize != 0)
+ // Only write stacks that contain items%0A                if (getStackInSlot(i) != null) 
 %7B%0A  
@@ -5148,38 +5148,55 @@
                 
+ // Make a new 
 NBT
+ 
 Tag
+ 
 Compound tag = i
@@ -5193,37 +5193,109 @@
 nd t
-ag = items.getCompoundTagAt(i
+o write the itemstack and slot index to%0A                    NBTTagCompound item = new NBTTagCompound(
 );%0A 
@@ -5316,27 +5316,32 @@
     
-tag.setByte
+ item.setInteger
 (%22Slot%22,
 (byt
@@ -5340,14 +5340,9 @@
 ot%22,
-(byte)
+ 
 i);%0A
@@ -5364,38 +5364,195 @@
     
-inventory%5Bi%5D.writeToNBT(tag);%0A
+ // Writes the itemstack in slot(i) to the Tag Compound we just made%0A                    getStackInSlot(i).writeToNBT(item);%0A%0A                    // add the tag compound to our tag list%0A 
     
@@ -5586,14 +5586,16 @@
 Tag(
-tag
+item
 );%0A
+ 
     

Fix Slot bug