package com.mcgoodtime.gti.common.items;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;

import java.util.List;

import static com.mcgoodtime.gti.common.core.Gti.MOD_ID;
import static com.mcgoodtime.gti.common.core.Gti.RESOURCE_DOMAIN;

/**
 * Created by BestOwl on 2015.11.5.0005.
 *
 * @author BestOwl
 */
public abstract class ItemMultiDamage extends ItemGti {

    protected List<String> internalNameList;
    protected IIcon[] icons;

    public ItemMultiDamage(String name) {
        super(name);
        int maxDamage = this.getMaxDamage();
        this.setMaxDamage(maxDamage);
        this.setHasSubtypes(true);
        this.icons = new IIcon[maxDamage];
        this.internalNameList = this.getInternalNameList();
    }

    @Override
    public void registerIcons(IIconRegister iconRegister) {
        for (int i = 0; i < this.getMaxDamage(); i++) {
            this.icons[i] = iconRegister.registerIcon(RESOURCE_DOMAIN + ":" + this.getTextureFolder() + "item" + this.getInternalName(i));
        }
    }

    /**
     * Gets an icon index based on an item's damage value
     */
    @Override
    public IIcon getIconFromDamage(int meta) {
        return this.icons[meta];
    }

    /**
     * Returns the maximum damage an item can take.
     */
    @Override
    public int getMaxDamage() {
        if (super.getMaxDamage() == 0) {
            int meta = 0;
            while (this.getInternalName(meta) != null) {
                meta++;
            }
            return meta;
        }
        else {
            return super.getMaxDamage();
        }
    }

    /**
     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
     * different names based on their damage or NBT.
     */
    @Override
    public String getUnlocalizedName(ItemStack itemStack) {
        return "item." + MOD_ID + "." + this.getInternalName(itemStack.getItemDamage());
    }

    public String getInternalName(int meta) {
        return this.internalNameList.get(meta);
    }

    protected abstract List<String> getInternalNameList();

    /**
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
     */
    @Override
    @SuppressWarnings("unchecked")
    public void getSubItems(Item item, CreativeTabs creativeTabs, List list) {
        for(int meta = 0; meta < this.getMaxDamage(); ++meta) {
            ItemStack stack = new ItemStack(this, 1, meta);
            list.add(stack);
        }
    }

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

    /**
     * Determines if the durability bar should be rendered for this item.
     * Defaults to vanilla stack.isDamaged behavior.
     * But modders can use this for any data they wish.
     *
     * @param stack The current Item Stack
     * @return True if it should render the 'durability' bar.
     */
    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean bool) {
        int i = 1;
        String unLocal = this.getUnlocalizedName(itemStack) + ".desc" + i;

        while (StatCollector.canTranslate(unLocal)) {
            list.add(StatCollector.translateToLocal(unLocal));
            i++;
            unLocal = this.getUnlocalizedName() + ".desc" + i;
        }
    }
}

@@ -654,16 +654,45 @@
 NameList
+ = this.getInternalNameList()
 ;%0A    pr
@@ -943,68 +943,8 @@
 e%5D;%0A
-        this.internalNameList = this.getInternalNameList();%0A
     
@@ -1533,95 +1533,21 @@
     
-if (super.getMaxDamage() == 0) %7B%0A            int meta = 0;%0A            while (
+return 
 this.
-getI
+i
 nter
@@ -1557,164 +1557,20 @@
 Name
-(meta) != null) %7B%0A                meta++;%0A            %7D%0A            return meta;%0A        %7D%0A        else %7B%0A            return super.getMaxDamage();%0A        %7D
+List.size();
 %0A   

Fix NullPoint bug.