156 lines
5.2 KiB
Java
156 lines
5.2 KiB
Java
package net.minecraft.item;
|
|
|
|
import java.util.List;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.client.util.ITooltipFlag;
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
import net.minecraft.enchantment.Enchantment;
|
|
import net.minecraft.enchantment.EnchantmentData;
|
|
import net.minecraft.init.Items;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.nbt.NBTTagList;
|
|
import net.minecraft.util.NonNullList;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
public class ItemEnchantedBook extends Item
|
|
{
|
|
/**
|
|
* Returns true if this item has an enchantment glint. By default, this returns
|
|
* <code>stack.isItemEnchanted()</code>, but other items can override it (for instance, written books always return
|
|
* true).
|
|
*
|
|
* Note that if you override this method, you generally want to also call the super version (on {@link Item}) to get
|
|
* the glint for enchanted items. Of course, that is unnecessary if the overwritten version always returns true.
|
|
*/
|
|
@SideOnly(Side.CLIENT)
|
|
public boolean hasEffect(ItemStack stack)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Checks isDamagable and if it cannot be stacked
|
|
*/
|
|
public boolean isEnchantable(ItemStack stack)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Return an item rarity from EnumRarity
|
|
*/
|
|
public EnumRarity getRarity(ItemStack stack)
|
|
{
|
|
return getEnchantments(stack).hasNoTags() ? super.getRarity(stack) : EnumRarity.UNCOMMON;
|
|
}
|
|
|
|
public static NBTTagList getEnchantments(ItemStack p_92110_0_)
|
|
{
|
|
NBTTagCompound nbttagcompound = p_92110_0_.getTagCompound();
|
|
return nbttagcompound != null ? nbttagcompound.getTagList("StoredEnchantments", 10) : new NBTTagList();
|
|
}
|
|
|
|
/**
|
|
* allows items to add custom lines of information to the mouseover description
|
|
*/
|
|
@SideOnly(Side.CLIENT)
|
|
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
|
|
{
|
|
super.addInformation(stack, worldIn, tooltip, flagIn);
|
|
NBTTagList nbttaglist = getEnchantments(stack);
|
|
|
|
for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
|
{
|
|
NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
|
int j = nbttagcompound.getShort("id");
|
|
Enchantment enchantment = Enchantment.getEnchantmentByID(j);
|
|
|
|
if (enchantment != null)
|
|
{
|
|
tooltip.add(enchantment.getTranslatedName(nbttagcompound.getShort("lvl")));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds an stored enchantment to an enchanted book ItemStack
|
|
*/
|
|
public static void addEnchantment(ItemStack p_92115_0_, EnchantmentData stack)
|
|
{
|
|
NBTTagList nbttaglist = getEnchantments(p_92115_0_);
|
|
boolean flag = true;
|
|
|
|
for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
|
{
|
|
NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
|
|
|
if (Enchantment.getEnchantmentByID(nbttagcompound.getShort("id")) == stack.enchantment)
|
|
{
|
|
if (nbttagcompound.getShort("lvl") < stack.enchantmentLevel)
|
|
{
|
|
nbttagcompound.setShort("lvl", (short)stack.enchantmentLevel);
|
|
}
|
|
|
|
flag = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (flag)
|
|
{
|
|
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
|
nbttagcompound1.setShort("id", (short)Enchantment.getEnchantmentID(stack.enchantment));
|
|
nbttagcompound1.setShort("lvl", (short)stack.enchantmentLevel);
|
|
nbttaglist.appendTag(nbttagcompound1);
|
|
}
|
|
|
|
if (!p_92115_0_.hasTagCompound())
|
|
{
|
|
p_92115_0_.setTagCompound(new NBTTagCompound());
|
|
}
|
|
|
|
p_92115_0_.getTagCompound().setTag("StoredEnchantments", nbttaglist);
|
|
}
|
|
|
|
/**
|
|
* Returns the ItemStack of an enchanted version of this item.
|
|
*/
|
|
public static ItemStack getEnchantedItemStack(EnchantmentData p_92111_0_)
|
|
{
|
|
ItemStack itemstack = new ItemStack(Items.ENCHANTED_BOOK);
|
|
addEnchantment(itemstack, p_92111_0_);
|
|
return itemstack;
|
|
}
|
|
|
|
/**
|
|
* returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
|
|
*/
|
|
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> items)
|
|
{
|
|
if (tab == CreativeTabs.SEARCH)
|
|
{
|
|
for (Enchantment enchantment : Enchantment.REGISTRY)
|
|
{
|
|
if (enchantment.type != null)
|
|
{
|
|
for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); ++i)
|
|
{
|
|
items.add(getEnchantedItemStack(new EnchantmentData(enchantment, i)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (tab.getRelevantEnchantmentTypes().length != 0)
|
|
{
|
|
for (Enchantment enchantment1 : Enchantment.REGISTRY)
|
|
{
|
|
if (tab.hasRelevantEnchantmentType(enchantment1.type))
|
|
{
|
|
items.add(getEnchantedItemStack(new EnchantmentData(enchantment1, enchantment1.getMaxLevel())));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |