001package net.minecraft.item;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.List;
006import net.minecraft.block.Block;
007import net.minecraft.block.BlockSkull;
008import net.minecraft.client.renderer.texture.IconRegister;
009import net.minecraft.creativetab.CreativeTabs;
010import net.minecraft.entity.player.EntityPlayer;
011import net.minecraft.tileentity.TileEntity;
012import net.minecraft.tileentity.TileEntitySkull;
013import net.minecraft.util.Icon;
014import net.minecraft.util.MathHelper;
015import net.minecraft.util.StatCollector;
016import net.minecraft.world.World;
017
018public class ItemSkull extends Item
019{
020    private static final String[] skullTypes = new String[] {"skeleton", "wither", "zombie", "char", "creeper"};
021    public static final String[] field_94587_a = new String[] {"skull_skeleton", "skull_wither", "skull_zombie", "skull_char", "skull_creeper"};
022    @SideOnly(Side.CLIENT)
023    private Icon[] field_94586_c;
024
025    public ItemSkull(int par1)
026    {
027        super(par1);
028        this.setCreativeTab(CreativeTabs.tabDecorations);
029        this.setMaxDamage(0);
030        this.setHasSubtypes(true);
031    }
032
033    /**
034     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
035     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
036     */
037    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
038    {
039        if (par7 == 0)
040        {
041            return false;
042        }
043        else if (!par3World.getBlockMaterial(par4, par5, par6).isSolid())
044        {
045            return false;
046        }
047        else
048        {
049            if (par7 == 1)
050            {
051                ++par5;
052            }
053
054            if (par7 == 2)
055            {
056                --par6;
057            }
058
059            if (par7 == 3)
060            {
061                ++par6;
062            }
063
064            if (par7 == 4)
065            {
066                --par4;
067            }
068
069            if (par7 == 5)
070            {
071                ++par4;
072            }
073
074            if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack))
075            {
076                return false;
077            }
078            else if (!Block.skull.canPlaceBlockAt(par3World, par4, par5, par6))
079            {
080                return false;
081            }
082            else
083            {
084                par3World.setBlockAndMetadataWithNotify(par4, par5, par6, Block.skull.blockID, par7, 2);
085                int i1 = 0;
086
087                if (par7 == 1)
088                {
089                    i1 = MathHelper.floor_double((double)(par2EntityPlayer.rotationYaw * 16.0F / 360.0F) + 0.5D) & 15;
090                }
091
092                TileEntity tileentity = par3World.getBlockTileEntity(par4, par5, par6);
093
094                if (tileentity != null && tileentity instanceof TileEntitySkull)
095                {
096                    String s = "";
097
098                    if (par1ItemStack.hasTagCompound() && par1ItemStack.getTagCompound().hasKey("SkullOwner"))
099                    {
100                        s = par1ItemStack.getTagCompound().getString("SkullOwner");
101                    }
102
103                    ((TileEntitySkull)tileentity).setSkullType(par1ItemStack.getItemDamage(), s);
104                    ((TileEntitySkull)tileentity).setSkullRotation(i1);
105                    ((BlockSkull)Block.skull).makeWither(par3World, par4, par5, par6, (TileEntitySkull)tileentity);
106                }
107
108                --par1ItemStack.stackSize;
109                return true;
110            }
111        }
112    }
113
114    @SideOnly(Side.CLIENT)
115
116    /**
117     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
118     */
119    public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
120    {
121        for (int j = 0; j < skullTypes.length; ++j)
122        {
123            par3List.add(new ItemStack(par1, 1, j));
124        }
125    }
126
127    /**
128     * Returns the metadata of the block which this Item (ItemBlock) can place
129     */
130    public int getMetadata(int par1)
131    {
132        return par1;
133    }
134
135    @SideOnly(Side.CLIENT)
136
137    /**
138     * Gets an icon index based on an item's damage value
139     */
140    public Icon getIconFromDamage(int par1)
141    {
142        if (par1 < 0 || par1 >= skullTypes.length)
143        {
144            par1 = 0;
145        }
146
147        return this.field_94586_c[par1];
148    }
149
150    /**
151     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
152     * different names based on their damage or NBT.
153     */
154    public String getUnlocalizedName(ItemStack par1ItemStack)
155    {
156        int i = par1ItemStack.getItemDamage();
157
158        if (i < 0 || i >= skullTypes.length)
159        {
160            i = 0;
161        }
162
163        return super.getUnlocalizedName() + "." + skullTypes[i];
164    }
165
166    public String getItemDisplayName(ItemStack par1ItemStack)
167    {
168        return par1ItemStack.getItemDamage() == 3 && par1ItemStack.hasTagCompound() && par1ItemStack.getTagCompound().hasKey("SkullOwner") ? StatCollector.translateToLocalFormatted("item.skull.player.name", new Object[] {par1ItemStack.getTagCompound().getString("SkullOwner")}): super.getItemDisplayName(par1ItemStack);
169    }
170
171    @SideOnly(Side.CLIENT)
172    public void func_94581_a(IconRegister par1IconRegister)
173    {
174        this.field_94586_c = new Icon[field_94587_a.length];
175
176        for (int i = 0; i < field_94587_a.length; ++i)
177        {
178            this.field_94586_c[i] = par1IconRegister.func_94245_a(field_94587_a[i]);
179        }
180    }
181}