    001package net.minecraft.item;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.List;
    006import java.util.Random;
    007import net.minecraft.enchantment.Enchantment;
    008import net.minecraft.enchantment.EnchantmentData;
    009import net.minecraft.entity.player.EntityPlayer;
    010import net.minecraft.nbt.NBTTagCompound;
    011import net.minecraft.nbt.NBTTagList;
    012import net.minecraft.util.MathHelper;
    013import net.minecraft.util.WeightedRandomChestContent;
    014
    015public class ItemEnchantedBook extends Item
    016{
    017    public ItemEnchantedBook(int par1)
    018    {
    019        super(par1);
    020    }
    021
    022    @SideOnly(Side.CLIENT)
    023    public boolean hasEffect(ItemStack par1ItemStack)
    024    {
    025        return true;
    026    }
    027
    028    /**
    029     * Checks isDamagable and if it cannot be stacked
    030     */
    031    public boolean isItemTool(ItemStack par1ItemStack)
    032    {
    033        return false;
    034    }
    035
    036    @SideOnly(Side.CLIENT)
    037
    038    /**
    039     * Return an item rarity from EnumRarity
    040     */
    041    public EnumRarity getRarity(ItemStack par1ItemStack)
    042    {
    043        return this.func_92110_g(par1ItemStack).tagCount() > 0 ? EnumRarity.uncommon : super.getRarity(par1ItemStack);
    044    }
    045
    046    public NBTTagList func_92110_g(ItemStack par1ItemStack)
    047    {
    048        return par1ItemStack.stackTagCompound != null && par1ItemStack.stackTagCompound.hasKey("StoredEnchantments") ? (NBTTagList)par1ItemStack.stackTagCompound.getTag("StoredEnchantments") : new NBTTagList();
    049    }
    050
    051    @SideOnly(Side.CLIENT)
    052
    053    /**
    054     * allows items to add custom lines of information to the mouseover description
    055     */
    056    public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
    057    {
    058        super.addInformation(par1ItemStack, par2EntityPlayer, par3List, par4);
    059        NBTTagList nbttaglist = this.func_92110_g(par1ItemStack);
    060
    061        if (nbttaglist != null)
    062        {
    063            for (int i = 0; i < nbttaglist.tagCount(); ++i)
    064            {
    065                short short1 = ((NBTTagCompound)nbttaglist.tagAt(i)).getShort("id");
    066                short short2 = ((NBTTagCompound)nbttaglist.tagAt(i)).getShort("lvl");
    067
    068                if (Enchantment.enchantmentsList[short1] != null)
    069                {
    070                    par3List.add(Enchantment.enchantmentsList[short1].getTranslatedName(short2));
    071                }
    072            }
    073        }
    074    }
    075
    076    public void func_92115_a(ItemStack par1ItemStack, EnchantmentData par2EnchantmentData)
    077    {
    078        NBTTagList nbttaglist = this.func_92110_g(par1ItemStack);
    079        boolean flag = true;
    080
    081        for (int i = 0; i < nbttaglist.tagCount(); ++i)
    082        {
    083            NBTTagCompound nbttagcompound = (NBTTagCompound)nbttaglist.tagAt(i);
    084
    085            if (nbttagcompound.getShort("id") == par2EnchantmentData.enchantmentobj.effectId)
    086            {
    087                if (nbttagcompound.getShort("lvl") < par2EnchantmentData.enchantmentLevel)
    088                {
    089                    nbttagcompound.setShort("lvl", (short)par2EnchantmentData.enchantmentLevel);
    090                }
    091
    092                flag = false;
    093                break;
    094            }
    095        }
    096
    097        if (flag)
    098        {
    099            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
    100            nbttagcompound1.setShort("id", (short)par2EnchantmentData.enchantmentobj.effectId);
    101            nbttagcompound1.setShort("lvl", (short)par2EnchantmentData.enchantmentLevel);
    102            nbttaglist.appendTag(nbttagcompound1);
    103        }
    104
    105        if (!par1ItemStack.hasTagCompound())
    106        {
    107            par1ItemStack.setTagCompound(new NBTTagCompound());
    108        }
    109
    110        par1ItemStack.getTagCompound().setTag("StoredEnchantments", nbttaglist);
    111    }
    112
    113    public ItemStack func_92111_a(EnchantmentData par1EnchantmentData)
    114    {
    115        ItemStack itemstack = new ItemStack(this);
    116        this.func_92115_a(itemstack, par1EnchantmentData);
    117        return itemstack;
    118    }
    119
    120    @SideOnly(Side.CLIENT)
    121    public void func_92113_a(Enchantment par1Enchantment, List par2List)
    122    {
    123        for (int i = par1Enchantment.getMinLevel(); i <= par1Enchantment.getMaxLevel(); ++i)
    124        {
    125            par2List.add(this.func_92111_a(new EnchantmentData(par1Enchantment, i)));
    126        }
    127    }
    128
    129    public ItemStack func_92109_a(Random par1Random)
    130    {
    131        Enchantment enchantment = Enchantment.field_92090_c[par1Random.nextInt(Enchantment.field_92090_c.length)];
    132        ItemStack itemstack = new ItemStack(this.itemID, 1, 0);
    133        int i = MathHelper.getRandomIntegerInRange(par1Random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    134        this.func_92115_a(itemstack, new EnchantmentData(enchantment, i));
    135        return itemstack;
    136    }
    137
    138    public WeightedRandomChestContent func_92114_b(Random par1Random)
    139    {
    140        return this.func_92112_a(par1Random, 1, 1, 1);
    141    }
    142
    143    public WeightedRandomChestContent func_92112_a(Random par1Random, int par2, int par3, int par4)
    144    {
    145        Enchantment enchantment = Enchantment.field_92090_c[par1Random.nextInt(Enchantment.field_92090_c.length)];
    146        ItemStack itemstack = new ItemStack(this.itemID, 1, 0);
    147        int l = MathHelper.getRandomIntegerInRange(par1Random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    148        this.func_92115_a(itemstack, new EnchantmentData(enchantment, l));
    149        return new WeightedRandomChestContent(itemstack, par2, par3, par4);
    150    }
    151}