| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.enchantment.Enchantment;
|
| 6 | import net.minecraft.enchantment.EnchantmentData;
|
| 7 | import net.minecraft.enchantment.EnchantmentHelper;
|
| 8 | import net.minecraft.entity.player.EntityPlayer;
|
| 9 | import net.minecraft.init.Items;
|
| 10 | import net.minecraft.nbt.NBTTagCompound;
|
| 11 | import net.minecraft.nbt.NBTTagList;
|
| 12 | import net.minecraft.util.WeightedRandomChestContent;
|
| 13 |
|
| 14 | public class ItemEnchantedBook extends Item
|
| 15 | {
|
| 16 | public boolean hasEffect(ItemStack stack)
|
| 17 | {
|
| 18 | return true;
|
| 19 | }
|
| 20 |
|
| 21 | /**
|
| 22 | * Checks isDamagable and if it cannot be stacked
|
| 23 | */
|
| 24 | public boolean isItemTool(ItemStack stack)
|
| 25 | {
|
| 26 | return false;
|
| 27 | }
|
| 28 |
|
| 29 | /**
|
| 30 | * Return an item rarity from EnumRarity
|
| 31 | */
|
| 32 | public EnumRarity getRarity(ItemStack stack)
|
| 33 | {
|
| 34 | return this.getEnchantments(stack).tagCount() > 0 ? EnumRarity.UNCOMMON : super.getRarity(stack);
|
| 35 | }
|
| 36 |
|
| 37 | public NBTTagList getEnchantments(ItemStack stack)
|
| 38 | {
|
| 39 | NBTTagCompound nbttagcompound = stack.getTagCompound();
|
| 40 | return nbttagcompound != null && nbttagcompound.hasKey("StoredEnchantments", 9) ? (NBTTagList)nbttagcompound.getTag("StoredEnchantments") : new NBTTagList();
|
| 41 | }
|
| 42 |
|
| 43 | /**
|
| 44 | * allows items to add custom lines of information to the mouseover description
|
| 45 | */
|
| 46 | public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
|
| 47 | {
|
| 48 | super.addInformation(stack, playerIn, tooltip, advanced);
|
| 49 | NBTTagList nbttaglist = this.getEnchantments(stack);
|
| 50 |
|
| 51 | if (nbttaglist != null)
|
| 52 | {
|
| 53 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 54 | {
|
| 55 | int j = nbttaglist.getCompoundTagAt(i).getShort("id");
|
| 56 | int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");
|
| 57 |
|
| 58 | if (Enchantment.getEnchantmentById(j) != null)
|
| 59 | {
|
| 60 | tooltip.add(Enchantment.getEnchantmentById(j).getTranslatedName(k));
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Adds an stored enchantment to an enchanted book ItemStack
|
| 68 | */
|
| 69 | public void addEnchantment(ItemStack stack, EnchantmentData enchantment)
|
| 70 | {
|
| 71 | NBTTagList nbttaglist = this.getEnchantments(stack);
|
| 72 | boolean flag = true;
|
| 73 |
|
| 74 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 75 | {
|
| 76 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 77 |
|
| 78 | if (nbttagcompound.getShort("id") == enchantment.enchantmentobj.effectId)
|
| 79 | {
|
| 80 | if (nbttagcompound.getShort("lvl") < enchantment.enchantmentLevel)
|
| 81 | {
|
| 82 | nbttagcompound.setShort("lvl", (short)enchantment.enchantmentLevel);
|
| 83 | }
|
| 84 |
|
| 85 | flag = false;
|
| 86 | break;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | if (flag)
|
| 91 | {
|
| 92 | NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
| 93 | nbttagcompound1.setShort("id", (short)enchantment.enchantmentobj.effectId);
|
| 94 | nbttagcompound1.setShort("lvl", (short)enchantment.enchantmentLevel);
|
| 95 | nbttaglist.appendTag(nbttagcompound1);
|
| 96 | }
|
| 97 |
|
| 98 | if (!stack.hasTagCompound())
|
| 99 | {
|
| 100 | stack.setTagCompound(new NBTTagCompound());
|
| 101 | }
|
| 102 |
|
| 103 | stack.getTagCompound().setTag("StoredEnchantments", nbttaglist);
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Returns the ItemStack of an enchanted version of this item.
|
| 108 | */
|
| 109 | public ItemStack getEnchantedItemStack(EnchantmentData data)
|
| 110 | {
|
| 111 | ItemStack itemstack = new ItemStack(this);
|
| 112 | this.addEnchantment(itemstack, data);
|
| 113 | return itemstack;
|
| 114 | }
|
| 115 |
|
| 116 | public void getAll(Enchantment enchantment, List<ItemStack> list)
|
| 117 | {
|
| 118 | for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); ++i)
|
| 119 | {
|
| 120 | list.add(this.getEnchantedItemStack(new EnchantmentData(enchantment, i)));
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public WeightedRandomChestContent getRandom(Random rand)
|
| 125 | {
|
| 126 | return this.getRandom(rand, 1, 1, 1);
|
| 127 | }
|
| 128 |
|
| 129 | public WeightedRandomChestContent getRandom(Random rand, int minChance, int maxChance, int weight)
|
| 130 | {
|
| 131 | ItemStack itemstack = new ItemStack(Items.book, 1, 0);
|
| 132 | EnchantmentHelper.addRandomEnchantment(rand, itemstack, 30);
|
| 133 | return new WeightedRandomChestContent(itemstack, minChance, maxChance, weight);
|
| 134 | }
|
| 135 | }
|
| 136 |
|