    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 var5 = this.func_92110_g(par1ItemStack);
    060
    061        if (var5 != null)
    062        {
    063            for (int var6 = 0; var6 < var5.tagCount(); ++var6)
    064            {
    065                short var7 = ((NBTTagCompound)var5.tagAt(var6)).getShort("id");
    066                short var8 = ((NBTTagCompound)var5.tagAt(var6)).getShort("lvl");
    067
    068                if (Enchantment.enchantmentsList[var7] != null)
    069                {
    070                    par3List.add(Enchantment.enchantmentsList[var7].getTranslatedName(var8));
    071                }
    072            }
    073        }
    074    }
    075
    076    public void func_92115_a(ItemStack par1ItemStack, EnchantmentData par2EnchantmentData)
    077    {
    078        NBTTagList var3 = this.func_92110_g(par1ItemStack);
    079        boolean var4 = true;
    080
    081        for (int var5 = 0; var5 < var3.tagCount(); ++var5)
    082        {
    083            NBTTagCompound var6 = (NBTTagCompound)var3.tagAt(var5);
    084
    085            if (var6.getShort("id") == par2EnchantmentData.enchantmentobj.effectId)
    086            {
    087                if (var6.getShort("lvl") < par2EnchantmentData.enchantmentLevel)
    088                {
    089                    var6.setShort("lvl", (short)par2EnchantmentData.enchantmentLevel);
    090                }
    091
    092                var4 = false;
    093                break;
    094            }
    095        }
    096
    097        if (var4)
    098        {
    099            NBTTagCompound var7 = new NBTTagCompound();
    100            var7.setShort("id", (short)par2EnchantmentData.enchantmentobj.effectId);
    101            var7.setShort("lvl", (short)par2EnchantmentData.enchantmentLevel);
    102            var3.appendTag(var7);
    103        }
    104
    105        if (!par1ItemStack.hasTagCompound())
    106        {
    107            par1ItemStack.setTagCompound(new NBTTagCompound());
    108        }
    109
    110        par1ItemStack.getTagCompound().setTag("StoredEnchantments", var3);
    111    }
    112
    113    public ItemStack func_92111_a(EnchantmentData par1EnchantmentData)
    114    {
    115        ItemStack var2 = new ItemStack(this);
    116        this.func_92115_a(var2, par1EnchantmentData);
    117        return var2;
    118    }
    119
    120    @SideOnly(Side.CLIENT)
    121    public void func_92113_a(Enchantment par1Enchantment, List par2List)
    122    {
    123        for (int var3 = par1Enchantment.getMinLevel(); var3 <= par1Enchantment.getMaxLevel(); ++var3)
    124        {
    125            par2List.add(this.func_92111_a(new EnchantmentData(par1Enchantment, var3)));
    126        }
    127    }
    128
    129    public ItemStack func_92109_a(Random par1Random)
    130    {
    131        Enchantment var2 = Enchantment.field_92090_c[par1Random.nextInt(Enchantment.field_92090_c.length)];
    132        ItemStack var3 = new ItemStack(this.itemID, 1, 0);
    133        int var4 = MathHelper.getRandomIntegerInRange(par1Random, var2.getMinLevel(), var2.getMaxLevel());
    134        this.func_92115_a(var3, new EnchantmentData(var2, var4));
    135        return var3;
    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 var5 = Enchantment.field_92090_c[par1Random.nextInt(Enchantment.field_92090_c.length)];
    146        ItemStack var6 = new ItemStack(this.itemID, 1, 0);
    147        int var7 = MathHelper.getRandomIntegerInRange(par1Random, var5.getMinLevel(), var5.getMaxLevel());
    148        this.func_92115_a(var6, new EnchantmentData(var5, var7));
    149        return new WeightedRandomChestContent(var6, par2, par3, par4);
    150    }
    151}